alexjercan/bugnet huggingface dataset:
Problem p02258 (C++) — buggy submission, verdict: 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];
  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;
}

Error: TLE

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];
  rep(i, n) rep(j, i) maxch(mx, a[i] - a[j]);
  cout << mx << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;

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

  pastMinimum = R[0];
  max = R[1] - R[0];

  for (i = 1; i < n; i++) {
    if (R[i - 1] < pastMinimum) {
      pastMinimum = R[i - 1];
    }
    if (R[i] - pastMinimum > max) {
      max = R[i] - pastMinimum;
    }
  }

  //  for(i=0; i<n-1; i++){
  //    for(j=i+1; j<n; j++){
  //      if(R[j]-R[i]>max){
  //        max = R[j] - R[i];
  //      }
  //    }
  //  }

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

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

  max = R[1] - R[0];

  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (R[j] - R[i] > max) {
        max = R[j] - R[i];
      }
    }
  }

  printf("%d\\n", max);
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
int main() {
  int input[2], keep, n, max;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      std::cin >> input[0];
      continue;
    }
    if (i == 1) {
      std::cin >> input[1];
      max = input[1] - input[0];
      continue;
    }
    std::cin >> keep;
    if (input[0] > input[1])
      input[0] = input[1];
    input[1] = keep;
    if (input[1] - input[0] > max)
      max = input[1] - input[0];
  }
  std::cout << max << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
int main() {
  int *input, i, j, n, max;
  scanf("%d", &n);
  input = (int *)malloc(sizeof(int) * n);
  for (i = 0; i < n; i++) {
    scanf("%d", &input[i]);
    if (i == 0)
      continue;
    if (i == 1) {
      max = input[1] - input[0];
      continue;
    }
    for (j = 0; j < i; j++) {
      if (input[i] - input[j] > max)
        max = input[i] - input[j];
    }
  }
  free(input);
  printf("%d\\n", max);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#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 j = 1; j < n; j++) {
    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }

  cout << maxv << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
static const int MAX = 20000;

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 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, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

  int r;
  int maxv = 0;
  int minv = 0;

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

    if (i == 0) {
      minv = r;
      continue;
    }

    if (maxv < r - minv or maxv == 0) {
      maxv = r - minv;
    }
    if (minv > r) {
      minv = r;
    }
  }

  cout << maxv << endl;

  return 0;
}


Error: TLE

Fixed submission:

#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 maxv = r[1] - r[0];
  for (int j = 1; j < n; j++) {
    for (int i = 0; i < j; i++) {
      int t = r[j] - r[i];
      if (t > maxv) {
        maxv = t;
      }
    }
  }

  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, t, i, dr;
  int mx_el = 0;

  cin >> n;
  int r[n];
  for (t = 0; t < n; t++) {
    cin >> r[t];
  }

  int mx = r[1] - r[0];

  for (t = n - 1; t > 1; t--) {
    if (r[t] > mx_el) {
      mx_el = r[t];
      for (i = 0; (i < t); i++) {
        dr = r[t] - r[i];
        if (dr > mx)
          mx = dr;
      }
    }
  }

  cout << mx << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, t, i, dr;

  cin >> n;
  int r[n];
  for (t = 0; t < n; t++) {
    cin >> r[t];
  }

  int mx = r[1] - r[0];

  for (t = n - 1; t > 1; t--) {
    for (i = 0; (i < t); i++) {
      dr = r[t] - r[i];
      if (dr > mx)
        mx = dr;
    }
  }

  cout << mx << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int maxProfit(vector<int> &array);

int main() {
  int size;
  vector<int> array;
  cin >> size;
  for (int i = 0; i < size; i++) {
    int tmp;
    cin >> tmp;
    array.push_back(tmp);
  }
  cout << maxProfit(array) << endl;
  return 0;
}

int maxProfit(vector<int> &array) {
  int min = array[1] > array[0] ? array[0] : array[1];
  int max_val = array[1] - array[0];
  for (int i = 2; i < array.size(); i++) {
    int profit = array[i] - min;
    if (profit > max_val) {
      max_val = profit;
    }
    if (min > array[i]) {
      min = array[i];
    }
  }
  return max_val;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int maxProfit(vector<int> &array);

int main() {
  int size;
  vector<int> array;
  cin >> size;
  for (int i = 0; i < size; i++) {
    int tmp;
    cin >> tmp;
    array.push_back(tmp);
  }
  cout << maxProfit(array) << endl;
  return 0;
}

int maxProfit(vector<int> &array) {
  int max_val = array[1] - array[0];
  for (int i = 0; i < array.size() - 1; i++) {
    for (int j = i + 1; j < array.size(); j++) {
      int now = array[j] - array[i];
      if (now > max_val) {
        max_val = now;
      }
    }
  }
  return max_val;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); i--)
#define STRS(i, str) for (int i = 0; (str)[i]; i++)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAXI(a, b)                                                             \\
  if (a < (b))                                                                 \\
  a = (b)
