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 = new int[n];

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

  int maxv = R[1] - R[0], minv = R[0];
  for (int j = 1; j <= n - 1; j++) {
    maxv = maxv > (R[j] - minv) ? maxv : R[j] - minv;
    minv = minv < R[j] ? minv : R[j];
  }

  cout << maxv << endl;

  delete[] R;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  int *R = new int[n];

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

  int maxv = R[1] - R[0];
  for (int j = 1; j <= n - 1; j++) {
    for (int i = 0; i <= j - 1; i++)
      maxv = maxv > (R[j] - R[i]) ? maxv : R[j] - R[i];
  }

  cout << maxv << endl;

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

#include <iostream>
using namespace std;
int max(int, int);
int min(int, int);
static const int MAX = 200000;
int main() {
  int R[MAX], n, i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> R[i];
  int maxv = -2000000000;
  int minv = R[0];
  for (i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }
  cout << maxv << endl;
  return 0;
}
int max(int a, int b) {
  if (b > a)
    return b;
  else
    return a;
}
int min(int a, int b) {
  if (b < a)
    return b;
  else
    return a;
}


Error: TLE

Fixed submission:

#include <iostream>
int main() {
  using namespace std;
  const int MAX = 200000;
  int n, i, j, price[MAX], maxv;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> price[i];
  maxv = price[1] - price[0];
  {
    for (i = 0; i < n; i++)
      for (j = i + 1; j < n; j++)
        if (price[j] - price[i] > maxv)
          maxv = price[j] - price[i];
    cout << maxv << endl;
  }
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: 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 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;
}

Error: TLE

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

