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

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

  cout << max << endl;
}

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];

  long int max = -1000000000;

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

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main() {
  int count;
  std::vector<int> X;
  cin >> count;
  for (int i = 0; i < count; ++i) {
    int x;
    cin >> x;
    X.push_back(x);
  }
  int minX = INT_MAX;
  int maxProfit = INT_MIN;
  for (int j = 1; j < X.size(); ++j) {
    minX = min(minX, X[j - 1]);
    maxProfit = max(X[j] - minX, maxProfit);
  }
  cout << maxProfit << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main() {
  int count;
  std::vector<int> X;
  cin >> count;
  for (int i = 0; i < count; ++i) {
    int x;
    cin >> x;
    X.push_back(x);
  }
  int d = INT_MIN;
  for (int j = 1; j < X.size(); ++j) {
    for (int i = 0; i < j; ++i) {
      d = max(X[j] - X[i], d);
    }
  }
  cout << d << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

// 2???n???200,000
// 1???Rt???10

#include <iostream>
#include <limits>
#include <vector>

int main() {
  int N;
  std::vector<int> R;

  std::cin >> N;
  R.resize(N);

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

  std::vector<int> R_min(N);
  std::vector<int> R_max(N);

  R_min[0] = R[0];
  R_max[N - 1] = R[N - 1];

  for (int i = 1; i < N; ++i) {
    R_min[i] = std::min(R[i], R_min[i - 1]);
  }
  for (int i = N - 2; i >= 0; --i) {
    R_max[i] = std::max(R[i], R_max[i + 1]);
  }

  // for(int i=0; i<N; ++i){
  //   std::cout << R[i] << ' ';
  // }
  // std::cout << std::endl;
  // for(int i=0; i<N; ++i){
  //   std::cout << R_min[i] << ' ';
  // }
  // std::cout << std::endl;
  // for(int i=0; i<N; ++i){
  //   std::cout << R_max[i] << ' ';
  // }
  // std::cout << std::endl;

  int max_diff = std::numeric_limits<int>::min();
  // for(int i=0; i<N; ++i){
  //   max_diff = std::max(max_diff, R_max[i] - R_min[i]);
  // }
  for (int i = 0; i < N - 1; ++i) {
    max_diff = std::max(max_diff, R_max[i + 1] - R_min[i]);
  }

  std::cout << max_diff << std::endl;
}

Error: -11

Fixed submission:

// 2???n???200,000
// 1???Rt???10

#include <iostream>
#include <limits>
#include <vector>

int main() {
  int N;
  std::vector<int> R;

  std::cin >> N;
  R.resize(N);

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

  std::vector<int> R_min;
  std::vector<int> R_max;

  R_min[0] = R[0];
  R_max[N - 1] = R[N - 1];

  for (int i = 1; i < N; ++i) {
    R_min[i] = std::min(R[i], R[i - 1]);
  }
  for (int i = N - 2; i >= 0; --i) {
    R_max[i] = std::max(R[i], R[i + 1]);
  }

  int max_diff = std::numeric_limits<int>::min();
  for (int i = 0; i < N; ++i) {
    max_diff = std::max(max_diff, R_max[i] - R_min[i]);
  }

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

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

/*
??????
*/

int main() {
  int n;
  std::vector<int> price;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d", &b);
    price.push_back(b);
  }

  int pmin = std::numeric_limits<int>::max();
  int maxprofit = std::numeric_limits<int>::min();
  for (auto p = price.begin(); p != price.end(); p++) {
    maxprofit = std::max(maxprofit, *p - pmin);
    pmin = std::min(pmin, *p);
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int n;
  scanf("%d", &n);
  std::vector<int> d;

  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d", &b);
    d.push_back(b);
  }

  int profit = std::numeric_limits<int>::min();
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (profit < d[j] - d[i])
        profit = d[j] - d[i];
    }
  }
  printf("%d\\n", profit);
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int nmin = (1 << 31) - 1;
  int ans = 1 << 31;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int dummy;
    cin >> dummy;
    ans = max(ans, dummy - nmin);
    nmin = min(nmin, dummy);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  long long n, num[200000];
  long long temp, nmax = -1000000000, nmin;

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

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

  cout << nmax << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long n, num[200000];
  long long temp, max = -1000000000;

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

  for (int i = 0; i < n - 1; i++) {
    temp = num[i + 1];
    for (int j = i + 2; j < n; j++) {
      if (temp < num[j]) {
        temp = num[j];
      }
    }

    if (temp - num[i] > max) {
      max = temp - num[i];
    }
  }

  cout << max << endl;

  return 0;
}
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> r(n);

  int buy;
  int profit;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
    if (i == 1) {
      profit = r[1] - r[0];
    } else if (i > 1) {
      profit = max(profit, r[i] - buy);
    }
    if (i == 0) {
      buy = r[i];
    } else if (buy > r[i]) {
      buy = r[i];
    }
  }

  cout << profit << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main(void) {
  while (1) {
    int n;
    cin >> n;
    vector<int> r(n);

    int sell, buy;
    int profit;
    for (int i = 0; i < n; i++) {
      cin >> r[i];
      if (i == 1) {
        profit = r[1] - r[0];
      } else if (i > 1) {
        profit = max(profit, r[i] - buy);
      }
      if (i == 0) {
        buy = r[i];
      } else if (buy > r[i]) {
        buy = r[i];
      }
    }

    cout << profit << 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], 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;
}

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

