alexjercan/bugnet huggingface dataset:
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int n;
  scanf("%d", &n);

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

  int minv = R[0];
  int max_gap = R[1] - R[0];

  for (int i = 1; i < n; i++) {
    if (max_gap < R[i] - minv) {
      max_gap = R[i] - minv;
    }
    if (R[i] < minv) {
      minv = R[i];
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int n;
  scanf("%d", &n);

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  vector<int> s(n + 1), m(n + 1, 0), a(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    m[i] = max(m[i + 1], s[i + 1]);
    a[i] = m[i] - s[i];
  }
  cout << *max_element(a.begin(), a.end() - 1) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < n - 1; i++) {
    s[i] = *max_element(s.begin() + i + 1, s.end()) - s[i];
  }
  cout << *max_element(s.begin(), s.end() - 1) << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, m, min, max = 1 - 1000000000;
  cin >> n;
  cin >> m;
  min = m;
  while (--n) {
    cin >> m;
    if (m - min > max)
      max = m - min;
    if (m < min)
      min = m;
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> a;
  int n, m, max;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    a.push_back(m);
  }
  max = a[1] - a[0];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[j] - a[i] > max)
        max = a[j] - a[i];
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cmath>
#include <iostream>

const int MAX_N = 200000;

int main() {
  int n = 0;
  int prices[MAX_N] = {0};

  std::cin >> n;

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

  int minv = prices[0];
  int maxv = INT_MIN;

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

  std::cout << maxv << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cmath>
#include <iostream>

const int MAX_N = 200000;

int main() {
  int n = 0;
  int prices[MAX_N] = {0};

  int maxProfit = INT_MIN;

  std::cin >> n;

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

    for (int j = i - 1; j >= 0; j--) {
      maxProfit = std::max(maxProfit, prices[i] - prices[j]);
    }
  }

  std::cout << maxProfit << std::endl;

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


Error: TLE

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;
  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;
}
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]; //???????????????
  int 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;
}

int max(int oldMax, int newMax) {
  int nowMax;

  nowMax = oldMax;
  if (newMax > oldMax) {
    nowMax = newMax;
  }

  return nowMax;
}

int min(int oldMin, int newMin) {
  int nowMin;

  nowMin = oldMin;
  if (newMin > oldMin) {
    nowMin = newMin;
  }

  return nowMin;
}

Error: 0

Fixed submission:

#include <stdio.h>

static const int Rt_MAX = 100000;

int main() {
  int n;          //????????°
  int Rt[Rt_MAX]; //??????
  int max;        //?????§??????
  int min;        //????°???????

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

  //?¨????
  max = Rt[1] - Rt[0];
  min = Rt[0];
  //+1??????????????????
  for (int i = 1; i < n; i++) {
    if (Rt[i] - min > max) {
      max = Rt[i] - min;
    }
    if (Rt[i] < min) {
      min = Rt[i];
    }
  }

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

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

int main() {
  int n;
  cin >> n;
  int data;
  int MAX = INT_MIN;
  int MIN = INT_MAX;
  for (int i = 0; i < n; i++) {
    cin >> data;
    MAX = max(MAX, data - MIN);
    MIN = min(MIN, data);
  }
  cout << MAX << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  int max = INT_MIN;
  for (auto &a : data) {
    cin >> a;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (data[j] - data[i] > max)
        max = data[j] - data[i];
    }
  }
  cout << max << endl;
}
Problem p02258 (C++) — buggy submission, verdict: 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 = -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;
}

Error: TLE

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

#include <bits/stdc++.h>

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

using namespace std;

const int inf = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  int min = INT_MAX;
  int max = -INT_MAX;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    if (max < tmp - min) {
      max = tmp - min;
    }
    if (min > tmp) {
      min = tmp;
    }
  }
  cout << max << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

const int inf = 1e9 + 7;

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

#include <stdio.h>