#define FOR(i, min, max) for (int(i) = (min); (i) < (max); (i)++)

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, ans, v[200000], pre = 0, pro = 0;
  cin >> n;
  FOR(i, 0, n) { cin >> v[i]; }
  ans = v[1] - v[0];

  FOR(i, 0, n) {
    if (v[i] <= v[pre])
      if (pro <= i) {
        FOR(j, i + 1, n) {
          if (v[j] - v[i] > ans) {
            ans = v[j] - v[i];
            pre = i;
            pro = j;
          }
        }
      } else {
        ans = v[pro] - v[i];
        pre = i;
      }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define FOR(i, min, max) for (int(i) = (min); (i) < (max); (i)++)

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, ans, v[200000], pre;
  cin >> n;
  FOR(i, 0, n) { cin >> v[i]; }
  ans = v[1] - v[0];
  pre = 0;

  FOR(i, 0, n) {
    if (pre != 0 || v[i] <= v[pre])
      FOR(j, i + 1, n) {
        if (v[j] - v[i] > ans) {
          ans = v[j] - v[i];
          pre = i;
        }
      }
  }
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int num;
  cin >> num;
  int currency[num];
  cin >> currency[0] >> currency[1];
  int min = (currency[0] < currency[1] ? currency[0] : currency[1]);
  int tmp = currency[1] - currency[0];
  for (int i = 2; i < num; i++) {
    cin >> currency[i];
    if (currency[i] - min > tmp)
      tmp = currency[i] - min;
    if (currency[i] < min)
      min = currency[i];
  }
  cout << tmp << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

using namespace std;

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

  int r[n];

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

  int _max = r[1] - r[0];
  int _min = pow(10, 9);

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

  cout << _max << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

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

  int r[n];

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

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

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

  cout << max_diff << endl;

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

#include <cmath>
#include <iostream>
#include <list>
#include <utility>
#include <vector>
using namespace std;

#define D

#ifdef D

int main(void) {
  int n;
  cin >> n;
  int max = -1000000000;

  int dis, in;
  cin >> in;
  int min = in;

  for (size_t i = 1; i < n; i++) {
    cin >> in;

    if (max < in - min)
      max = in - min;
    if (min > in)
      min = in;
  }

  cout << max << endl;

  return 0;
}
#endif // D

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <list>
#include <utility>
#include <vector>
using namespace std;

#define D

#ifdef D
int main(void) {
  int n;
  cin >> n;
  vector<long long> arr(n);
  for (auto &a : arr) {
    cin >> a;
  }
  long long max = arr.back() - arr.front();
  long long dis;
  for (size_t i = 0; i < n; i++) {
    for (size_t j = i + 1; j < n; j++) {
      dis = arr[j] - arr[i];
      if (max < dis)
        max = dis;
    }
  }
  cout << max << endl;

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


#include <iostream>

using namespace std;
#define NUMBER 200000

int main() {

  int ary[NUMBER];
  int min_register[NUMBER];

  /*
  for(int i = 0; i < NUMBER; i++){
          ary[i] = -1;
          min_register[i] = -1;
  }
  */

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> ary[i];
  }
  int min = ary[0];
  min_register[0] = min;
  for (int i = 1; i < n; i++) {
    if (min > ary[i]) {
      min = ary[i];
    }
    min_register[i] = min;
  }

  int max = -1000000001;
  for (int i = n - 1; i > 0; i--) {
    max = (max > ary[i] - min_register[i - 1]) ? max
                                               : ary[i] - min_register[i - 1];
  }
  cout << max << endl;
}

Error: TLE

Fixed submission:


#include <iostream>

using namespace std;
#define NUMBER 200000

int main() {

  int ary[NUMBER];

  for (int i = 0; i < NUMBER; i++) {
    ary[i] = -1;
  }

  int n;
  cin >> n;

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

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

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

using namespace std;

int solve(const vector<int> &v) {
  int res = numeric_limits<int>::min();
  for (int i = 0; i < v.size(); i++) {
    for (int j = i + 1; j < v.size(); j++) {
      res = max(res, v[j] - v[i]);
    }
  }
  return res;
}

int solve2(const vector<int> &v) {
  int maxv = numeric_limits<int>::min();
  int minv = v.front();

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

  return maxv;
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

  // cout << solve(v) << endl;
  cout << solve2(v) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int solve(const vector<int> &v) {
  int res = numeric_limits<int>::min();
  for (int i = 0; i < v.size(); i++) {
    for (int j = i + 1; j < v.size(); j++) {
      res = max(res, v[j] - v[i]);
    }
  }
  return res;
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

  cout << solve(v) << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int n, a[200010], ma[200010], mi[200010], ans = -1145141919;
int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    if (i == 0)
      mi[i] = a[i];
    else
      mi[i] = min(mi[i - 1], a[i]);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      ma[i] = a[i];
    else
      ma[i] = max(ma[i + 1], a[i]);
  }
  rep(i, n - 1) ans = max(ma[i + 1] - mi[i], ans);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, r[1000000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  int mi = r[0], ma = r[0];
  for (int j = 1; j < n; j++) {
    if (ma < r[j] - mi)
      ma = r[j] - mi;
    if (mi > r[j])
      mi = r[j];
  }
  cout << ma - mi << 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<long> nums;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    nums.push_back(x);
  }

  long del_max = nums[1] - nums[0];
  long minv = nums[0];
  for (int j = 1; j < n; ++j) {
    long delta = nums[j] - minv;
    if (delta > del_max) {
      del_max = delta;
    }
    if (minv > nums[j]) {
      minv = nums[j];
    }
  }
  cout << del_max << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long> nums;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    nums.push_back(x);
  }

  long max = -1000000000;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      long diff = nums[j] - nums[i];
      if (diff > max) {
        max = diff;
      }
    }
  }
  cout << max << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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


Error: TLE

Fixed submission:

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

