alexjercan/bugnet huggingface dataset:
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  vector<int> a;
  int n, r;
  cin >> n;

  int maxv = -2000000000, minv = 2000000000;

  for (int i = 0; i < n; i++) {
    cin >> r;
    maxv = max(maxv, r - minv);
    minv = min(minv, r);
  }
  cout << maxv << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  int maxv = -2000000000, minv = a[0];

  for (int i = 1; i < n; i++) {
    maxv = max(maxv, a[i] - minv);
    minv = min(minv, a[i]);
  }
  cout << maxv << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int main() {

  int R[200000], 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;

int main(void) {

  int R[20000] = {0}, maxv = -1000000000, minv;
  int i, n;

  cin >> n;

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

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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n, r[200000], minr = 2000000000, ans = -2000000000;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> r[i];
  for (int i = 0; i < n; i++) {
    ans = max(r[i] - minr, ans);
    minr = min(r[i], minr);
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

#include <iostream>
using namespace std;

int rt[200000];

int main() {
  ios::sync_with_stdio(false);
  int n;
  int i, j;
  int max, min;
  cin >> n;
  max = 0x80000000;
  for (i = 0; i < n; i++) {
    cin >> rt[i];
  }
  min = rt[0];
  for (i = 1; i < n; i++) {
    if ((rt[i] - min) > max) {
      max = rt[i] - min;
    }
    if ((rt[i]) < min) {
      min = rt[i];
    }
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int rt[200000];

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

#include <iostream>
#include <vector>
using namespace std;
//---------------------------------------
int max_profit(vector<int> vec);
//---------------------------------------

int main(void) {
  int num;
  vector<int> num_all;

  cin >> num;

  for (int i = 0; i < num; i++) {
    int n;
    cin >> n;
    num_all.push_back(n);
  }
  cout << max_profit(num_all) << endl;

  return 0;
}
//--------------------------------------------
int max_profit(vector<int> vec) {
  int minv = vec[0];
  int maxv = vec[1] - vec[0];

  for (int j = 1; j < vec.size(); j++) {
    if ((vec[j] - minv) > maxv) {
      maxv = vec[j] - minv;
    }
    if ((vec[j] < minv)) { //????????????????????????
      minv = vec[j];
    }
  }
  return maxv;
}
/*time error
int max_profit(vector<int> vec)
{
        int max = vec[1] - vec[0]; //???????????????????????\\????????????

        for(int i = vec.size() - 1; i > 0; i--){
                for(int j = 0; j < i; j++){
                        int num = vec[i] - vec[j];
                        if(max < num){
                                max = num;
                        }
                }
        }
        return max;
}
*/

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
//---------------------------------------
int max_profit(vector<int> vec);
//---------------------------------------

int main(void) {
  int num;
  vector<int> num_all;

  cin >> num;

  for (int i = 0; i < num; i++) {
    int n;
    cin >> n;
    num_all.push_back(n);
  }
  cout << max_profit(num_all) << endl;

  return 0;
}
//--------------------------------------------
int max_profit(vector<int> vec) {
  int max = vec[1] - vec[0]; //???????????????????????\\????????????

  for (int i = vec.size() - 1; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      int num = vec[i] - vec[j];
      if (max < num) {
        max = num;
      }
    }
  }
  return max;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

    // cout << "DEBUG:" << "minv=" << minv << ",maxv=" << maxv << endl;
  }

  cout << maxv << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

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

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

      if ((price[j] - price[i]) > max)
        max = (price[j] - price[i]);
    }
  }

  cout << max << endl;

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

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <typeinfo>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;

int main() {

  int n;
  cin >> n;

  vector<int> r(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;

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <typeinfo>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;

int main() {

  int n;
  cin >> n;

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

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

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (max < r[j] - r[i]) {
        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) {
  int n, R[200001];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  int minv = R[0];
  int maxv = -1000000001;
  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 <iostream>
using namespace std;
int main(void) {
  int n, R[200001];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  int m = -1000000000;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (R[j] - R[i] > m)
        m = R[j] - R[i];
    }
  }

  cout << m << endl;

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

#include <cmath>
#include <iostream>

#define SIZE 200000

using namespace std;

int main(void) {
  int n, max, min, tmp;

  cin >> n;

  cin >> tmp >> max;

  min = max;
  max -= tmp;
  min = (tmp < min) ? tmp : min;

  for (int i = 0; i < n - 2; i++) {
    cin >> tmp;
    max = (max < tmp - min) ? (tmp - min) : max;
    min = (tmp < min) ? tmp : min;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

#define SIZE 200000

using namespace std;

int main(void) {
  int n, max, tmp, list[SIZE];

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

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

  cout << max << "\\n";

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

#include <iostream>
using namespace std;

int main() {
  int n;
  int i, j;
  int *p;
  int max = -1000000000;
  int min;

  cin >> n;
  p = new int[n];

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

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

  cout << max << endl;

  delete[] p;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  int i, j;
  int *p;
  int pro = -1000000000;

  cin >> n;
  p = new int[n];

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

  for (j = 1; j < n; j++) {
    for (i = 0; i < j; i++) {
      if (pro < p[j] - p[i]) {
        pro = p[j] - p[i];
      }
    }
  }

  cout << pro << endl;

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

#include "bits/stdc++.h"

using namespace std;

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

Error: 0
sh: 1: pause: not found


Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> R(n);
  for (int i = 0; i < n; ++i)
    cin >> R[i];
  auto itr_min = min_element(R.begin(), R.end());
  auto itr_max = max_element(itr_min, R.end());
  cout << ((*itr_max - *itr_min) <= 0 ? -1 : (*itr_max - *itr_min)) << endl;
  system("pause");
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, r;
  long long max = -2000000000, min = 2000000000;
  cin >> n;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

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

using namespace std;
int main() {
  int n;
  cin >> n;
  int maxV = -1000000000;

  vector<int> v;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  int minV = v[0];
  for (int i = 1; i < n; i++) {
    maxV = max(maxV, v[i] - minV);
    minV = min(minV, v[i]);
  }
  cout << maxV << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
using namespace std;

int main() {
  int n;
  long int r[200000];
  long int maxv = -1000000000000;
  long int minv = 1000000000000;

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

  for (int i = 0; i < n; i++) {
    maxv = (maxv < (r[i] - minv)) ? (r[i] - minv) : maxv;
    minv = (minv > r[i]) ? r[i] : minv;
  }
  cout << maxv << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  long int r[200000];
  long int maxv = -1000000000000;

  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 (maxv < r[j] - r[i])
        maxv = r[j] - r[i];
    }
  }
  cout << maxv << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n;
  cin >> n;

  int *r = new int[n];
  int maxv = -2000000000;
  int minv = 1000000000;

  if (n == 2) {
    cin >> r[0] >> r[1];
    maxv = r[1] - r[0];
    cout << maxv << endl;
    return 0;
  }

  for (int i = 0; i < n; ++i) {
    cin >> r[i];
    maxv = max(maxv, r[i] - minv);
    minv = min(minv, r[i]);
  }

  cout << maxv << endl;

  delete[] r;
  return 0;
}

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 ans = -1000000000;
  for (int j = 0; j < n; ++j) {
    for (int k = j + 1; k < n; ++k) {
      int tmp = r[k] - r[j];
      if (ans < tmp)
        ans = tmp;
    }
  }
  cout << ans << endl;

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

#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
  int n = 0;
  cin >> n;
  int min;
  cin >> min;
  int maxp = -2147483647;
  for (int i = 0; i < n - 1; i++) {
    int x = 0;
    cin >> x;
    if (maxp < x - min)
      maxp = x - min;
    if (x < min)
      min = x;
  }
  cout << maxp << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
  int n = 0;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = -2147483647;
  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;

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

#include <iostream>
using namespace std;

int main() {
  int n, r;
  int max, min;

  cin >> n;

  cin >> r;          // 最初の数を入れる
  min = r;           // 最小値は最初の数
  max = -2000000000; // 差の最大値は-20億で初期化

  for (int i = 0; i < n - 1; i++) // 初めの数はもう入力しているので、n - 1
  {
    cin >> r;
    if (max < r - min)
      max = r - min;
    if (min > r)
      min = r;
  }

  cout << max << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, sum = -2000000000;

  cin >> n;

  int r[n];

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

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

  cout << sum << endl;

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

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

int main() {
  int n, min, sub;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i != n; i++) {
    cin >> a[i];
    if (i == 0)
      min = a[0];
    if (i == 1)
      sub = a[1] - a[0];
    if (i > 1 && sub < a[i] - min)
      sub = a[i] - min;
    if (i > 0 && a[i] < min)
      min = a[i];
  }
  cout << sub << endl;
}


Error: TLE

Fixed submission:

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

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

#include <iostream>
using namespace std;
int main() {
  int n, tag, minv, i, j;
  int *p;
  cin >> n;
  p = new int[n];
  for (i = 0; i < n; i++)
    cin >> p[i];
  tag = p[1] - p[0];
  minv = p[0];
  for (i = 1; i < n; i++) {
    if (p[i] - minv > tag)
      tag = p[i] - minv;
    if (minv > p[i])
      minv = p[i];
  }
  cout << tag << endl;
  delete[] p;
  return 0;
}


Error: TLE

Fixed submission:

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


#include <stdio.h>

#define INPUT_MAX_NUM (200000)

int main(void) {
  // input num
  int num = 0;
  //  printf("input num\\n");
  scanf("%d", &num);

  // input value
  int r[INPUT_MAX_NUM];
  for (int i = 0; i < num; i++) {
    scanf("%d", &r[i]);
  }

  // max value of diff
  /*
  int max_diff;
  max_diff = r[1] - r[0];
  for (int i = 1; i < num; i++ ) {
    for (int j = i; j < num; j++ ) {
      if ( max_diff < r[j] - r[i-1] ) {
        max_diff = r[j] - r[i-1];
      }
    }
  }
  */

  int max_diff = r[1] - r[0];
  int min = r[0];
  for (int i = 1; i < num; i++) {
    if (max_diff < r[i] - min) {
      max_diff = r[i] - min;
    }
    if (min > r[i]) {
      min = r[i];
    }
  }
  // printf("max_diff = %d\\n", max_diff);
  printf("%d\\n", max_diff);

  return 0;
}

Error: TLE

Fixed submission:


#include <stdio.h>

#define INPUT_MAX_NUM (200000)

int main(void) {
  // input num
  int num = 0;
  //  printf("input num\\n");
  scanf("%d", &num);

  // input value
  int r[INPUT_MAX_NUM];
  for (int i = 0; i < num; i++) {
    scanf("%d", &r[i]);
  }

  // max value of diff
  int max_diff;
  max_diff = r[1] - r[0];
  for (int i = 1; i < num; i++) {
    for (int j = i; j < num; j++) {
      if (max_diff < r[j] - r[i - 1]) {
        max_diff = r[j] - r[i - 1];
      }
    }
  }
  // printf("max_diff = %d\\n", max_diff);
  printf("%d\\n", max_diff);

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

#include <bits/stdc++.h>

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

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

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

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

typedef long long ll;
using namespace std;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

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

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

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

typedef long long ll;
using namespace std;

int n;
int main() {
  cin >> n;
  int r[n];
  rep(i, 0, n) { cin >> r[i]; }
  int ans = INT_MIN;
  rep(i, 0, n - 1) {
    rep(j, i + 1, n) { ans = max(r[j] - r[i], ans); }
  }
  pout(ans);
  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;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

using namespace std;

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

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

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

  cout << diffmax << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

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

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

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

  cout << diffmax << endl;

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

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

int main(void) {
  int n, rt[MAX];

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

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

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

Error: TLE

Fixed submission:

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

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

  for (int i = 0; i < n; i++)
    cin >> rt[i];
  int maxv = rt[1] - rt[0];
  for (int j = 1; j < n; j++) {
    for (int i = 0; i < j; i++) {
      maxv = maxv < rt[j] - rt[i] ? rt[j] - rt[i] : maxv;
    }
  }
  cout << maxv << endl;
  return 0;
}