int main(void) {
  int n;
  long ans, min;
  scanf("%d", &n);
  long r[n];
  for (int i = 0; i < n; i++) {
    scanf("%ld", &r[i]);
  }
  ans = r[1] - r[0];
  min = r[0];
  for (int i = 1; i < n; i++) {
    if (min > r[i]) {
      min = r[i];
    } else {
      long temp = r[i] - min;
      if (ans < temp) {
        ans = temp;
      }
    }
  }
  printf("%ld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int n;
  int ans;
  scanf("%d", &n);
  int r[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);
  }
  ans = r[1] - r[0];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (ans < r[j] - r[i]) {
        ans = r[j] - r[i];
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, result, m;

  cin >> n;

  int num[n];

  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  m = num[0];
  result = num[1] - num[0];

  for (int i = 1; i < n; i++) {
    result = max(result, num[i] - m);
    m = min(m, num[i]);
  }

  cout << result << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, result = 0, f_result = 0;

  cin >> n;

  int num[n];

  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      result = num[j] - num[i];
      if (i == 0 && j == 1) {
        f_result = result;
      } else if (result > f_result) {
        f_result = result;
      }
    }
  }

  cout << f_result << "\\n";

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

#include <stdio.h>

int main(void) {
  int r[200000], n, min, p = -999999999, i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &r[i]);
  }

  for (i = 0; i < n; i++) {
    if (i > 0) {
      if (r[i] - min > p) {
        p = r[i] - min;
      }
      if (r[i] < min) {
        min = r[i];
      }
    } else {
      min = r[i];
    }
  }

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

  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>

int main(void) {
  int r[200000], n, min, max, i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", r[i]);
  }

  for (i = 0; i < n; i++) {
    if (i > 0) {
      if (r[i] > max) {
        max = r[i];
      } else if (r[i] < min) {
        min = r[i];
      }
    } else {
      max = r[i];
      min = r[i];
    }
  }

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

  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 n, num_list[MAX];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> num_list[i];
  int maxv, minv;
  maxv = num_list[1] - num_list[0];
  minv = num_list[0];
  for (int i = 1; i < n; i++) {
    maxv = max(maxv, num_list[i] - minv);
    minv = min(minv, num_list[i]);
  }

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

Error: TLE

Fixed submission:

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

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

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

int main() {
  int n;
  cin >> n;
  int R[n];
  for (int t = 0; t < n; t++) {
    cin >> R[t];
  }
  int maxv = R[1] - R[0];
  int minv = R[0];
  for (int j = 1; j < n; j++) {
    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }
  printf("%d\\n", maxv);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define MAX 200000

int main() {
  long n, i, maxP = -20000000000;
  long a[MAX];
  long x, k;
  scanf("%ld", &n);
  for (i = 0; i < n; i++)
    scanf("%ld", &a[i]);
  for (i = 1; i < n; i++) {
    for (k = 0; k < i; k++) {
      x = a[i] - a[k];
      if (x > maxP)
        maxP = x;
    }
  }
  printf("%ld\\n", maxP);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int dataset;
  cin >> dataset;
  int miin[200000];
  int ma = -2047483647;
  int a[200000];
  for (size_t i = 0; i < dataset; i++) {
    cin >> a[i];
  }
  miin[0] = a[0];
  for (size_t i = 1; i < dataset - 1; i++) {
    miin[i] = min(miin[i - 1], a[i]);
  }
  for (size_t i = 1; i < dataset; i++) {
    ma = max(ma, a[i] - miin[i - 1]);
  }
  cout << ma << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int dataset;
  cin >> dataset;
  int miin[20000];
  int ma = -2047483647;
  int a[20000];
  for (size_t i = 0; i < dataset; i++) {
    cin >> a[i];
  }
  miin[0] = a[0];
  for (size_t i = 1; i < dataset - 1; i++) {
    miin[i] = min(miin[i - 1], a[i]);
  }
  for (size_t i = 1; i < dataset; i++) {
    ma = max(ma, a[i] - miin[i - 1]);
  }
  cout << ma << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

#include <stdio.h>
int main() {
  long long int r[200001] = {0}, i, j, n, max = -1000000000, c, prev;
  prev = 1000000001;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &r[i]);
  }
  for (i = 0; i < n; i++) {
    if (prev <= r[i])
      continue;
    for (j = i + 1; j < n; j++) {
      c = r[j] - r[i];
      if (c > max) {
        max = c;
      }
    }
    prev = r[i];
  }
  printf("%lld\\n", max);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

#include <stdio.h>
int main() {
  long long int r[200001] = {0}, i, j, n, max = -1000000000, c;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &r[i]);
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      c = r[j] - r[i];
      if (c > max) {
        max = c;
      }
    }
  }
  printf("%lld\\n", max);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(asd, fgh) for (int asd = 0; asd < fgh; asd++)
