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

#include <iostream>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n;
  cin >> n;
  int r[n];
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  int minv = r[0];
  int maxv = -2000000000;
  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 n;
  cin >> n;
  int 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 (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() {
  int n;
  cin >> n;
  int array[n];
  for (int i = 0; i < n; i++) {
    cin >> array[i];
  }
  int min = array[0];
  int max = array[1] - array[0];

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

    if ((array[i] - min) > max) {
      max = array[i] - min;
    }
    if (array[i] < min) {
      min = array[i];
    }
  }
  cout << max << "\\n";
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int array[n];
  for (int i = 0; i < n; i++) {
    cin >> array[i];
  }
  int max = array[1] - array[0];

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

#include <climits>
#include <cstdio>

int max(int a, int b) { return (a > b) ? a : b; }

int maxRange(int *a, int start, int end) {
  int max = INT_MIN;
  for (int i = start; i <= end; i++) {
    if (a[i] > max)
      max = a[i];
  }
  return max;
}

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

  int max_range = INT_MIN;
  int min = R[0];
  int max_v = maxRange(R, 1, N - 1);
  for (int i = 0; i < N - 1; i++) {
    if (R[i] < min)
      min = R[i];
    if (R[i] == max_v)
      max_v = maxRange(R, i + 1, N - 1);
    max_range = max(max_range, max_v - min);
  }
  printf("%d\\n", max_range);
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>

int max(int a, int b) { return (a > b) ? a : b; }

int maxRange(int *a, int start, int end) {
  int max = INT_MIN;
  for (int i = start; i <= end; i++) {
    if (a[i] > max)
      max = a[i];
  }
  return max;
}

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

  int max_range = INT_MIN;
  int min = R[0];
  for (int i = 0; i < N - 1; i++) {
    if (R[i] < min)
      min = R[i];
    max_range = max(max_range, maxRange(R, i + 1, N - 1) - min);
  }
  printf("%d\\n", max_range);
  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 M = -1000000000;
  int m;
  cin >> m;
  for (int i = 1; i < n; ++i) {
    int r;
    cin >> r;
    M = max(M, r - m);
    m = min(m, r);
  }
  cout << M << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#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 M = r[1] - r[0];
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      M = max(M, r[j] - r[i]);
    }
  }
  cout << M << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int R, n, i, max_gain, min;
  cin >> n;
  cin >> R;
  min = R;
  cin >> R;
  max_gain = R - min;
  if (min > R)
    min = R;
  for (i = 2; i < n; i++) {
    cin >> R;
    if (max_gain < R - min)
      max_gain = R - min;
    if (min > R)
      min = R;
  }
  cout << max_gain << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int R[200000], max_gain, n, i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> R[i];
  }
  max_gain = R[1] - R[0];
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (max_gain < R[j] - R[i])
        max_gain = R[j] - R[i];
    }
  }
  cout << max_gain << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

void input_array(int a[], int n) {
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
}

int max_value(int a[], int n) {

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

  for (int i = 1; i < n; i++) {
    maxv = (maxv < (a[i] - minv)) ? a[i] - minv : maxv;
    minv = (minv < a[i]) ? minv : a[i];
  }

  return maxv;
}