#include <stdio.h>

#define N 200000

int main() {
  int n, i, j, maxi, maxj;
  scanf("%d", &n);
  int num[N];
  for (i = 0; i < n; i++)
    scanf("%d", &num[i]);
  int max = num[1] - num[0];
  maxj = num[1];
  maxi = num[0];
  for (i = 0; i <= n - 2; i++) {
    if (num[i] > maxi)
      continue;
    for (j = i + 1; j <= n - 1; j++) {
      int diff = num[j] - num[i];
      if (diff > max) {
        max = diff;
        maxi = num[i];
        maxj = num[j];
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define N 200000

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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int N = 200000;
int r[N];
int n;

int main() {
  int i, j;
  scanf("%d ", &n);
  for (i = 0; i < n; i++)
    scanf("%d ", r + i);

  /*
    //O(n^2):TLE
    int res = r[1] - r[0];
    for (i=0; i<n-1; i++)
      for (j=i+1; j<n; j++) {
        res = (res < r[j] - r[i]) ? r[j] - r[i] : res;
      }
    printf("%d\\n", res);
  */

  /*
    //WA(miniが余計に更新されてmaxj-miniがおかしなことになる)
    int maxj = r[1];
    int mini = r[0];
    for (j=1; j<n; j++) {
      if (maxj-mini < r[j]-mini) {
        maxj = r[j];
        if (j < n-1 && r[j] < mini) mini = r[j];
      }
  //    if (j < n-1 && r[j] < mini) mini = r[j];
    }
    printf("%d\\n", maxj-mini);
  */

  // resにmax-minを持たせる(上のようにminiの更新で変なことにならない)
  int res = r[1] - r[0];
  int mini = r[0];
  for (j = 1; j < n; j++) {
    if (res < r[j] - mini)
      res = r[j] - mini;
    if (r[j] < mini)
      mini = r[j];
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int N = 200000;
int r[N];
int n;

int main() {
  int i, j;
  scanf("%d ", &n);
  for (i = 0; i < n; i++)
    scanf("%d ", r + i);

  // O(n^2):TLE
  int res = r[1] - r[0];
  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++) {
      res = (res < r[j] - r[i]) ? r[j] - r[i] : res;
    }

  /*
    int maxj = r[1];
    int mini = r[0];
    for (j=1; j<n; j++) {
      if (maxj-mini < r[j]-mini) maxj = r[j];
      if (r[j] < mini) mini = r[j];
    }
  */

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

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

#include <iostream>
using namespace std;

static const int Len = 200000;
int a[Len];

int main() {

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

  int maxV = -1000000001; // ????????????????°?????¶????
  int minV = a[0];        // ????¬¬????????°??????????°????

  for (int i = 1; i < n; i++) {
    maxV = max(maxV, a[i] - minV); // ??´??°?????§???
    minV = min(minV, a[i]);        // ??´??°??????????°????
  }

  cout << maxV << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

static const int Len = 200000;
int a[Len];

int main() {

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

  int maxV = -1000000001;
  for (int j = 1; j < n; j++) {
    for (int i = 0; i < j; i++) {
      maxV = maxV >= (a[j] - a[i]) ? maxV : a[j] - a[i];
    }
  }
  cout << maxV << endl;

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

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

int main() {
  long n;
  cin >> n;
  // n = 6;
  long max = -1000000000;
  vector<long> R(n);
  for (long i = 0; i < n; i++) {
    cin >> R[i];
  }
  long Rmin = R[0];
  /*
  R[0] = 5;
  R[1] = 3;
  R[2] = 1;
  R[3] = 3;
  R[4] = 4;
  R[5] = 3;
  */
  for (long i = 1; i < n; i++) {
    if (R[i] - Rmin > max)
      max = R[i] - Rmin;
    if (R[i] < Rmin)
      Rmin = R[i];
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long n;
  cin >> n;
  // n = 3;
  long max = -1000000000;
  vector<long> R(n);
  for (long i = 0; i < n; i++) {
    cin >> R[i];
  }
  /*
  R[0] = 4;
  R[1] = 3;
  R[2] = 2;
  */
  for (long i = 0; i < n; i++) {
    for (long j = i + 1; j < n; j++) {
      if (R[j] - R[i] > max) {
        max = R[j] - R[i];
      }
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main(void) {
  // Here your code !
  int n; // ???????????°
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int min, ans;
  min = a[0];
  ans = a[1] - a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] < min) {
      min = a[i];
    } else if (ans < a[i] - min) {
      ans = a[i] - min;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  // Here your code !
  int n, key, p = 0, q = 0;
  bool l = true;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  key = a[1] - a[0];
  for (int i = 0; i < n; i++) {
    if (a[i] < q) {
      l = false;
    }
    if (a[i] < p && a[i] > q && l) {
      continue;
    }
    for (int j = 0; j < i; j++) {
      if (a[i] - a[j] > key) {
        key = a[i] - a[j];
        p = a[i];
        q = a[j];
        l = true;
      }
    }
  }
  cout << key << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int main() {
  int n;
  int r[200000];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> r[i];
  }
  int s = r[1] - r[0];
  int rr = r[0];
  for (int i = 1; i < n; i++) {
    if (r[i] - rr > s) {
      s = r[i] - rr;
    }
    if (r[i] < rr) {
      rr = r[i];
    }
  }
  std::cout << s << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

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

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

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

  vector<int> v(n, 0);
  for (auto &&x : v) {
    cin >> x;
  }

  int ans = v[1] - v[0];

  int minv = v[0];

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

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int> v(n, 0);
  for (auto &&x : v) {
    cin >> x;
  }

  int ans = v[1] - v[0];

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

  cout << ans << endl;

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

Error: TLE

Fixed submission:

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

#include <iostream>
#include <limits>
#include <queue>

using namespace std;

int main() {
  int n, MAX = numeric_limits<int>::min();

  queue<int> R;

  cin >> n;

  for (int i = 0; i < n; i++) {
    int temp;

    cin >> temp;

    R.push(temp);

    if (MAX < R.back() - R.front() && R.size() > 1) {
      MAX = R.back() - R.front();
    }

    if (R.back() < R.front()) {
      while (R.size() > 1) {
        R.pop();
      }
    }
  }

  cout << MAX << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>

using namespace std;

unsigned N;
int MAX = INT_MIN;

int main() {
  cin >> N;

  vector<int> R(N);

  for (vector<int>::size_type i = 0; i < N; i++) {
    cin >> R[i];
  }

  for (vector<int>::size_type i = 0; i < N; i++) {
    for (vector<int>::size_type j = i + 1; j < N; j++) {
      if (R[j] - R[i] > MAX) {
        MAX = R[j] - R[i];
      }
    }
  }

  cout << MAX << endl;

  return 0;
}

// Time : O( N(N + 1) / 2 )
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int first;
  cin >> first;
  long long int min = first;
  n--;
  long long int max = -1000000000;
  long long int result;
  while (n--) {
    long long int x;
    cin >> x;
    result = x - min;
    if (x < min)
      min = x;
    // cout<<min<<endl;
    if (result > max)
      max = result;
  }
  cout << max << endl;
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int min = 1000000000 + 5;
  long long int max = -1;
  long long int result;
  while (n--) {
    long long int x;
    cin >> x;
    result = x - min;
    if (x < min)
      min = x;
    // cout<<min<<endl;
    if (result > max)
      max = result;
  }
  if (max == 0)
    cout << "-1" << endl;
  else
    cout << max << endl;
  while (1)
    ;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const int MAX = 200001;

int main() {
  int n;
  cin >> n;
  int R[MAX];

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

  int minv = R[0];
  int maxv = R[1] - 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;

const int MAX = 200001;

int main() {
  int n;
  cin >> n;
  int R[MAX];

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

  int minv = -10E10;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      minv = max(R[i] - R[j], minv);
    }
  }
  cout << minv << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 999999999
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

int main() {
  int n, maxv, minv;
  cin >> n;
  int R[n];
  REP(i, n) cin >> R[i];
  maxv = R[1] - R[0];
  minv = R[0];

  FOR(i, 1, n) {
    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;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 999999999
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

int main() {
  int n, maxv;
  cin >> n;
  int R[n];
  REP(i, n) cin >> R[i];
  maxv = R[1] - R[0];

  REP(i, n) {
    FOR(j, i + 1, n) {
      if (maxv < (R[j] - R[i]))
        maxv = R[j] - R[i];
    }
  }
  cout << maxv << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <limits.h>
#include <stdio.h>

#define MAX 200000

int main() {
  int n;
  long value[MAX];
  long prof;
  long min_prof = LONG_MAX;
  long max_prof = LONG_MIN;

  scanf("%d", &n);
  if (n < 2 || n > 200000) {
    printf("Error: n is out of range");
    return -1;
  }

  for (int i = 0; i < n; i++) {
    scanf("%ld", &value[i]);
    if (value[i] < 1 || value[i] > 10e9) {
      printf("Error: value[i] is out of range");
      return -1;
    }
  }

  for (int i = 0; i < n; i++) {
    // check max
    if ((max_prof < value[i] - min_prof) && i >= 1) {
      /* printf("## %ld, %ld", value[i], min_prof); */
      max_prof = value[i] - min_prof;
    }
    // check min
    if (value[i] < min_prof) {
      min_prof = value[i];
    }
    /* printf("min: %ld\\n", min_prof); */
    /* printf("max: %ld\\n", max_prof); */
  }
  printf("%ld\\n", max_prof);
}

Error: TLE

Fixed submission:

#include <limits.h>
#include <stdio.h>

#define MAX 200000

int main() {
  int n;
  long value[MAX];
  scanf("%d", &n);
  if (n < 2 || n > 200000) {
    printf("Error: n is out of range");
    return -1;
  }

  for (int i = 0; i < n; i++) {
    scanf("%ld", &value[i]);
    if (value[i] < 1 || value[i] > 10e9) {
      printf("Error: value[i] is out of range");
      return -1;
    }
  }

  /* for(int i = 0; i < n; i++) { */
  /*   printf("%d\\n", value[i]); */
  /* } */

  long max_prof = LONG_MIN;
  long prof;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      /* printf("i = %d, v[i] = %ld, j = %d, v[j] = %ld", i, value[i], j,
       * value[j]); */
      prof = value[i] - value[j];
      if (prof > max_prof) {
        max_prof = prof;
      }
    }
  }
  printf("%ld\\n", max_prof);
}