#define MAX_N 200000

int main(void) {
  int n;
  int a[MAX_N];
  int m = INT_MAX;
  int ans = INT_MIN;
  cin >> n;
  REP(i, n) { cin >> a[i]; }

  for (int i = 1; i < n; i++) {
    m = min(m, a[i - 1]);
    ans = max(ans, a[i] - m);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(asd, fgh) for (int asd = 0; asd < fgh; asd++)

int main(void) {
  int n;
  int r[200000];
  int res = INT_MIN;
  cin >> n;
  REP(i, n) { cin >> r[i]; }

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

  cout << res << endl;

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

Error: TLE

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

#include <bits/stdc++.h>
using namespace std;
const int inf = 2100000000;
int a[200000];
int main() {
  int n, mi = inf, ans = -inf;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] - mi > ans) {
      ans = a[i] - mi;
    }
    if (mi > a[i]) {
      mi = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200000];
int main() {
  int n, c = -1000000000, b = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (b < a[j]) {
        b = a[j];
      }
      if (b > a[j]) {
        goto loop;
      }
      if (c < a[j] - a[i]) {
        c = a[j] - a[i];
      }
    loop:;
    }
    b = 0;
  }
  cout << c << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;
const int MAXN = 200000;

int R[MAXN];

int main() {
  int n;

  cin >> n;
  rep(i, n) cin >> R[i];

  int maxv = -1e9;
  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 <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;
const int MAXN = 200000;

int R[MAXN], mR[MAXN];

int main() {
  int n;
  int ans = -1e9;

  cin >> n;
  rep(i, n) cin >> R[i];

  for (int i = 0; i < n - 1; i++) {
    int m = 0;
    for (int j = i + 1; j < n; j++) {
      if (R[j] > m)
        m = R[j];
    }
    mR[i] = m;
  }
  rep(i, n - 1) {
    if (mR[i] - R[i] > ans)
      ans = mR[i] - R[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int MaximumProfit(vector<int> A, int n) {
  int min = A[0];
  int max = -1000000000;
  for (int i = 1; i < n; i++) {
    max = std::max(max, A[i] - min);
    min = std::min(A[i], min);
  }
  return max;
}

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

  vector<int> A;
  A.resize(n);

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

  cout << MaximumProfit(A, n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int MaximumProfit(vector<int> A, int n) {
  int max = -1000000000;
  for (int i = n - 1; i > 0; i--) {
    for (int j = 1; j <= i; j++) {
      if (max < A[i] - A[i - j])
        max = A[i] - A[i - j];
    }
  }
  return max;
}

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

  vector<int> A;
  A.resize(n);

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

  cout << MaximumProfit(A, n) << endl;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;

/**
 * vector<int>ar(3);
 * for(auto&e:ar){
 *     scanf("%d",&e);
 * }
 * sort(ar.begin(),ar.end())
 * int sum=accumulate(ar.begin(),ar.end(),0);
 ***/

int main() {
  double pai = 3.141592653589;
  int n, memo = 1000000001, r[200000], ans = -1000000001;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    if (memo < r[i]) {
      continue;
    } else {
      memo = r[i];
    }
    for (int j = i + 1; j < n; j++) {
      if (r[j] - r[i] > ans) {
        ans = r[j] - r[i];
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;

/**
 * vector<int>ar(3);
 * for(auto&e:ar){
 *     scanf("%d",&e);
 * }
 * sort(ar.begin(),ar.end())
 * int sum=accumulate(ar.begin(),ar.end(),0);
 ***/

int main() {
  double pai = 3.141592653589;
  int n, r[200000], ans = -1000000001;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (r[j] - r[i] > ans) {
        ans = r[j] - r[i];
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}