int main() {

  int n;
  std::cin >> n;
  int R[n];
  input_array(R, n);
  int v = max_value(R, n);
  std::cout << v << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

void input_array(int a[], int n) {
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
}

int max_value(int a[], int n) {

  int max_v = a[1] - a[0];
  int idx_before, idx_before_max_a, idx_max_a;

  for (int i = 0; i < n; i++) {
    int max_a = a[i + 1];
    if (i > 0 && i < n - 1) {
      while (a[i] > a[idx_before] && i < n - 1) {
        i += 1;
      }
      if (i == n - 1) {
        break;
      }

      max_a = a[i + 1];
      idx_max_a = i + 1;

      if (idx_max_a <= idx_before_max_a) {
        idx_before = i;
        max_v = a[idx_before_max_a] - a[i];

      } else {
        for (int j = i + 2; j < n; j++) {
          if (a[j] > max_a) {
            max_a = a[j];
            idx_max_a = j;
          }
        }
        idx_before = i;
        if ((max_a - a[i]) > max_v) {
          max_v = max_a - a[i];
          idx_before_max_a = idx_max_a;
        }
      }
    } else {
      for (int j = i + 2; j < n; j++) {
        if (a[j] > max_a) {
          max_a = a[j];
          idx_max_a = j;
        }
      }
      idx_before = i;
      max_v = max_a - a[i];
      idx_before_max_a = idx_max_a;
    }
  }
  return max_v;
}

int main() {

  int n;
  std::cin >> n;
  int R[n];
  input_array(R, n);
  int v = max_value(R, n);
  std::cout << v << '\\n';
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int money[200001];
  money[0] = 2000000000;
  int maxProfit = 1 - 1000000000;
  int min = money[0];

  for (int i = 1; i <= n; i++) {
    cin >> money[i];
    if (money[i] - min > maxProfit) {
      maxProfit = money[i] - min;
    }
    if (min > money[i]) {
      min = money[i];
    }
  }
  cout << maxProfit << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int money[200001];
  int min;
  int profit = 1 - 1000000000;

  for (int i = 1; i <= n; i++) {
    cin >> money[i];
  }
  for (int j = 1; j < n; j++) {
    min = money[j];
    for (int k = j + 1; k <= n; k++) {
      if (money[k] - min > profit) {
        profit = money[k] - min;
      }
    }
  }

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

#include <iostream>

using namespace std;
static const int MAX = 200000;

int main() {
  int a[MAX], n;
  cin >> n;
  int max, tmp;
  int min;
  max = -1000000000;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  min = a[0];
  for (int i = 1; i < n; i++) {
    tmp = a[i] - min;
    if (max < tmp) {
      max = tmp;
    }
    if (min > a[i]) {
      min = a[i];
    }
  }
  cout << max << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
static const int MAX = 200000;

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

#include <iostream>
using namespace std;

int main() {
  int n, dif;
  cin >> n;
  int *r = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  int currentMax = r[1] - r[0];
  int currentMin = r[0];
  for (int i = 1; i < n; i++) {
    if (r[i] - currentMin > currentMax)
      currentMax = r[i] - currentMin;
    if (r[i] < currentMin)
      currentMin = r[i];
  }

  cout << currentMax << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, dif;
  cin >> n;
  int *r = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  int currentMax = r[1] - r[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      dif = r[i] - r[j];
      if (dif > currentMax) {
        currentMax = dif;
      }
    }
  }

  cout << currentMax << endl;

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

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

int main() {

  long long n, MAX = -1000000000;
  cin >> n;
  vector<int> R(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 (MAX < R[j] - R[i])
        MAX = R[j] - R[i];
      if (R[i] >= R[j])
        break;
    }
  }

  cout << MAX << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  long long n, MAX = -1000000000;
  cin >> n;
  vector<int> R(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 (MAX < R[j] - R[i])
        MAX = R[j] - R[i];
    }
  }

  cout << MAX << endl;

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

// ALDS_1_1_D.cpp
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int R[n];
  int maxv = -2000000000;

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

  int minv = R[0];

  for (size_t i = 1; i < n; i++) {
    if (maxv < R[i] - minv) {
      maxv = R[i] - minv;
    }
    if (minv > R[i]) {
      minv = R[i];
    }
  }

  cout << maxv << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int R[n];
  int maxv = -2000000000;

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

  for (size_t i = 1; i < n; i++) {
    for (size_t j = 0; j < i; j++) {
      if (maxv <= R[i] - R[j]) {
        maxv = R[i] - R[j];
      }
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

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

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

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

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

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  int n;

  scanf("%d", &n);
  vector<int> r(n), max_n(n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &r[i]);

  max_n[n - 2] = r[n - 1];
  for (int i = n - 3; i >= 0; --i)
    max_n[i] = max(r[i + 1], max_n[i + 1]);

  int ans = -1e9 - 1;
  for (int i = 0; i < n - 1; ++i)
    ans = max(ans, max_n[i] - r[i]);

  cout << ans << "\\n";

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  int n;

  scanf("%d", n);
  vector<int> r(n), max_n(n);
  for (int i = 0; i < n; ++i)
    cin >> r[i];

  max_n[n - 2] = r[n - 1];
  for (int i = n - 3; i >= 0; --i)
    max_n[i] = max(r[i + 1], max_n[i + 1]);

  int ans = -1e9 - 1;
  for (int i = 0; i < n - 1; ++i)
    ans = max(ans, max_n[i] - r[i]);

  cout << ans << "\\n";

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int R[200008];
int n, dif, minv;

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

void solve() {
  dif = -2000000000;
  minv = R[0];
  for (int i = 1; i < n; i++) {
    dif = max(dif, R[i] - minv);
    minv = min(minv, R[i]);
  }
  cout << dif << endl;
}
int main() {
  input();
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int R[200008];
int n;
int dif;

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

void solve() {
  dif = -2000000000;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (dif < (R[j] - R[i])) {
        dif = R[j] - R[i];
      }
    }
  }
  cout << dif << endl;
}
int main() {
  input();
  solve();
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int R[200000];
  int n;
  int maxv, minv;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];
  maxv = -2000000001;
  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 <cmath>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int R[200000];
  int t;
  int max = -2000000001; // 最大利益
  int min;
  cin >> t;

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

  for (int j = 0; j < t; j++) {
    for (int i = 0; i < j; i++) {
      max = (max < R[j] - R[i]) ? R[j] - R[i] : max;
    }
  }
  cout << max << endl;

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

#include <iostream>
using namespace std;

int main() {
  unsigned long n = 0;
  long a = 0, b = 0, pf = 0;

  cin >> n;
  cin >> a;

  b = a;
  pf = -a;

  for (unsigned long i = 0; i < n - 1; i++) {
    cin >> a;

    if (a - b >= pf)
      pf = a - b;
    if (a < b)
      b = a;
  }

  cout << pf << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  unsigned long n = 0;
  long a[200000] = {0}, pf = 0;

  cin >> n;

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

  pf = a[1] - a[0];

  for (unsigned long i = 0; i < n - 1; i++) {
    for (unsigned long j = i + 1; j < n; j++) {
      if (a[j] - a[i] > pf)
        pf = a[j] - a[i];
    }
  }

  cout << pf << endl;

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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
  int m, n;
  cin >> n;
  int ans = -INF;
  cin >> m;
  int Min = m;
  for (int i = 2; i <= n; i++) {
    cin >> m;
    ans = max(ans, m - Min);
    Min = min(Min, m);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
  int m, n;
  while (scanf("%d", &n)) {
    int ans = -INF;
    cin >> m;
    int Min = m;
    for (int i = 2; i <= n; i++) {
      cin >> m;
      ans = max(ans, m - Min);
      Min = min(Min, m);
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  int a[200000];
  cin >> n;
  int mi = 2000000000, mx = -1000000000;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i] - mi);
    mi = min(mi, a[i]);
  }

  cout << mx << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int A[100000];

void sort(int N) {
  for (int i = 1; i < N; i++) {
    for (int t = i; t > 0; t--) {
      if (A[t] < A[t - 1]) {
        swap(A[t], A[t - 1]);
      } else {
        break;
      }
    }
  }
}

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

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

  cout << mx << endl;

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

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

int main() {
  int n, c, b = 2000000000, a = -1000000001;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c;
    a = max(c - b, a);
    b = min(c, b);
  }
  cout << a << 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];
  int a = r[1] - r[0];
  for (int i = 1; i < n; i++) {
    for (int o = i - 1; o >= 0; o--) {
      if (r[i] - r[o] > a)
        a = r[i] - r[o];
    }
  }
  cout << a << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, i, j, l = 0;
  long m = -1000000000, k = 0;
  int a[200000];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  l = a[0];
  for (i = 1; i < n; i++) {
    k = a[i] - l;
    if (m < k) {
      m = k;
    }
    if (a[i] < l) {
      l = a[i];
    }
  }

  cout << m << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, i, j;
  long m = -1000000000, k = 0;
  int a[200000];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (i = 0; i < n - 1; i++) {
    for (j = i; j < n - 1; j++) {
      k = a[j + 1] - a[i];
      if (m < k) {
        m = k;
      }
    }
  }

  cout << m << endl;

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

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

int main() {
  int n, i, j, maxp, minp;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  maxp = a[1] - a[0];
  minp = a[0];
  for (i = 1; i < n; i++) {
    maxp = max(maxp, a[i] - minp);
    minp = min(minp, a[i]);
  }

  cout << maxp << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

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

#include <iostream>
using namespace std;
#define is_uruu main

int is_uruu() {
  int n, maxv = -1000000000;
  cin >> n;
  int R[n];
  for (int i = 0; i < n; i++)
    cin >> R[i];

  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;
#define is_uruu main

int is_uruu() {
  int n, max = -1000000000;
  cin >> n;
  int R[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++) {
      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 <bits/stdc++.h>
using namespace std;
#define lli long long int
#define N 200000

int main() {
  lli ans, n, mini;
  lli a[N];

  cin >> n;

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

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define N 200000

int main() {
  lli ans, n;
  lli a[N];

  cin >> n;

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

  ans = a[1] - a[0];
  for (lli i = 0; i < n - 1; i++) {
    for (lli j = i + 1; j < n; j++) {
      if (ans < a[j] - a[i]) {
        ans = a[j] - a[i];
      }
    }
  }

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

#include <climits>
#include <iostream>
#include <math.h>
#include <vector>

int caluculate(const std::vector<int> &ls) {
  int max = INT_MIN, min = ls.at(0), diff = 0;

  // for(int j=1;i<ls.size();j++){
  //     for(int i=0;i<j-1;i++){
  //         diff=ls.at(j)-ls.at(i);
  //         max=(std::max)(max,diff);
  //     }
  //     //std::cout<<ls.at(i)<<std::endl;
  // }
  for (int j = 1; j < ls.size(); j++) {
    max = (std::max)(max, ls.at(j) - min);
    min = (std::min)(min, ls.at(j));
  }
  return max;
}

int main() {
  int n, x, maxValue;
  std::vector<int> ls(0);

  std::cin >> n;
  while (n--) {
    std::cin >> x;
    ls.push_back(x);
  }
  //?????§??????????¨????
  maxValue = caluculate(ls);

  std::cout << maxValue << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <math.h>
#include <vector>

int caluculate(const std::vector<int> &ls) {
  int max = INT_MIN, diff = 0;

  for (int i = 1; i < ls.size(); i++) {
    for (int j = i; j < ls.size(); j++) {
      diff = ls.at(j) - ls.at(i - 1);
      max = (std::max)(max, diff);
    }
    // std::cout<<ls.at(i)<<std::endl;
  }
  return max;
}

int main() {
  int n, x, maxValue;
  std::vector<int> ls(0);

  std::cin >> n;
  while (n--) {
    std::cin >> x;
    ls.push_back(x);
  }
  //?????§??????????¨????
  maxValue = caluculate(ls);

  std::cout << maxValue << std::endl;
  return 0;
}