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

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

const int INF = 1 << 30;
int n, r[200000];

void solve() {
  int ans = -INF, pos = 0;
  for (int i = 1; i < n; i++) {
    ans = max(ans, r[i] - r[pos]);
    if (r[pos] > r[i]) {
      pos = i;
    }
  }
  cout << ans << endl;
}

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

Error: 0

Fixed submission:

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

const int INF = 1 << 29;
int n, r[200000];

void solve() {
  int ans = -INF, pos = 0;
  for (int i = 1; i < n; i++) {
    ans = max(ans, r[i] - r[pos]);
    if (r[pos] > r[i]) {
      pos = i;
    }
  }
  assert(ans != -INF);
  cout << ans << endl;
}

int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++)
      cin >> r[i];
    solve();
  }
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<double> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  double max_profit = -1000000000;
  double min_r = r[0];
  for (int i = 1; i < n; i++) {
    min_r = min(min_r, r[i - 1]);
    max_profit = max(max_profit, r[i] - min_r);
  }
  cout << setprecision(10) << max_profit << endl;
}


Error: TLE

Fixed submission:

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

using namespace std;

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

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 2000000;
int main(int argc, char *argv[]) {
  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 <cstdio>
#include <iostream>
using namespace std;

const int MAXN = 200001;

int main(int argc, char *argv[]) {
  int n;
  int A[MAXN];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  int max = -1000000000;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int temp = A[j] - A[i];
      if (temp > max) {
        max = temp;
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

int main() {
  long long n, i, max = -9999999999, Rmin = 9999999999;
  cin >> n;
  long long R[n];
  for (i = 0; i < n; ++i) {
    cin >> R[i];
    if (i > 0 && R[i] - Rmin > max) {
      max = R[i] - Rmin;
    }
    if (R[i] < Rmin)
      Rmin = R[i];
  }
  cout << max << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int main() {
  long long n, i, max = -9999999999, j;
  cin >> n;
  long long R[n];
  for (i = 0; i < n; ++i) {
    cin >> R[i];
    for (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 main() {
  int n;
  cin >> n;
  int *r;
  r = new int[n];
  if (r == NULL) {
    cout << "Error!" << endl;
    return -1;
  }
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> r[i];
  }
  int max = r[1] - r[0];
  for (j = n - 1; j >= 0; j--) {
    if (r[j] <= max)
      continue;
    for (i = j - 1; i >= 0; i--) {
      if (r[j] - r[i] > max)
        max = r[j] - r[i];
    }
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int *r;
  r = new int[n];
  if (r == NULL) {
    cout << "Error!" << endl;
    return -1;
  }
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> r[i];
  }
  int max = r[1] - r[0];
  for (i = 0; i < n; i++) {
    for (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 <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

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

  int n;
  cin >> n;
  vector<ll> R(n);
  int max = INT_MIN;
  int min = INT_MAX;
  REP(i, n) {
    cin >> R[i];
    if (max < R[i] - min)
      max = R[i] - min;
    if (min > R[i])
      min = R[i];
  }
  cout << max << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

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

  int n;
  cin >> n;
  vector<ll> R(n);
  REP(i, n) cin >> R[i];
  ll res = R[1] - R[0];
  REP(i, n) FOR(j, i + 1, n) { res = max(res, R[j] - R[i]); }
  cout << res << "\\n";

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

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n, buf, minv, maxv;
  cin >> n;

  cin >> minv;
  cin >> buf;
  maxv = buf - minv;
  minv = std::min(minv, buf);

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

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int max_prof(int r[], size_t s) {
  int t, max = r[1] - r[0];
  for (size_t i = 0; i < s; i++) {
    for (size_t j = i + 1; j < s; j++) {
      if ((t = r[j] - r[i]) > max)
        max = t;
    }
  }
  return max;
}

int main() {
  static_assert(1000000000 < INT_MAX, "");
  int n, r[200000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  cout << max_prof(r, n) << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, A[200000];

int solve(void) {
  auto maxp = A[1] - A[0];
  auto minr = min(A[0], A[1]);
  for (auto i = 2; i < N; i++) {
    maxp = max(maxp, A[i] - minr);
    minr = min(minr, A[i]);
  }
  return maxp;
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  cout << solve() << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int N, A[200000];

int solve(void) {
  int maxp = 1 - 1000000000;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      maxp = max(maxp, A[j] - A[i]);
    }
  }
  return maxp;
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  cout << solve() << endl;

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

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;

class c_fx {
private:
  int n;
  long t[200000];

public:
  void f_fx();
};

void c_fx::f_fx() {
  long i, j;
  long max_sa;
  long min_x;

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

  max_sa = t[1] - t[0];
  min_x = t[0];

  for (j = 1; j < n; ++j) {
    if (max_sa < t[j] - min_x)
      max_sa = t[j] - min_x;
    if (min_x > t[j])
      min_x = t[j];
  }

  cout << max_sa << endl;
}

int main() {
  c_fx fx01;

  fx01.f_fx();

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;

class c_fx {
private:
  int n;
  long t[200000];

public:
  void f_fx();
};

void c_fx::f_fx() {
  long i, j;
  long max_sa;

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

  max_sa = t[n - 1] - t[n - 2];

  for (i = n - 1; i >= 0; --i) {
    for (j = i - 1; j >= 0; --j) {
      if (max_sa < t[i] - t[j]) {
        max_sa = t[i] - t[j];
      }
    }
  }

  cout << max_sa << endl;
}

int main() {
  c_fx fx01;

  fx01.f_fx();

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

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

int maxProfit(vector<int> &A, int N) {
  int maxProfit = -2000000000;
  int minR = A[0];
  for (int i = 1; i < N; i++) {
    maxProfit = max(maxProfit, A[i] - minR);
    minR = min(minR, A[i]);
  }
  return maxProfit;
}

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

  int sw = maxProfit(A, N);
  cout << sw << endl;
}


Error: 0

Fixed submission:

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

int maxProfit(vector<int> &A, int N) {
  int maxProfit = 10 ^ 12 * (-1);
  int minR = A[0];
  for (int i = 1; i < N; i++) {
    maxProfit = max(maxProfit, A[i] - minR);
    minR = min(minR, A[i]);
  }
  return maxProfit;
}

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

  int sw = maxProfit(A, N);
  cout << sw << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long

#define MAX_N 200000

using namespace std;

signed main() {
  int n, r[MAX_N];

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

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

  cout << maxv << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long

#define MAX_N 200000

using namespace std;

signed main() {
  int n;
  int r[MAX_N];

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

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

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

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

#include <cstdio>
int d[200000 + 2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &d[i]);

  int maxv = d[1] - d[0]; // 2 <= n < 200000
  int minv = d[0]; // Store a mininum value and get diff value with next value
                   // after next mininum
  for (int i = 1; i < n; i++) {
    maxv = (d[i] - minv > maxv) ? (d[i] - minv) : maxv;
    minv = (minv > d[i]) ? d[i] : minv;
  } //	local save history value

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
int d[200000 + 2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &d[i]);

  int maxv = -1000000001; //(n>=2)?d[1]-d[0]:-1;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (maxv < d[j] - d[i])
        maxv = d[j] - d[i];
    }
  }

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

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%lld", &v[i]);
  }
  ll ans = -INF;
  ll mn = v[0];
  for (int i = 1; i < n; ++i) {
    ans = max(ans, v[i] - mn);
    mn = min(mn, v[i]);
  }
  fprintf(stdout, "%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main() {
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%lld", &v[i]);
  }
  ll ans = -INF;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ans = max(ans, v[j] - v[i]);
    }
  }
  fprintf(stdout, "%lld\\n", ans);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <stdint.h>
#include <string>
#include <vector>

std::vector<int> nums;

int main(int argc, char *argv[]) {
  int N;

  std::cin >> N;

  for (int i = 0; i < N; i++) {
    int v;
    std::cin >> v;
    nums.push_back(v);
  }

  int maxDiff = -1000000000;
  int kmax = nums[N - 1] - 1;
  for (int k = N - 1; k >= 0; k--) {
    if (nums[k] <= kmax)
      continue;
    kmax = nums[k];
    for (int p = k - 1; p >= 0; p--) {
      if (nums[k] - nums[p] > maxDiff) {
        maxDiff = nums[k] - nums[p];
      }
    }
  }
  std::cout << maxDiff << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <stdint.h>
#include <string>
#include <vector>

std::vector<int> nums;

int main(int argc, char *argv[]) {
  int N;

  std::cin >> N;

  for (int i = 0; i < N; i++) {
    int v;
    std::cin >> v;
    nums.push_back(v);
  }

  int maxDiff = -1000000000;
  for (int k = N - 1; k >= 0; k--) {
    for (int p = k - 1; p >= 0; p--) {
      if (nums[k] - nums[p] > maxDiff) {
        maxDiff = nums[k] - nums[p];
      }
    }
  }
  std::cout << maxDiff << std::endl;

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
typedef int TInt;
typedef std::vector<TInt> TList;
TList Input() {
  int Count;
  std::cin >> Count;
  TList List(Count);
  for (int i = 0; i < Count; ++i) {
    std::cin >> List[i];
  }
  return List;
}
int main() {
  TList A = Input();
  assert(A.size() >= 2);
  int Profit = A[1] - A[0];
  int i = 0;
  while (i < A.size()) {
    int j;
    for (j = i + 1; j < A.size(); ++j) {
      int Diff = A[j] - A[i];
      if (Diff < 0) {
        i = j;
        break;
      }
      if (Diff > Profit)
        Profit = Diff;
    }
    if (j == A.size())
      break;
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
typedef int TInt;
typedef std::vector<TInt> TList;
TList Input() {
  int Count;
  std::cin >> Count;
  TList List(Count);
  for (int i = 0; i < Count; ++i) {
    std::cin >> List[i];
  }
  return List;
}
int main() {
  TList A = Input();
  assert(A.size() >= 2);
  int Profit = A[1] - A[0];
  int i = 0;
  while (i < A.size()) {
    int j = i + 1;
    for (int j = i + 1; j < A.size(); ++j) {
      int Diff = A[j] - A[i];
      if (Diff < 0) {
        i = j;
        break;
      }
      if (Diff > Profit)
        Profit = Diff;
    }
    if (j == A.size())
      break;
  }

  std::cout << Profit << std::endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = static_cast<int>(2e+5);
static const int MIN = static_cast<int>(1e+9);

int main() {
  int R[MAX], n;

  cin >> n;

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

  int maxv = 1 - MIN;
  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 a[200010];

int main() {
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int maxn = -1000000001;
  int minv = a[0];
  for (i = 1; i < n; i++) {
    maxn = max(a[i] - minv, maxn);
    minv = min(a[i], minv);
  }
  cout << maxn << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int a[200010];

int main() {
  long long int i, j, maxn = -20000000000000, n, temp;
  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++) {
      temp = a[j] - a[i];
      maxn = max(maxn, temp);
    }
  cout << maxn << 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 long int> r(n);
  for (int i = 0; i < n; i++)
    cin >> r[i];

  long long int Max = -10000000000;
  long long int Min = r[0];

  for (int i = 1; i < n; i++) {
    if (r[i] - Min > Max)
      Max = r[i] - Min;
    if (Min > r[i])
      Min = r[i];
  }

  cout << Max << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  long long int MAX = -10000000000;

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

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

  cout << MAX << endl;

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

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

int a[200001];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mi = a[0];
  int ans = -1000000001;
  for (int i = 1; i < n; i++) {
    mi = min(mi, a[i - 1]);
    ans = max(ans, a[i] - mi);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

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

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

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

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

#define INF pow(10, 10)

using namespace std;

int main() {
  int cnt = 0;
  vector<int> R;
  int maxSub = -INF;
  int max = -INF;
  int min = INF;
  int n;
  cin >> cnt;
  for (int j = 0; j < cnt; j++) {
    cin >> n;
    R.push_back(n);

    int sub = n - min;
    if (sub > maxSub) {
      maxSub = sub;
    }

    // first time
    if (j == 0) {
      min = max = n;
      continue;
    }

    if (min > n) {
      min = n;
    }
    if (max < n) {
      max = n;
    }

    // for(int i=0;i<R.size()-1;i++){
    // int sub = R.back() - R[i];
    // if(sub > Max){
    //    Max = sub;
    //}
    //    }

    // cout << "M: " << Max << endl;
  }

  cout << maxSub << endl;
}


Error: TLE

Fixed submission:

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

#define INF pow(10, 10)

using namespace std;

int main() {
  int cnt = 0;
  vector<int> R;
  R.push_back(INF);
  int Max = -INF;
  int n;
  cin >> cnt;
  for (int j = 0; j < cnt; j++) {
    cin >> n;
    R.push_back(n);

    for (int i = 0; i < R.size() - 1; i++) {
      int sub = R.back() - R[i];
      if (sub > Max) {
        Max = sub;
      }
    }

    // cout << "M: " << Max << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <time.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int maxa = -2000000000;
  int minv;
  cin >> minv;
  int temp;

  for (int i = 1; i < n; i++) {
    cin >> temp;
    maxa = max(maxa, temp - minv);
    minv = min(minv, temp);
  }
  cout << maxa << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <time.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int R[200010];
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  int max = R[1] - R[0];
  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 <iostream>
#include <string>
using namespace std;

int main() {
  int n_col;
  cin >> n_col;
  int *p_currency;
  p_currency = new int[n_col];

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

  int max_profit = p_currency[1] - p_currency[0];
  // cout << "first max_profit: " << max_profit << "\\n";
  int min = p_currency[0];
  // cout << "first min: " << min << "\\n";

  for (int i = 1; i < n_col; i++) {
    int diff = p_currency[i] - min;
    if (max_profit < diff) {
      max_profit = diff;
    }

    if (min > p_currency[i]) {
      min = p_currency[i];
    }

    // cout << "min:" << min << "\\n";
    // cout << "max_profit: " << max_profit << "\\n";
  }
  cout << max_profit << "\\n";
  delete[] p_currency;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n_col;
  cin >> n_col;
  int *p_currency;
  p_currency = new int[n_col];
  int max_profit = -10000000000000;
  for (int i = 0; i < n_col; i++) {
    cin >> p_currency[i];

    for (int j = 0; j < i; j++) {
      int diff = p_currency[i] - p_currency[j];

      if (diff > max_profit) {
        max_profit = diff;
      }
    }
  }
  cout << max_profit << "\\n";
  delete[] p_currency;
  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(void) {
  int N, R[MAX];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> R[i];

  int maxv = -1000000000;
  int minr = R[0];

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

  cout << maxv << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N, R[MAX];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> R[i];

  int maxv = -1000000000;

  for (int j = 1; j < N; j++)
    for (int i = 0; i < j; i++)
      maxv = max(maxv, R[j] - R[i]);

  cout << maxv << endl;

  return 0;
}