#define MINI(a, b)                                                             \\
  if (a > (b))                                                                 \\
  a = (b)

int main() {
  int n;
  int a;
  int min, max;
  scanf("%d%d%d", &n, &min, &a);
  max = a - min;
  MINI(min, a);
  FOR(i, 2, n) {
    scanf("%d", &a);
    MAXI(max, a - min);
    MINI(min, a);
  }
  printf("%d\\n", max);
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); i--)
#define chino(a, n)                                                            \\
  int n, *a;                                                                   \\
  scanf("%d", &n);                                                             \\
  a = (int *)malloc(sizeof(int) * n);                                          \\
  for (int i = 0; i < n; i++)                                                  \\
  scanf("%d", a + i)

int main() {
  chino(a, n);
  int ans = INT_MIN;
  REP(i, n) FOR(j, i + 1, n) if (a[j] - a[i] > ans) ans = a[j] - a[i];
  printf("%d\\n", ans);
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a[200005];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int maa = 0;
  int memo[200005];
  for (int i = n - 1; i != 0; i--) {
    maa = max(maa, a[i]);
    memo[i - 1] = maa;
  }
  int ans = -1000000000;
  for (int i = 0; i < n - 1; i++) {
    ans = max(memo[i] - a[i], ans);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a[200000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = -9999999999999;
  for (int i = 0; i < n; i++) {
    for (int s = i + 1; s < n; s++) {
      // if (a[s] <= a[i])continue;
      ans = max(a[s] - a[i], ans);
    }
  }
  cout << ans << endl;
}
Problem p02258 (C++) — buggy submission, verdict: 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);
  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;
}

Error: TLE

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);
  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;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>

int max(int *, int);

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

int max(int array[], int n) {
  int r = 0, i = 0, k, biggest = 1, smallest = 1000000000,
      champion = -1000000000, challenger;
  while (1) {
    i = r;
    while (i <= n - 1) {

      if (i == r) {
        biggest = 1;
        i = r + 1;
      }
      if (array[i] >= biggest) {
        biggest = array[i];
        r = i;
      }
      i++;
    }
    for (k = 0; k <= r - 1; k++) {
      if (array[k] <= smallest)
        smallest = array[k];
    }
    challenger = biggest - smallest;
    if (champion <= challenger)
      champion = challenger;
    if (r == n - 1)
      break;
  }
  return champion;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>

int max(int *, int);

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

int max(int array[], int n) {
  int start, target;
  int champion = -1000000000, challenger;
  for (start = 0; start <= n - 2; start++) {
    int biggest = 0;
    for (target = start + 1; target <= n - 1; target++) {
      if (1000000000 - array[start] <= champion)
        break;
      if (array[target] > biggest)
        biggest = array[target];
    }
    challenger = biggest - array[start];
    if (challenger > champion)
      champion = challenger;
  }
  return champion;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

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

  vector<int> inputs;
  for (int i = 0; i < cnt; i++) {
    int y;
    cin >> y;
    inputs.push_back(y);
  }

  int minv = inputs[0];
  int maxdiff = -1000000000;
  for (int i = 1; i < inputs.size(); i++) {
    maxdiff = max(maxdiff, inputs[i] - minv);
    minv = min(minv, inputs[i]);
  }

  cout << maxdiff << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

  vector<int> inputs;
  for (int i = 0; i < cnt; i++) {
    int y;
    cin >> y;
    inputs.push_back(y);
  }

  int max_diff = 0;
  bool has_diff = false;
  int diff = 0;
  int now = 0;
  int before = 0;
  for (int i = 0; i < inputs.size(); i++) {
    diff = 0;
    for (int j = i + 1; j < inputs.size(); j++) {
      now = inputs[j];
      before = inputs[i];
      diff = now - before;
      if (has_diff == false) {
        max_diff = diff;
        has_diff = true;
      } else if (diff > max_diff) {
        max_diff = diff;
      }
    }
  }

  cout << max_diff << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int r[200001], n, max, min;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  min = r[0];
  max = r[1] - r[0];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (min > r[i])
        min = r[i];
      if (r[j] - min > max) {
        max = r[j] - r[i];
      }
    }
  }
  cout << max << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  int num, max1 = -2000000000, min1 = 2000000000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    max1 = max(max1, (num - min1));
    min1 = min(min1, num);
  }
  cout << max1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  long long int num[200000], max1 = -1000000000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      max1 = max(max1, (num[i] - num[j]));
    }
  }
  cout << max1 << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, rmin = 1000000000, drmax = -1000000000, i, j;
  cin >> n;
  vector<int> r(n);
  cin >> r[0];
  rmin = r[0];
  for (i = 1; i < n; i++) {
    cin >> r[i];
    if (drmax < r[i] - rmin)
      drmax = r[i] - rmin;
    if (rmin > r[i])
      rmin = r[i];
  }
  cout << drmax << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m = -1000000000, i, j;
  vector<int> r;
  cin >> n;
  r = vector<int>(n);
  for (i = 0; i < n; i++) {
    cin >> r[i];
    for (j = i - 1; j >= 0; j--) {
      if (m < r[i] - r[j])
        m = r[i] - r[j];
    }
  }
  cout << m << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#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;
}