int main() {
  int n, r[200100], max = -2000000000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  for (int i = 0; i < n; i++) {
    for (int 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 <algorithm>
#include <iostream>
using namespace std;

int main() {
  int A[200000], n;
  cin >> n;

  int i, j;

  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  int maxv = -1000000000;
  int minv = A[0];
  for (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;

int main() {
  int A[200000] = {0}, n;
  cin >> n;

  int i, j;

  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  int max = -1000000000;
  for (i = 0; i < n; i++)
    for (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 <iostream>
#include <math.h>
#include <sstream>

using namespace std;
int getMaxProfit(int *ary, int ary_size);

int main() {
  string str;
  getline(cin, str);
  int num;
  stringstream num_ss(str);
  num_ss >> num;

  int counter = 0;
  int *ary = new int[num];
  for (int i = 0; i < num; i++) {
    getline(cin, str);
    stringstream tmp_ss(str);
    tmp_ss >> ary[i];
  }

  cout << getMaxProfit(ary, num) << endl;
  return 0;
}

int getMaxProfit(int *ary, int ary_size) {
  int max_prf = ary[1] - ary[0];
  int min_num = (ary[1] > ary[0] ? ary[0] : ary[1]);
  for (int i = 2; i < ary_size; i++) {
    int tmp = ary[i] - min_num;
    if (tmp > max_prf) {
      max_prf = tmp;
    }
    min_num = (ary[i] < min_num ? ary[i] : min_num);
  }
  return max_prf;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <sstream>

using namespace std;
int getMaxProfit(int *ary, int ary_size);

int main() {
  string str;
  getline(cin, str);
  int num;
  stringstream num_ss(str);
  num_ss >> num;

  int counter = 0;
  int *ary = new int[num];
  for (int i = 0; i < num; i++) {
    getline(cin, str);
    stringstream tmp_ss(str);
    tmp_ss >> ary[i];
  }

  cout << getMaxProfit(ary, num) << endl;
  return 0;
}

int getMaxProfit(int *ary, int ary_size) {
  int max_prf = ary[1] - ary[0];
  for (int i = 2; i < ary_size; i++) {
    for (int j = 0; j < i; j++) {
      int tmp = ary[i] - ary[j];
      if (tmp > max_prf) {
        max_prf = tmp;
      }
    }
  }
  return max_prf;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, minh, maxh;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  minh = a[0];
  maxh = a[1] - a[0];
  for (int i = 1; i < n; i++) {
    maxh = max(maxh, a[i] - minh);
    minh = min(minh, a[i]);
  }
  cout << maxh << endl;
  return 0;
}

/*int main(){
        int n;
        cin>>n;
        int a[n];
        for(int i = 0;i<n;i++){
                cin>>a[i];
        }
        int syueki = a[1] -a[0];
        for(int i = 0;i<n-1;i++){
                for(int j = i+1;j<n;j++){
                        syueki = max(syueki,a[j]-a[i]);
                }
        }
        cout<<syueki<<endl;
}*/


Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;
  int maxv = -1000000000;
  int minv;
  int R[200000];
  for (int t = 0; t < n; t++) {
    cin >> R[t];
  }
  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: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int R[200000];
  for (int t = 0; t < n; t++) {
    cin >> R[t];
  }
  int max = R[1] - R[0];
  int tmp;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      tmp = R[j] - R[i];
      if (max < tmp) {
        max = tmp;
      }
    }
  }
  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;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int maxval = -1000000000;
  int minval = a[0];
  for (int i = 1; i < n; i++) {
    maxval = max(maxval, a[i] - minval);
    minval = min(minval, a[i]);
  }

  cout << maxval << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  cout << maxval << endl;

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

#define pb push_back
#define vi vector<int>
#include <bits/stdc++.h>
using namespace std;
int pro[300000];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> pro[i];
  int ans = INT_MIN, min_ = 0;
  for (int i = 1; i < n; i++) {
    ans = max(ans, pro[i] - pro[min_]);
    if (pro[min_] > pro[i])
      min_ = i;
  }
  //	if(ans == 0) ans = -1;
  cout << ans << endl;
}
int main(void) {
  solve();
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#define pb push_back
#define vi vector<int>
#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  vi pro(n, 0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> pro[i];
  int ans = 0, min_ = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, pro[i] - pro[min_]);
    if (pro[min_] > pro[i])
      min_ = i;
  }
  cout << ans << endl;
}
int main(void) {
  solve();
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 200000

int main() {

  int n;
  cin >> n;
  int A[MAX];

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

  int maxv = A[1] - A[0];
  int minv = A[0];

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

  cout << maxv << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 200000

int main() {

  int n;
  cin >> n;
  int A[MAX];

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

  int maxv = A[1] - A[0];

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

  cout << maxv << endl;

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

#include <iostream>
using namespace std;

int main() {
  int n, max, tmp, minR;
  cin >> n;
  int input[n];
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    input[i] = tmp;
  }

  max = -1000000000;
  minR = input[0];
  for (int i = 1; i < n; i++) {
    if (max < input[i] - minR)
      max = input[i] - minR;
    if (minR > input[i])
      minR = input[i];
  }

  cout << max << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, max, tmp;
  cin >> n;
  int input[n];
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    input[i] = tmp;
  }

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

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

#include <iostream>
using namespace std;
int main() {

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  int n, x[1000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  int max = x[1] - x[0];
  for (int k = 0; k < n; k++) {
    for (int m = k + 1; m < n; m++) {
      if (max < (x[m] - x[k]))
        max = x[m] - x[k];
    }
  }
  cout << max << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  int R[n];
  for (int t = 0; t < n; t++) {
    scanf("%d", &R[t]);
  }
  int minv = R[0];
  int maxv = -INT_MAX;
  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 <cstdio>

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