prithivMLmods/Coder-Stat huggingface dataset:
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int const SIZ = 100000001;
bitset<SIZ> chk;

void sieve() {
  chk.set();
  chk.set(0, 0);
  chk.set(1, 0);
  for (int i = 4; i < SIZ; i += 2) {
    chk.set(i, 0);
  }
  for (int i = 3; i < SIZ; i += 2) {
    if (!chk[i])
      continue;
    for (int j = 2; i * j < SIZ; ++j) {
      chk.set(i * j, 0);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  sieve();

  int n;
  cin >> n;
  int m, ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    if (chk[m]) {
      ++ans;
      chk[m] = 0;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int const SIZ = 100000001;
bitset<SIZ> chk;

void sieve() {
  chk.set();
  chk.set(0, 0);
  chk.set(1, 0);
  for (int i = 4; i < SIZ; i += 2) {
    chk.set(i, 0);
  }
  for (int i = 3; i * i < SIZ; i += 2) {
    if (!chk[i])
      continue;
    for (int j = 2; i * j < SIZ; ++j) {
      chk.set(i * j, 0);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  sieve();

  int n;
  cin >> n;
  int m, ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> m;
    if (chk[m]) {
      ++ans;
      chk[m] = 0;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(n) for (int i = 0; i < (n); i++)
#define pb(n) push_back((n))

// typedef long long ll;

using namespace std;

// vector<bool> p(100000100,true);

// void makep(){
// 	for(int i=0; i<100000100; i++){
// 		if(i<2)p[i]=false;
// 		for(int j=2; j<=sqrt(i); j++){
// 			if(i%j==0){
// 				p[i]=false;
// 				break;
// 			}
// 		}
// 	}
// }

bool is_p(int n) {
  if (n < 2)
    return false;
  for (int i = 0; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, cnt = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (is_p(a))
      cnt++;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(n) for (int i = 0; i < (n); i++)
#define pb(n) push_back((n))

// typedef long long ll;

using namespace std;

// vector<bool> p(100000100,true);

// void makep(){
// 	for(int i=0; i<100000100; i++){
// 		if(i<2)p[i]=false;
// 		for(int j=2; j<=sqrt(i); j++){
// 			if(i%j==0){
// 				p[i]=false;
// 				break;
// 			}
// 		}
// 	}
// }

bool is_p(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, cnt = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (is_p(a))
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int a, d = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    int b;
    cin >> b;
    d++;
    for (int j = 2; j < b / 2 + 1; j++) {
      if (b % j == 0) {
        d--;
        break;
      }
    }
  }
  cout << d << endl;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int a, d = 0;
  cin >> a;
  for (int i = 0; i < a; i++) {
    int b;
    cin >> b;
    d++;
    for (int j = 2; j * j <= b; j++) {
      if (b % j == 0) {
        d--;
        break;
      }
    }
  }
  cout << d << endl;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n;
int a;
int p = 0;

void is_prime() {
  for (int i = 2; i <= a / 2; i++) {
    if (a % i == 0) {
      return;
    }
  }

  p++;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    is_prime();
  }

  cout << p << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n;
int a;
int p = 0;

void is_prime() {
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      return;
    }
  }

  p++;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    is_prime();
  }

  cout << p << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
bool IsPrime(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0) {
    return false;
  }
  for (int i = 3; i < a; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  int a = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (IsPrime(a))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
bool IsPrime(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0) {
    return false;
  }
  for (int i = 3; i <= sqrt(a); i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  int a = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (IsPrime(a))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {

  int n;
  scanf("%d", &n);
  int x[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
  }

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (isPrime(x[i])) {
      ++cnt;
    }
  }
  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0)
      return false;
    ++i;
  }
  return true;
}

int main() {

  int n;
  scanf("%d", &n);
  int x[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x[i]);
  }

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (isPrime(x[i])) {
      ++cnt;
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;
int gcd(int a1, int b1) {
  if (a1 == 0 || b1 == 0) {
    return 0;
  }
  if (a1 >= b1) {
    if (a1 % b1 == 0) {
      return b1;
    } else {
      return gcd(b1, a1 % b1);
    }
  } else {
    if (b1 % a1 == 0) {
      return a1;
    } else {
      return gcd(a1, b1 % a1);
    }
  }
}

int main(void) {
  // Here your code !
  int n, k, p = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    k = 0;
    cin >> a[i];
    if (a[i] == 2) {
      p++;
      continue;
    } else if (a[i] % 2 == 0) {
      continue;
    }
    for (int j = 0; j < floor(sqrt(a[i])); j++) {
      if (gcd(a[i], j + 1) == j + 1) {
        k++;
      }
    }
    if (k == 1) {
      p++;
    }
  }
  cout << p << endl;
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int gcd(int a1, int b1) {
  if (a1 == 0 || b1 == 0) {
    return 0;
  }
  if (a1 >= b1) {
    if (a1 % b1 == 0) {
      return b1;
    } else {
      return gcd(b1, a1 % b1);
    }
  } else {
    if (b1 % a1 == 0) {
      return a1;
    } else {
      return gcd(a1, b1 % a1);
    }
  }
}

int main(void) {
  // Here your code !
  int n, k, p = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    k = 0;
    cin >> a[i];
    if (a[i] == 2) {
      p++;
      continue;
    } else if (a[i] % 2 == 0) {
      continue;
    }
    for (int j = 0; j < floor(sqrt(a[i])); j++) {
      if (a[i] % (j + 1) == 0) {
        k++;
      }
    }
    if (k == 1) {
      p++;
    }
  }
  cout << p << endl;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

bool Sieve(int x) {
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  vector<int> items;
  // input
  cin >> n;
  items.resize(n);
  for (int i = 0; i < n; i++)
    cin >> items[i];

  // process
  int count = 0;
  for (int y : items)
    if (Sieve(y))
      count++;
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

bool Sieve(int x) {
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  vector<int> items;
  // input
  cin >> n;
  items.resize(n);
  for (int i = 0; i < n; i++)
    cin >> items[i];

  // process
  int count = 0;
  for (int i = 0; i < n; i++)
    if (Sieve(items[i]))
      count++;
  cout << count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

bool isPrime(int x) {
  for (int i = 2; i < x; ++i) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int n, x, c = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    if (isPrime(x)) {
      ++c;
    }
  }
  cout << c << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

bool isPrime(int x) {
  int max = sqrt(x);
  for (int i = 2; i <= max; ++i) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int n, x, c = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    if (isPrime(x)) {
      ++c;
    }
  }
  cout << c << endl;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

bool isPrime(std::size_t a) {
  if (a == 2) {
    return true;
  } else if (a % 2 == 0) {
    return false;
  }

  for (std::size_t i = 3; i < a / 2; i = i + 2) {
    if (a % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  std::size_t n;
  std::cin >> n;

  std::size_t count = 0;
  for (std::size_t i = 0; i < n; ++i) {
    std::size_t a;
    std::cin >> a;
    if (isPrime(a)) {
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

bool isPrime(std::size_t a) {
  if (a == 2) {
    return true;
  } else if (a % 2 == 0) {
    return false;
  }

  for (std::size_t i = 3; i <= (std::size_t)(sqrt(a)); i = i + 2) {
    if (a % i == 0) {
      return false;
    }
  }

  return true;
}

int main() {
  std::size_t n;
  std::cin >> n;

  std::size_t count = 0;
  for (std::size_t i = 0; i < n; ++i) {
    std::size_t a;
    std::cin >> a;
    if (isPrime(a)) {
      count++;
    }
  }

  std::cout << count << std::endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;

  int cnt = 0;
  int num = 0;
  for (int i = 0; i < N; i++) {
    cin >> num;
    for (int j = 1; j < num; j++) {
      if (j == 1) {
        continue;
      }
      if (num % j == 0) {
        num = 0;
        break;
      }
    }
    if (num != 0) {
      cnt += 1;
    }
  }

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;

  int cnt = 0;
  int num = 0;
  for (int i = 0; i < N; i++) {
    cin >> num;
    if (num == 2) {
      cnt += 1;
      continue;
    }
    if (num % 2 == 0) {
      continue;
    }

    double sqrtnum = sqrt(num);
    for (int j = 3; j <= sqrtnum; j += 2) {
      if (num % j == 0) {
        num = 0;
        break;
      }
    }
    if (num != 0) {
      cnt += 1;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cassert>
#include <iostream>
#include <math.h>
using namespace std;

bool isPrime(int x) {
  if (x < 2)
    return false;
  else if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  int limit = (int)sqrt((double)x) + 1;
  for (int i = 3; i < x / 2; i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, x;
  int cnt = 0;
  cin >> n;
  assert(1 <= n && n <= 10000);
  for (int i = 0; i < n; i++) {
    cin >> x;
    assert(2 <= x && x <= 1000000000);
    if (isPrime(x))
      cnt++;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <cassert>
#include <iostream>
#include <math.h>
using namespace std;

bool isPrime(int x) {
  if (x < 2)
    return false;
  else if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  int limit = (int)sqrt((double)x) + 1;
  for (int i = 3; i <= limit; i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, x;
  int cnt = 0;
  cin >> n;
  assert(1 <= n && n <= 10000);
  for (int i = 0; i < n; i++) {
    cin >> x;
    assert(2 <= x && x <= 1000000000);
    if (isPrime(x))
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define INT_MIN -2000000000
#define MAX_R 200001
using namespace std;

int main() {
  int n, k;
  while (cin >> n) {
    int max = INT_MIN;
    int r[MAX_R];
    k = n;
    while (k--) {
      cin >> r[k];
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (r[i] - r[j] > max)
          max = r[i] - r[j];
      }
    }
    cout << max << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define INT_MIN -2000000000
#define MAX_R 200001
using namespace std;

int main() {
  int n, k;
  while (cin >> n) {
    int max = INT_MIN;
    int r[MAX_R];
    k = n;
    while (k--) {
      cin >> r[k];
    }
    for (int i = 0; i < n; i++) {
      if (r[i] < max)
        continue;
      for (int j = i + 1; j < n; j++) {
        if (r[i] - r[j] > max)
          max = r[i] - r[j];
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%lld", &v[i]);
  }
  ll ans = -INF;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ans = max(ans, v[j] - v[i]);
    }
  }
  fprintf(stdout, "%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%lld", &v[i]);
  }
  ll ans = -INF;
  ll mn = v[0];
  for (int i = 1; i < n; ++i) {
    ans = max(ans, v[i] - mn);
    mn = min(mn, v[i]);
  }
  fprintf(stdout, "%lld\n", ans);
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>

using namespace std;

static const int MAX = 200000;

int main() {
  int num = 0, maxProfit = -1000000000;
  int value[MAX] = {0};

  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> value[i];
  }

  for (int i = 0; i < num; i++) {
    for (int j = i + 1; j < num; j++) {
      maxProfit = max(maxProfit, value[j] - value[i]);
    }
  }

  cout << maxProfit << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

static const int MAX = 200000;

int main() {
  int num = 0, maxProfit = -1000000000;
  int value[MAX] = {0};

  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> value[i];
  }

  int minValue = value[0];

  for (int i = 1; i < num; i++) {
    maxProfit = max(maxProfit, value[i] - minValue);
    minValue = min(minValue, value[i]);
  }

  cout << maxProfit << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> Vector;
typedef vector<vector<ll>> DVector;

#define fi first
#define se second
#define pb push_back
#define INF INT_MAX
#define bcnt __builtin_popcount
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define ub(x, y) upper_bound(all(x), y) - x.begin()
#define lb(x, y) lower_bound(all(x), y) - x.begin()
#define uni(x) x.erase(unique(all(x)), x.end())
#define rep(i, n) repl(i, 0, n - 1)
#define repl(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define exist(x, y) (find(all(x), y) != x.end())
#define each(itr, v) for (auto itr : v)
#define usort(x) sort(all(x))
#define dsort(x) sort(all(x), greater<int>())
#define mkp(x, y) make_pair(x, y)

int main() {
  cin.sync_with_stdio(false);
  int n;
  cin >> n;
  Vector a(n);
  int mx = -INF;
  rep(i, n) cin >> a[i];
  rep(i, n) rep(j, i) maxch(mx, a[i] - a[j]);
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> Vector;
typedef vector<vector<ll>> DVector;

#define fi first
#define se second
#define pb push_back
#define INF INT_MAX
#define bcnt __builtin_popcount
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define ub(x, y) upper_bound(all(x), y) - x.begin()
#define lb(x, y) lower_bound(all(x), y) - x.begin()
#define uni(x) x.erase(unique(all(x)), x.end())
#define rep(i, n) repl(i, 0, n - 1)
#define repl(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define exist(x, y) (find(all(x), y) != x.end())
#define each(itr, v) for (auto itr : v)
#define usort(x) sort(all(x))
#define dsort(x) sort(all(x), greater<int>())
#define mkp(x, y) make_pair(x, y)

int main() {
  cin.sync_with_stdio(false);
  int n;
  cin >> n;
  Vector a(n);
  int mx = -INF;
  rep(i, n) cin >> a[i];
  Vector table(n);
  table[0] = a[0];
  repl(i, 1, n - 1) table[i] = mmin(table[i - 1], a[i]);
  repl(i, 1, n - 1) maxch(mx, a[i] - table[i - 1]);
  cout << mx << endl;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> R(n);
  for (int i = 0; i < n; ++i)
    cin >> R[i];
  auto itr_min = min_element(R.begin(), R.end());
  auto itr_max = max_element(itr_min, R.end());
  cout << ((*itr_max - *itr_min) <= 0 ? -1 : (*itr_max - *itr_min)) << endl;
  system("pause");
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

int main() {
  int n;
  cin >> n;
  int ans = -999999999, minv;
  cin >> minv;
  for (int i = 1; i < n; ++i) {
    int a;
    cin >> a;
    ans = max(ans, a - minv);
    minv = min(minv, a);
  }
  cout << ans << endl;
  // system("pause");
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int r[n];
  for (int i = 0; i < n; ++i) {
    cin >> r[i];
  }
  int M = r[1] - r[0];
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      M = max(M, r[j] - r[i]);
    }
  }
  cout << M << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int M = -1000000000;
  int m;
  cin >> m;
  for (int i = 1; i < n; ++i) {
    int r;
    cin >> r;
    M = max(M, r - m);
    m = min(m, r);
  }
  cout << M << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int d[200000 + 2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &d[i]);

  int maxv = -1000000001; //(n>=2)?d[1]-d[0]:-1;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (maxv < d[j] - d[i])
        maxv = d[j] - d[i];
    }
  }

  printf("%d\n", maxv);

  return 0;
}

Fixed submission:
#include <cstdio>
int d[200000 + 2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &d[i]);

  int maxv = d[1] - d[0]; // 2 <= n < 200000
  int minv = d[0]; // Store a mininum value and get diff value with next value
                   // after next mininum
  for (int i = 1; i < n; i++) {
    maxv = (d[i] - minv > maxv) ? (d[i] - minv) : maxv;
    minv = (minv > d[i]) ? d[i] : minv;
  } //	local save history value

  printf("%d\n", maxv);

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
typedef int TInt;
typedef std::vector<TInt> TList;
TList Input() {
  int Count;
  std::cin >> Count;
  TList List(Count);
  for (int i = 0; i < Count; ++i) {
    std::cin >> List[i];
  }
  return List;
}
int main() {
  TList A = Input();
  assert(A.size() >= 2);
  int Profit = A[1] - A[0];
  int i = 0;
  while (i < A.size()) {
    int j = i + 1;
    for (int j = i + 1; j < A.size(); ++j) {
      int Diff = A[j] - A[i];
      if (Diff < 0) {
        i = j;
        break;
      }
      if (Diff > Profit)
        Profit = Diff;
    }
    if (j == A.size())
      break;
  }

  std::cout << Profit << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
typedef int TInt;
typedef std::vector<TInt> TList;
TList Input() {
  int Count;
  std::cin >> Count;
  TList List(Count);
  for (int i = 0; i < Count; ++i) {
    std::cin >> List[i];
  }
  return List;
}
int main() {
  TList A = Input();
  assert(A.size() >= 2);
  int Profit = A[1] - A[0];
  int i = 0;
  while (i < A.size()) {
    int j;
    for (j = i + 1; j < A.size(); ++j) {
      int Diff = A[j] - A[i];
      if (Diff < 0) {
        i = j;
        break;
      }
      if (Diff > Profit)
        Profit = Diff;
    }
    if (j == A.size())
      break;
  }

  std::cout << Profit << std::endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int N, A[200000];

int solve(void) {
  int maxp = 1 - 1000000000;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      maxp = max(maxp, A[j] - A[i]);
    }
  }
  return maxp;
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  cout << solve() << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int N, A[200000];

int solve(void) {
  auto maxp = A[1] - A[0];
  auto minr = min(A[0], A[1]);
  for (auto i = 2; i < N; i++) {
    maxp = max(maxp, A[i] - minr);
    minr = min(minr, A[i]);
  }
  return maxp;
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  cout << solve() << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int R[MAX], n;

  cin >> n;
  for (int i = 0; i = n; i++)
    cin >> R[i];

  int maxv = -2000000000;
  int minv = R[0];

  for (int i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }

  cout << maxv << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int R[MAX], n;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];

  int maxv = -2000000000;
  int minv = R[0];

  for (int i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }

  cout << maxv << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

int main() {
  std::vector<long> Dat = {};
  int t = 0;
  std::cin >> t;
  for (int i = 0; i < t; i++) {
    int tmp = 0;
    std::cin >> tmp;
    Dat.push_back(tmp);
  }
  long max = *(Dat.begin() + 1) - *Dat.begin();
  for (auto cur = Dat.begin(); cur != Dat.end() - 1; cur++) // may bug
  {
    long start = *cur;
    long tmp = *(cur + 1);
    for (auto curr = cur + 2; curr != Dat.end(); curr++) {
      if (*curr > tmp)
        tmp = *curr;
    }
    if (tmp - *cur > max)
      max = tmp - *cur;
  }
  std::cout << max << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

int main() {
  std::vector<long> Dat = {};
  int t = 0;
  std::cin >> t;
  for (int i = 0; i < t; i++) {
    int tmp = 0;
    std::cin >> tmp;
    Dat.push_back(tmp);
  }
  long min = *Dat.begin(), max = -2E9;
  for (auto cur = Dat.begin() + 1; cur != Dat.end(); cur++) {
    if (*(cur - 1) < min)
      min = *(cur - 1);
    if (*cur - min > max)
      max = *cur - min;
  }
  std::cout << max << std::endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long n, a[1000000], x, y, mi, an;
int main() {
  cin >> n;
  for (x = 0; x < n; x++) {
    cin >> a[x];
    for (y = x - 1; y >= 0; y--) {
      if (x == 1) {
        mi = a[x];
      }
      if (a[x] < mi) {
        mi = a[x];
      }
      if (x == 1 && y == 0) {
        an = a[x] - a[y];
      }
      if (an < a[x] - a[y]) {
        an = a[x] - a[y];
      }
    }
  }
  cout << an << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, a[1000000], x, y, mi, an;
int main() {
  cin >> n;
  for (x = 0; x < n; x++) {
    cin >> a[x];
    if (x == 1) {
      an = a[x] - a[x - 1];
    }
    if (x == 0) {
      mi = a[x];
    }
    if (an < a[x] - mi) {
      an = a[x] - mi;
    }
    if (mi > a[x]) {
      mi = a[x];
    }
  }
  cout << an << endl;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int *r;
  r = new int[n];
  if (r == NULL) {
    cout << "Error!" << endl;
    return -1;
  }
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> r[i];
  }
  int max = r[1] - r[0];
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (r[j] - r[i] > max)
        max = r[j] - r[i];
    }
  }
  cout << max << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int *r;
  r = new int[n];
  if (r == NULL) {
    cout << "Error!" << endl;
    return -1;
  }
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> r[i];
  }
  int max = r[1] - r[0];
  for (j = n - 1; j >= 0; j--) {
    if (r[j] <= max)
      continue;
    for (i = j - 1; i >= 0; i--) {
      if (r[j] - r[i] > max)
        max = r[j] - r[i];
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<double> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  double max_profit = -1000000000;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      max_profit = max(max_profit, r[j] - r[i]);
    }
  }
  cout << setprecision(10) << max_profit << endl;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<double> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  double max_profit = -1000000000;
  double min_r = r[0];
  for (int i = 1; i < n; i++) {
    min_r = min(min_r, r[i - 1]);
    max_profit = max(max_profit, r[i] - min_r);
  }
  cout << setprecision(10) << max_profit << endl;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> rs(n);
  for (auto &r : rs) {
    cin >> r;
  }

  int ans = INT_MIN;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = max(ans, rs[j] - rs[i]);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> rs(n);
  for (auto &r : rs) {
    cin >> r;
  }

  int ans = INT_MIN;
  int min_r = rs[0];
  for (int i = 1; i < n; i++) {
    ans = max(ans, rs[i] - min_r);
    min_r = min(min_r, rs[i]);
  }
  cout << ans << endl;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

class MaximumProfit {
public:
  int solve(int n, int *r);
};

int MaximumProfit::solve(int n, int *r) {
  int maxp = -2000000000;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++) {
      if ((r[j] - r[i]) > maxp)
        maxp = r[j] - r[i];
    }

  return maxp;
}

int main(void) {

  MaximumProfit *mp = new MaximumProfit();

  int n;

  cin >> n;

  int *r = new int[n];

  for (int i = 0; i < n; i++)
    cin >> r[i];

  cout << mp->solve(n, r) << endl;

  delete mp;
  delete[] r;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

class MaximumProfit {
public:
  int solve(int n, int *r);
};

int MaximumProfit::solve(int n, int *r) {
  int maxp = -2000000000;
  int minr = r[0];
  for (int i = 1; i < n; i++) {
    if ((r[i] - minr) > maxp)
      maxp = r[i] - minr;
    if (r[i] < minr)
      minr = r[i];
  }

  return maxp;
}

int main(void) {

  MaximumProfit *mp = new MaximumProfit();

  int n;

  cin >> n;

  int *r = new int[n];

  for (int i = 0; i < n; i++)
    cin >> r[i];

  cout << mp->solve(n, r) << endl;

  delete mp;
  delete[] r;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int m[200000];
  for (int i = 0; i < n; i++) {
    cin >> m[i];
  }
  int max = m[1] - m[0];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (max < m[j] - m[i])
        max = m[j] - m[i];
    }
  }
  cout << max << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int m[200000];
  for (int i = 0; i < n; i++) {
    cin >> m[i];
  }
  int max = -1000000000, min = m[0];
  for (int i = 1; i < n; i++) {
    if (max < m[i] - min)
      max = m[i] - min;
    if (m[i] < min)
      min = m[i];
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200001] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int max = a[1] - a[0];
  for (int i = 1; i < n; i++)
    for (int j = 0; j < i; j++)
      if (a[i] - a[j] > max)
        max = a[i] - a[j];
  cout << max << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200001] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int max = a[1] - a[0];
  int min = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] - min > max)
      max = a[i] - min;
    if (min > a[i])
      min = a[i];
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, tmp;
  cin >> n;
  vector<int> rt(n);
  for (int i = 0; i < n; i++) {
    cin >> rt[i];
  }
  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i < j) {
        tmp = rt[j] - rt[i];
        if (ans < tmp)
          ans = tmp;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, tmp;
  cin >> n;
  vector<int> rt(n);
  for (int i = 0; i < n; i++) {
    cin >> rt[i];
  }
  int ans = INT_MIN;
  int minv = rt[0];
  for (int j = 1; j < n; j++) {
    ans = (ans >= rt[j] - minv) ? ans : rt[j] - minv;
    minv = (minv <= rt[j]) ? minv : rt[j];
  }
  cout << ans << endl;

  return 0;
}
Problem p02258 (Python)

Buggy submission (status: Runtime Error):
import sys

values = [int(input()) for i in range(int(input()))]
maxv = -sys.maxsize - 1
minv = values[0]

for val in enumerate(values):
    if maxv < (val - minv):
        maxv = val - minv
    if minv > val:
        minv = val
print(maxv)


Fixed submission:
n = int(input())
R = [int(input()) for _ in range(n)]
minv, maxv = R[0], R[1] - R[0]
for j in range(1, n):
    maxv = max(maxv, R[j] - minv)
    minv = min(minv, R[j])
print(maxv)
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main(void) {
  int i = 0;
  int n = 0;
  int min;
  int now;
  int max_margin = -2147483648;
  int margin;

  scanf("%d", &n);
  scanf("%d", &min);
  for (i = 1; i < n; i++) {
    scanf("%d", now);
    margin = now - min;
    if (margin > max_margin) {
      max_margin = margin;
    }
    if (now < min) {
      min = now;
    }
  }
  printf("%d\n", max_margin);

  return 0;
}

Fixed submission:
#include <stdio.h>

int main(void) {
  int i = 0;
  int n = 0;
  int min;
  int now;
  int max_margin = -2147483648;
  int margin;

  scanf("%d", &n);
  scanf("%d", &min);
  for (i = 1; i < n; i++) {
    scanf("%d", &now);
    margin = now - min;
    if (margin > max_margin) {
      max_margin = margin;
    }
    if (now < min) {
      min = now;
    }
  }
  printf("%d\n", max_margin);

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int timeSerialDiff(vector<int> &v) {
  int size = v.size(), i, j, max, cnt = 0;
  for (i = 0; i < size; i++) {
    int start = v[i];
    for (j = i + 1; j < size; j++) {
      int diff = v[j] - v[i];
      max = (cnt == 0 || max < diff) ? diff : max;
      cnt++;
    }
  }
  return max;
}
int main() {
  int n, i, x, min, max;
  vector<int> v;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  cout << timeSerialDiff(v) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int timeSerialDiff(vector<int> &v) {
  int size = v.size();
  if (size < 2) {
    return 0;
  }
  int i, max, min, cnt = 0;
  min = v[0];
  max = v[1] - v[0];
  for (i = 1; i < size; i++) {
    int diff = v[i] - min;
    max = max < diff ? diff : max;
    min = min > v[i] ? v[i] : min;
  }
  return max;
}
int main() {
  int n, i, x, min, max;
  vector<int> v;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  cout << timeSerialDiff(v) << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> r;
  for (int i = 0; i < n; i++) {
    int ri;
    cin >> ri;
    r.push_back(ri);
  }

  int diffmax = r[1] - r[0];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      diffmax = max(diffmax, r[j] - r[i]);
    }
  }

  cout << diffmax << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> r;
  for (int i = 0; i < n; i++) {
    int ri;
    cin >> ri;
    r.push_back(ri);
  }

  int diffmax = r[1] - r[0];
  int rmin = r[0];
  for (int i = 1; i < n; i++) {
    diffmax = max(diffmax, r[i] - rmin);
    rmin = min(rmin, r[i]);
  }

  cout << diffmax << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define pout(n) printf("%d\n", n)

#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, n, a) for (int i = n - 1; i >= a; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());

const int d4x[4] = {1, 0, -1, 0};
const int d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

#define MAX_N (int)2e5 + 20
#define INF 1e12 + 20

typedef long long ll;
using namespace std;

int n;
int main() {
  cin >> n;
  int r[n];
  rep(i, 0, n) { cin >> r[i]; }
  int ans = INT_MIN;
  rep(i, 0, n - 1) {
    rep(j, i + 1, n) { ans = max(r[j] - r[i], ans); }
  }
  pout(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define pout(n) printf("%d\n", n)

#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, n, a) for (int i = n - 1; i >= a; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());

const int d4x[4] = {1, 0, -1, 0};
const int d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

#define MAX_N (int)2e5 + 20
#define INF 1e12 + 20

typedef long long ll;
using namespace std;

int n;
int main() {
  cin >> n;
  int r[n];
  rep(i, 0, n) { cin >> r[i]; }
  int ans = INT_MIN;
  int mi = r[0];
  rep(i, 1, n) {
    ans = max(ans, r[i] - mi);
    mi = min(mi, r[i]);
  }
  pout(ans);
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
const int NMAX = 20000;

int maximum_profit(int *r, int n) {
  int mini = r[0];
  int maxi = r[1] - r[0];
  for (int i = 1; i < n; i++) {
    maxi = max(maxi, r[i] - mini);
    mini = min(mini, r[i]);
  }

  return maxi;
}

int main(void) {
  int *r;
  r = new int[NMAX];

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
    // cout << r[i] << endl;
  }

  int max = maximum_profit(r, n);
  cout << max << endl;

  delete r;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
const int NMAX = 2000000;

int maximum_profit(int *r, int n) {
  int mini = r[0];
  int maxi = r[1] - r[0];
  for (int i = 1; i < n; i++) {
    maxi = max(maxi, r[i] - mini);
    mini = min(mini, r[i]);
  }

  return maxi;
}

int main(void) {
  int *r;
  r = new int[NMAX];

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
    // cout << r[i] << endl;
  }

  int max = maximum_profit(r, n);
  cout << max << endl;

  delete r;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int m = (1 << 31);
  for (int i = 0; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      m = max(a[i] - a[j], m);
    }
  }
  cout << m << endl;
  delete[] a;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int m = (1 << 31);
  int minx = a[0];
  for (int i = 1; i < n; i++) {
    m = max(m, a[i] - minx);
    minx = min(minx, a[i]);
  }
  cout << m << endl;
  delete[] a;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int functione(int num[], int N);

int main() {
  int n;
  cin >> n;
  int *num = new int[n];

  for (int i = 0; i < n; i++)
    cin >> num[i];

  cout << functione(num, n) << endl;
  return 0;
}

int functione(int num[], int N) {
  int profit = -2000000000;
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++) {
      if (profit < num[j] - num[i])
        profit = num[j] - num[i];
    }
  return profit;
}

Fixed submission:
#include <iostream>
using namespace std;

int functione(int num[], int N);

int main() {
  int n;
  cin >> n;
  int *num = new int[n];

  for (int i = 0; i < n; i++)
    cin >> num[i];

  cout << functione(num, n) << endl;
  return 0;
}

int functione(int num[], int N) {
  int max = -2000000000;
  int min = num[0];
  for (int i = 1; i < N; i++) {
    if (max < num[i] - min)
      max = num[i] - min;
    if (min > num[i])
      min = num[i];
  }
  return max;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  int n;
  cin >> n;

  vector<int> prices(n);
  for (int i = 0; i < n; i++) {
    cin >> prices[i];
  }

  int mprofit = -1000000000;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      mprofit = max(mprofit, prices[j] - prices[i]);
    }
  }

  cout << mprofit << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  int n;
  cin >> n;

  vector<int> prices(n);
  for (int i = 0; i < n; i++) {
    cin >> prices[i];
  }

  int mprofit = -1111111111;
  int minv = prices[0];
  for (int i = 1; i < n; i++) {
    mprofit = max(mprofit, prices[i] - minv);
    minv = min(minv, prices[i]);
  }

  cout << mprofit << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int n;
  cin >> n;
  vector<int> r(n);
  for (int i = 0; i < n; ++i) {
    cin >> r[i];
  }
  int ans = -1000000007;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ans = max(ans, r[j] - r[i]);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int n;
  cin >> n;
  vector<int> r(n);
  for (int i = 0; i < n; ++i) {
    cin >> r[i];
  }
  int ans = -1000000007;
  int minv = r[0];
  for (int i = 1; i < n; ++i) {
    ans = max(ans, r[i] - minv);
    minv = min(minv, r[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 20000;

int main() {
  int n;
  int minv;
  int maxv = -2000000000;
  int R[MAX];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  minv = R[0];
  for (int j = 1; j < n; j++) {
    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }
  cout << maxv << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int n;
  int minv;
  int maxv = -2000000000;
  int R[MAX];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  minv = R[0];
  for (int j = 1; j < n; j++) {
    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }
  cout << maxv << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX_VALUE = 200000;
int R[MAX_VALUE];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &R[i]);

  int max_income = -1000000001;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
      max_income = max(max_income, R[j] - R[i]);

  printf("%d\n", max_income);
}

Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX_VALUE = 200000;
int R[MAX_VALUE];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &R[i]);

  int min_val = R[0];
  int max_val = -1000000001;
  for (int j = 1; j < n; j++) {
    max_val = max(max_val, R[j] - min_val);
    min_val = min(min_val, R[j]);
  }
  printf("%d\n", max_val);
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <limits.h>
#include <stdio.h>

int main(void) {
  int n, i, tmp, tmp2;
  int profit = INT_MIN;
  int min = INT_MAX;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &tmp);
    tmp2 = tmp - min;
    if (tmp2 > profit) {
      profit = tmp2;
    }
    if (tmp < min)
      min = tmp;
  }
  return profit;
}


Fixed submission:
#include <limits.h>
#include <stdio.h>

int main(void) {
  int n, i, tmp, tmp2;
  int profit = INT_MIN;
  int min = INT_MAX;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &tmp);
    tmp2 = tmp - min;
    if (tmp2 > profit) {
      profit = tmp2;
    }
    if (tmp < min)
      min = tmp;
  }
  printf("%d\n", profit);
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define SORT(c) sort((c).begin(), (c).end())
const double EPS = 1e-10;                 // sample:if((double)>=(double)+EPS)
#define CLR(a) memset((&a), 0, sizeof(a)) // clear memory
#define dump(x) cerr << #x << " = " << (x) << endl; // debug
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
// cin,cout release
// std::cin.tie(0);
// ios::sync_with_stdio(false);

int main(void) {
  int n;
  cin >> n;
  // vector<int> r;
  int r[200000];
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  int maxv = INT_MIN;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      maxv = max(maxv, r[j] - r[i]);
    }
  }
  cout << maxv << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define SORT(c) sort((c).begin(), (c).end())
const double EPS = 1e-10;                 // sample:if((double)>=(double)+EPS)
#define CLR(a) memset((&a), 0, sizeof(a)) // clear memory
#define dump(x) cerr << #x << " = " << (x) << endl; // debug
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
// cin,cout release
// std::cin.tie(0);
// ios::sync_with_stdio(false);

int main(void) {
  int n;
  cin >> n;
  // vector<int> r;
  int r[200000];
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  int maxv = INT_MIN, minv = r[0];
  for (int i = 1; i < n; i++) {
    maxv = max(maxv, r[i] - minv);
    minv = min(minv, r[i]);
  }
  cout << maxv << endl;

  return 0;
}