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

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

#define INF -99999999999;
using namespace std;

void findcloss(int *A, int low, int high, int mid, int *maxleft, int *maxright,
               int *val) {
  int leftsum = INF;
  int rightsum = INF;
  int sum = 0;

  for (int i = mid; i >= low; i--) {
    sum += A[i];
    if (sum > leftsum) {
      leftsum = sum;
      int *p = &i;
      *maxleft = *p;
    }
  }

  sum = 0;
  for (int i = mid + 1; i <= high; i++) {
    sum += A[i];
    if (sum > rightsum) {
      rightsum = sum;
      int *p = &i;
      *maxright = *p;
    }
  }
  sum = leftsum + rightsum;
  int *p = &sum;
  *val = *p;
}

void findmax(int *A, int low, int high, int *anslow, int *anshigh,
             int *ansval) {
  if (high == low) {
    int *p = &low;
    int *q = &high;
    int *r = &A[low];
    *anslow = *p;
    *anshigh = *q;
    *ansval = *r;
  } else {
    int mid = (int)floor((low + high) / 2);
    int leftlow = INF;
    int lefthigh = INF;
    int leftsum = INF;
    findmax(A, low, mid, &leftlow, &lefthigh, &leftsum);
    int rightlow = INF;
    int righthigh = INF;
    int rightsum = INF;
    findmax(A, mid + 1, high, &rightlow, &righthigh, &rightsum);
    int crosslow = INF;
    int crosshigh = INF;
    int crosssum = INF;
    findcloss(A, low, high, mid, &crosslow, &crosshigh, &crosssum);

    if (leftsum >= rightsum && leftsum >= crosssum) {
      int *a = &leftlow;
      int *b = &lefthigh;
      int *c = &leftsum;
      *anslow = *a;
      *anshigh = *b;
      *ansval = *c;
    } else if (rightsum >= leftsum && rightsum >= crosssum) {
      int *a = &rightlow;
      int *b = &righthigh;
      int *c = &rightsum;
      *anslow = *a;
      *anshigh = *b;
      *ansval = *c;
    } else {
      int *a = &crosslow;
      int *b = &crosshigh;
      int *c = &crosssum;
      *anslow = *a;
      *anshigh = *b;
      *ansval = *c;
    }
  }
}
int main() {
  int N;
  cin >> N;
  int *A = (int *)malloc(sizeof(int) * (N + 1));
  int *B = (int *)malloc(sizeof(int) * (N + 1));
  A[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    B[i] = A[i] - A[i - 1];
  }
  // B[i]?????????????????§??¨??????????????????????????°??????.
  int anslow = INF;
  int anshigh = INF;
  int ansval = INF;
  findmax(B, 2, N, &anslow, &anshigh, &ansval);
  cout << ansval << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  int *A = (int *)malloc(sizeof(int) * (N + 1));
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  //??????
  int i, j;
  int v, pro = A[2] - A[1];
  for (i = 1; i < N; i++) {
    for (j = i + 1; j <= N; j++) {
      v = A[j] - A[i];
      if (v > pro) {
        pro = v;
      }
    }
  }
  cout << pro << "\\n";
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <iostream>

using namespace std;

int main() {
  int R[200000], n;
  int i, max, min;

  cin >> n;

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

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

  min = R[0];

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

  cout << max << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int R[200000], n;
  int i, max;

  cin >> n;

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

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

  for (int j = n - 1; j > 0; j--) {
    for (i = 0; i <= j - 1; i++) {
      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 <cstdio>
using namespace std;
int getMaxProfit(int[], int);

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

  printf("%d\\n", getMaxProfit(data, n));
  return 0;
}

int getMaxProfit(int data[], int n) {
  int i = 0, key, m = INT_MIN;

  key = data[0];
  for (int i = 1; i < n; i++) {
    if (data[i] - key > m)
      m = data[i] - key;
    if (key > data[i])
      key = data[i];
  }
  return m;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;
int main() {
  int m = INT_MIN, n;
  scanf("%d", &n);
  int data[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &data[i]);

  for (int i = n - 1; i > 0; i--)
    for (int j = i - 1; j >= 0; j--)
      m = max(m, data[i] - data[j]);

  printf("%d\\n", m);
  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: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000000;
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 <bits/stdc++.h>
#define LL long long
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<LL> vc(n);

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

  LL res = INT_MIN;
  LL temp = vc[0];

  for (int i = 1; i < n; i++) {
    res = max(res, vc[i] - temp);
    temp = min(temp, vc[i]);
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<LL> vc(n);

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

  LL res = INT_MIN;

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

  cout << res << endl;

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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  long R[200000], m = -1000000000, min;
  int i, j, N;
  cin >> N;
  cin >> min;
  for (j = 0; j < N - 1; j++) {
    cin >> R[j];
    if (R[j] - min > m)
      m = R[j] - min;
    if (min > R[j])
      min = R[j];
  }
  cout << m << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  long R[200000], m = -1000000000;
  int i, j, N;
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> R[i];
  }
  for (i = 0; i < N; i++) {
    for (j = i + 1; j < N; j++) {
      if (R[j] - R[i] > m)
        m = R[j] - R[i];
    }
  }
  cout << m << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

class MaximumProfit {
public:
  int solve(int n, int *r);
};

int MaximumProfit::solve(int n, int *r) {
  int maxp = -2000000000;
  int minr = r[0];
  for (int i = 1; i < n; i++) {
    if ((r[i] - minr) > maxp)
      maxp = r[i] - minr;
    if (r[i] < minr)
      minr = r[i];
  }

  return maxp;
}

int main(void) {

  MaximumProfit *mp = new MaximumProfit();

  int n;

  cin >> n;

  int *r = new int[n];

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

  cout << mp->solve(n, r) << endl;

  delete mp;
  delete[] r;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

class MaximumProfit {
public:
  int solve(int n, int *r);
};

int MaximumProfit::solve(int n, int *r) {
  int maxp = -2000000000;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++) {
      if ((r[j] - r[i]) > maxp)
        maxp = r[j] - r[i];
    }

  return maxp;
}

int main(void) {

  MaximumProfit *mp = new MaximumProfit();

  int n;

  cin >> n;

  int *r = new int[n];

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

  cout << mp->solve(n, r) << endl;

  delete mp;
  delete[] r;

  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 <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int a[220000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int maxa = -9999999999;
  int maxq = a[0];
  for (int i = 1; i < n; i++) {

    maxa = max(maxa, a[i] - maxq);
    maxq = min(maxq, a[i]);
  }
  cout << maxa << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int a[200010];
int main() {
  int n, max = -2000000010, s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      s = a[i] - a[j];
      if (s > max)
        max = s;
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// Created by yoshihito.hotta on 2018/06/10.
//
#include <climits>
#include <iostream>
using namespace std;

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

  int *r;
  r = new int[n];

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

  int minr;
  int maxv;

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

  cout << maxv << endl;

  return 0;
}


Error: TLE

Fixed submission:

//
// Created by yoshihito.hotta on 2018/06/10.
//
#include <iostream>
using namespace std;

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

  int *r;
  r = new int[n];

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

  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;

  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 max_profit(vector<int> &price) {
  int profit;
  long long int max = -999999999;
  int min = 1;
  for (int i = 1; i <= price.size() - 1; i++) {

    if (i == 1) {
      min = price[0];
    } else if (min >= price[i - 1]) {
      min = price[i - 1];
    }

    profit = price[i] - min;

    if (max < profit) {
      max = profit;
    }
  }
  return max;
}
int main(void) {
  int n;
  cin >> n;

  vector<int> price(n);

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

    cin >> price[i];
  }

  int max = max_profit(price);

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

Error: TLE

Fixed submission:

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

int max_profit(vector<int> &price) {
  int a;
  long long int max = -999999999;
  for (int i = 1; i <= price.size() - 1; i++) {
    for (int j = 0; j < i; j++) {
      a = price[i] - price[j];

      if (max < a) {
        max = a;
      }
    }
  }
  return max;
}
int main(void) {
  int n;
  cin >> n;

  vector<int> price(n);

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

    cin >> price[i];
  }

  int max = max_profit(price);

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

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

using namespace std;

int main() {
  int n, nextNum, max = INT_MIN, preMin = INT_MAX;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> nextNum;
    if (nextNum - preMin > max) {
      max = nextNum - preMin;
    }
    if (nextNum < preMin)
      preMin = nextNum;
  }

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

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n, *nums, max = INT_MIN, preMin = INT_MAX;
  cin >> n;
  nums = (int *)malloc(sizeof(int) * n);

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

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

  cout << max << endl;
  free(nums);
  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 <bits/stdc++.h>
using namespace std;

int main() {
  int N, MAX = -2000000000;
  cin >> N;
  vector<int> R(N);
  for (int i = 0; i < N; i++)
    cin >> R.at(i);

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (MAX < (R.at(j) - R.at(i)))
        MAX = R.at(j) - R.at(i);
    }
  }
  cout << MAX << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int r, m = -1e9, min;
  int N;
  cin >> N;
  cin >> min;
  for (int j = 0; j < N - 1; j++) {
    cin >> r;
    if (r - min > m)
      m = r - min;
    if (min > r)
      min = r;
  }
  cout << m << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  int t[200000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &t[i]);
  }
  int ans = -1e9;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = max(ans, t[j] - t[i]);
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int a[200010];
int main() {
  int n, minh, maxh = -2000000000;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  minh = a[0];
  for (int i = 1; i < n; i++) {
    maxh = max(maxh, a[i] - minh);
    minh = min(minh, a[i]);
  }
  printf("%d\\n", maxh);
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;
vector<int> n;
int main(void) {
  int count = 0, num, ans = INT_MIN, cn;
  cin >> count;
  for (int i = 0; i < count; i++) {
    cin >> num;
    n.push_back(num);
  }
  count = n.size();
  for (int j = 0; j < count - 1; j++)
    for (int i = j + 1; i < count; i++) {
      cn = n.at(i) - n.at(j);
      ans = cn >= ans ? cn : ans;
    }
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  vector<int> v1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    v1.push_back(m);
  }
  int MIN = v1[0], ans = v1[1] - v1[0];
  for (int i = 2; v1.begin() + i != v1.end(); i++) {
    MIN = min(MIN, v1[i - 1]);
    ans = max(ans, v1[i] - MIN);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  vector<int> v1, v2;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    v1.push_back(m);
  }
  for (int i = 1; (v1.end() - i) != v1.begin(); i++) {
    v2.push_back(*(v1.end() - i) - *min_element(v1.begin(), v1.end() - i));
  }
  int ans = *max_element(v2.begin(), v2.end());
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int M(vector<int> A) {
  int m = 0;
  int c = 0;
  int d = A[1] - A[0];
  while (1) {
    for (int i = m + 1; i < A.size(); i++) {
      c++;
      if (A[i] - A[m] < 0) {
        m = i;
        break;
      }
      if (A[i] - A[m] > d) {
        d = A[i] - A[m];
      }
    }
    if (c >= A.size() - 1)
      break;
  }
  return d;
}

int main() {
  int n;
  cin >> n;
  vector<int> A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  cout << M(A) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int M(vector<int> A) {
  int m = 0;
  bool flag = true;
  int d = A[1] - A[0];
  while (1) {
    for (int i = m + 1; i < A.size(); i++) {
      flag = true;
      if (A[i] - A[m] < 0) {
        m = i;
        flag = false;
        break;
      }
      if (A[i] - A[m] > d) {
        d = A[i] - A[m];
      }
    }
    if (flag == true)
      break;
  }
  return d;
}

int main() {
  int n;
  cin >> n;
  vector<int> A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  cout << M(A) << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n;
  int a[200000];

  cin >> n;

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

  int maxv = -2147483647;
  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;

int main() {
  int n;

  cin >> n;

  int *a = new int[n];

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

  int max = -2147483647;
  int result = 0;
  for (int i = n - 1; i > 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      result = a[i] - a[j];
      if (result > max)
        max = result;
    }
  }

  cout << max << endl;

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

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

using namespace std;

static const int MAX = 200000;

int main() {
  int R[MAX];
  int num;

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

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

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

  cout << maxv << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

  std::vector<int> r_t;
  for (int i = 0; i < num; i++) {
    int n;
    cin >> n;
    r_t.push_back(int(n));
  }

  int max_value = INT_MIN;
  for (int i = 0; i < num - 1; i++) {
    for (int j = i + 1; j < num; j++) {
      if (r_t[j] - r_t[i] > max_value) {
        max_value = r_t[j] - r_t[i];
      }
    }
  }

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

#include <iostream>
using namespace std;

int main() {
  int n, R[200001], min = 2e9, max = -2e9;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> R[i];

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

  cout << max << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, R[200001], max = -1e9;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> R[i];

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

  cout << max << endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  long n;
  cin >> n;
  long long S[200001];
  int i, k;
  long long ans = -1000000001;

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

  for (i = 1; i < n; i++) {
    ans = max(ans, S[i] - minimum);
    minimum = min(minimum, S[i]);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  long n;
  cin >> n;
  long long S[200001];
  int i, k;
  long long ans = -1000000001;
  for (i = 0; i < n; i++) {
    cin >> S[i];
  }

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

#include <bits/stdc++.h>

using namespace std;

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

  int input;
  int min = INT_MAX, max = INT_MIN;

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

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

  cout << max << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

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

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

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N;
  cin >> N;
  int R[N + 5];
  int maxn = -999999999;
  for (int t = 0; t < N; t++) {
    cin >> R[t];
  }
  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 <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N;
  cin >> N;
  int R[N + 5];
  int maxn = -999999999;
  for (int t = 0; t < N; t++) {
    cin >> R[t];

    if (t > 0) {
      for (int i = 0; i < t; i++) {
        maxn = max(R[t] - R[i], maxn);
      }
    }
  }
  cout << maxn << endl;
  return 0;
}