Error: 0

Fixed submission:

#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;
}
Problem p02258 (C++) — buggy submission, verdict: 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 = -1000000000;
  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;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int num;
  cin >> num;
  int b[200000];
  int x, y;
  cin >> x;
  for (int i = 0; i < num - 1; i++) {
    cin >> y;
    b[i] = y - x;
  }
  int accum = -1000000000;
  for (int i = 0; i < num - 1; i++) {
    for (int j = i; j < num - 1; j++) {
      if (b[j] > accum)
        accum = b[j];
    }
    for (int j = i; j < num - 1; j++) {
      b[j + 1] -= b[i];
    }
  }
  cout << accum << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#define MAX_DATA 200000
#define MIN_DATA 2

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

  int NUM;
  scanf("%d", &NUM);

  int *data;

  if (NUM >= MIN_DATA && NUM <= MAX_DATA) {
    data = (int *)malloc(sizeof(int) * NUM);
  } else {
    return 1;
  }

  int max = -1000000000;
  int min = 0;
  int temp;

  scanf("%d", &min);

  for (int i = 1; i < NUM; i++) {
    scanf("%d", &temp);

    int sub = temp - min;

    if (sub > max) {
      max = sub;
    }

    if (temp < min) {
      min = temp;
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define MAX_DATA 200000
#define MIN_DATA 2

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

  int NUM;
  scanf("%d", &NUM);

  int *data;

  if (NUM >= MIN_DATA && NUM <= MAX_DATA) {
    data = (int *)malloc(sizeof(int) * NUM);
  } else {
    return 1;
  }

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

  int max = data[1] - data[0];

  for (int i = 0; i < NUM; i++) {
    for (int j = i + 1; j < NUM; j++) {
      int temp = data[j] - data[i];

      if (temp > max)
        max = temp;
    }
  }

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

  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#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 = R[1] - R[0];
  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;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 20000;

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

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

  int maxv = R[1] - R[0];
  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, verdict: Time Limit Exceeded

#include <cassert>
#include <iostream>

using namespace std;

int main(int argc, char const *argv[]) {
  int count;
  cin >> count;
  int *nums = new int[count];
  for (int i = 0; i < count; i++) {
    cin >> nums[i];
  }

  assert(count >= 2);
  if (count < 2) {
    return 1;
  }

  int maxprofit = -nums[0];
  int maxprofit_buy = nums[0];
  int min_buy = nums[0];

  for (int i = 1; i < count; i++) {
    int profit = nums[i] - min_buy;
    if (maxprofit < profit) {
      maxprofit_buy = min_buy;
    }
    if (nums[i] < min_buy) {
      min_buy = nums[i];
    }
    profit = nums[i] - maxprofit_buy;
    if (maxprofit < profit) {
      maxprofit = profit;
    }
  }
  cout << maxprofit << endl;

  delete[] nums;
  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>

using namespace std;

int main(int argc, char const *argv[]) {
  int count;
  cin >> count;
  int *nums = new int[count];
  for (int i = 0; i < count; i++) {
    cin >> nums[i];
  }

  assert(count >= 2);
  if (count < 2) {
    return 1;
  }

  int maxprofit = nums[1] - nums[0];

  for (int i = 0; i < count - 1; i++) {
    for (int j = i + 1; j < count; j++) {
      int profit = nums[j] - nums[i];
      if (maxprofit < profit) {
        maxprofit = profit;
      }
    }
  }
  cout << maxprofit << endl;

  delete[] nums;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <vector>

template <typename T> T Input() {
  T value;
  std::cin >> value;
  return value;
}

int main() {
  std::ios::sync_with_stdio(false);
  int num, max, min;
  std::vector<int> arr(200000);
  std::cin >> num;
  for (auto i = 0; i < num; ++i)
    arr[i] = Input<int>();
  min = arr[0];
  max = INT_MIN;
  for (auto i = 1; i < num; ++i) {
    max = std::max(max, arr[i] - min);
    min = std::min(min, arr[i]);
  }

  std::cout << max << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
#include <vector>

template <typename T> T Input() {
  T value;
  std::cin >> value;
  return value;
}

int main() {
  int num, max = INT_MIN;
  std::vector<int> arr(200000);
  std::cin >> num;
  for (auto i = 0; i < num; ++i)
    arr[i] = Input<int>();
  for (auto i = 0; i < num; ++i) {
    for (auto j = num - 1; j > i; --j) {
      if (max < arr[j] - arr[i])
        max = arr[j] - arr[i];
    }
  }

  std::cout << max << "\\n";

  return 0;
}