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

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

using namespace std;

int solve(vector<int> list) {
  int diff_max;
  int tmp_max = 0;
  int tmp_max_num;
  for (int i = 0; i < list.size() - 1; ++i) {
    if (i == 0) {
      for (int j = 1; j < list.size(); ++j) {
        if (list[j] > tmp_max) {
          tmp_max = list[j];
          tmp_max_num = j;
        }
      }
      diff_max = tmp_max - list[i];
    } else if (i < tmp_max_num) {
      int diff = tmp_max - list[i];
      if (diff > diff_max) {
        diff_max = diff;
      }
    } else {
      tmp_max = 0;
      for (int j = i + 1; j < list.size(); ++j) {
        if (list[j] > tmp_max) {
          tmp_max = list[j];
          tmp_max_num = j;
        }
      }
      int diff = tmp_max - list[i];
      if (diff > diff_max) {
        diff_max = diff;
      }
    }
  }
  return diff_max;
}

int main() {
  int N;
  cin >> N;
  vector<int> v;
  for (int i = 0; i < N; ++i) {
    string n;
    cin >> n;
    v.push_back(stoi(n));
  }
  cout << solve(v) << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

int solve(vector<int> list) {
  int max;
  for (int i = 0; i < list.size(); ++i) {
    for (int j = i + 1; j < list.size(); ++j) {
      int diff = list[j] - list[i];
      if (i == 0 && j == 1) {
        max = diff;
      }
      if (diff > max) {
        max = diff;
      }
    }
  }
  return max;
}

int main() {
  int N;
  cin >> N;
  vector<int> v;
  for (int i = 0; i < N; ++i) {
    string n;
    cin >> n;
    v.push_back(stoi(n));
  }
  cout << solve(v) << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, a, ans = -1e9, amin = 1e9;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (i > 0)
      ans = max(a - amin, ans);
    amin = min(amin, a);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, a[200000], ans = -1e9;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      ans = max(ans, a[j] - a[i]);
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

#include <iostream>
using namespace std;
int main() {
  int n, a[200001] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200001];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ans = a[1] - a[0];
  for (int i = 0; i < n - 1; i++)
    for (int 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 <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<long long> v(N);
  cin >> v[0];
  for (int i = 1; i < N; i++) {
    cin >> v[i];
  }
  long long maxv = -10000000007;
  long long minv = 10000000007;
  for (int i = 0; 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 <cstdio>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

const int MAX = (int)pow(10., 9.);
const int MIN = 1;

int main(int argc, char const *argv[]) {
  int input_size, num, temp, num_min;
  int sub_max = MIN - MAX;
  cin >> input_size;

  for (int i = 0; i < input_size; ++i) {
    cin >> num;
    if (i == 0) {
      num_min = num;
      continue;
    }
    temp = num - num_min;
    if (sub_max < temp)
      sub_max = temp;
    if (num_min > num)
      num_min = num;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main(int argc, char const *argv[]) {
  int input_size, sub_max, temp;
  cin >> input_size;
  int array_num[input_size];

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

  for (int i = 0; i < input_size; ++i) {
    if (i == 0)
      sub_max = array_num[i + 1] - array_num[i];
    for (int j = i + 1; j < input_size; ++j) {
      temp = array_num[j] - array_num[i];
      if (temp > sub_max)
        sub_max = temp;
    }
  }

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

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

#include <stdio.h>
int main() {
  int i, n, j, max, r[200000];
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &r[i]);
  max = r[1] - r[0];
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (r[i] > r[j]) {
        i = j;
        continue;
      }
      if (r[j] - r[i] > max)
        max = r[j] - r[i];
    }
  }
  printf("%d\\n", max);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  int n, a[200000], i, j, max;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  max = a[1] - a[0];
  for (i = n; i > 0; i--) {
    for (j = i - 1; j >= 0; i--) {
      if (a[i] < a[j])
        i = j;
      else if (a[i] - a[j] > max)
        max = a[i] - a[j];
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int main() {
  int number;
  std::cin >> number;

  int price[number];
  for (int i = 0; i < number; i++) {
    std::cin >> price[i];
  }

  int maximum = price[1] - price[0];
  int low_index = 0;
  int high_index = 1;
  for (int i = 0; i < number; i++) {
    if (price[low_index] < price[i]) {
      continue;
    }
    for (int j = i + 1; j < number; j++) {
      if (price[j] < price[high_index] && i < high_index) {
        continue;
      }

      if (maximum < price[j] - price[i]) {
        maximum = price[j] - price[i];
        high_index = j;
        low_index = i;
      }
    }
  }
  std::cout << maximum << std::endl;
}


Error: TLE

Fixed submission:

#include <iostream>

int main() {
  int number;
  std::cin >> number;

  int price[number];
  for (int i = 0; i < number; i++) {
    std::cin >> price[i];
  }

  int maximum = price[1] - price[0];
  int low_index = 0;
  int high_index = 1;
  for (int i = 0; i < number; i++) {
    for (int j = i + 1; j < number; j++) {
      if (maximum < price[j] - price[i]) {
        maximum = price[j] - price[i];
        high_index = j;
        low_index = i;
      }
    }
  }
  std::cout << maximum << std::endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int num, i, j, max_profit, min_val;
  cin >> num;
  int arr[num];
  for (i = 0; i < num; i++) {
    cin >> arr[i];
  }
  max_profit = arr[1] - arr[0];
  min_val = arr[0];
  for (i = 1; i < num; i++) {
    if (max_profit < arr[i] - min_val) {
      max_profit = arr[i] - min_val;
    }
    if (min_val > arr[i]) {
      min_val = arr[i];
    }
  }
  cout << max_profit << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int p[200000] = {0};
int main() {
  int i, n, d = -1000000001, b;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &p[i]);
  b = p[n - 1];
  for (i = n - 2; i >= 0; i--) {
    d = max(d, b - p[i]);
    if (b < p[i])
      b = p[i];
  }
  printf("%d\\n", d);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int p[200000];
int main() {
  int i, j, d = -1000000001, n;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &p[i]);
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      d = max(d, p[j] - p[i]);
    }
  }
  printf("%d\\n", d);
  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;
static const int MAX = 200000;
int main() {
  int n;
  int R[MAX];
  cin >> n;
  if (n <= 1 || n > 200000)
    return 0;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  int max = R[1] - R[0];
  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: Runtime Error

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

int main() {
  int n;
  int minv;
  int maxv = -2000000000;
  int R[MAX];

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

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  int minv;
  int maxv = -2000000000;
  int R[MAX];

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

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
void input(vector<int> &, int &, int &);
int main() {
  vector<int> fx(200001);
  int dec = 0, n, at1, at2, n_max, n_min;
  int ans, p, max, min;
  ans = -1000000001;
  max = -1000000001;
  input(fx, n, max);
  min = fx[0];
  for (int i = 1; i < n; ++i) {
    p = fx[i] - min;
    if (max < p)
      max = p;
    if (min > fx[i])
      min = fx[i];
  }
  cout << max << endl;
  return 0;
}
void input(vector<int> &fx, int &n, int &max) {
  int m;
  m = -1;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> fx[i];
    if (fx[i] == m) {
      max = 0;
      n--;
      i--;
    } else
      m = fx[i];
  }
  return;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
void input(vector<int> &, int &, int &);
int main() {
  vector<int> fx(200001);
  vector<bool> dp(1000000001, true);
  int dec = 0, n;
  int max, p;
  max = -1000000000;
  input(fx, n, max);
  for (int i = 0; i < n; ++i) {
    if (dp[i])
      dp[i] = false;
    else
      continue;
    for (int j = i + 1; j < n; ++j) {
      p = fx[j] - fx[i];
      if (max < p)
        max = p;
    }
  }
  cout << max << endl;
  return 0;
}
void input(vector<int> &fx, int &n, int &max) {
  int m;
  m = -1;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> fx[i];
    if (fx[i] == m) {
      max = 0;
      n--;
      i--;
    } else
      m = fx[i];
  }
  return;
}
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() {
  long long a[200005];
  long long n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long res = a[1] - a[0];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[j] >= a[i]) {
        res = a[j] - a[i] > res ? a[j] - a[i] : res;
      }
    }
  }
  cout << res << endl;

  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 n;

  cin >> n;

  int value[n];

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

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

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

  cout << max << endl;

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

#include <iostream>
using namespace std;

int main() {
  int n, i, r, min = 1000000000, max = -1000000000;

  cin >> n;

  cin >> min;
  for (i = 1; 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 <stdio.h>
int main() {
  int N, i, j;
  long R[200000], m = -2147483648;

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

  for (i = N - 1; i >= 0; --i) {
    for (j = N - 1; j > i; --j) {
      if (R[j] - R[i] > m)
        m = R[j] - R[i];
    }
  }

  printf("%d\\n", m);
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, R[200000], max;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];
  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;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {
  int n;
  int r[200001];
  int maxi = -INF;
  int mini;

  cin >> n;
  FOR(i, 1, n) { cin >> r[i]; }
  mini = r[1];

  FOR(i, 2, n) {
    maxi = max(maxi, r[i] - mini);
    mini = min(mini, r[i]);
  }
  cout << maxi << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {
  int n;
  int r[200001];
  int max = -INF;

  cin >> n;
  FOR(i, 1, n) { cin >> r[i]; }

  FOR(i, 1, n - 1) {
    FOR(t, i + 1, n) {
      if (r[t] - r[i] > max) {
        max = r[t] - r[i];
      }
    }
  }
  cout << max << endl;

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

#include <cmath>
#include <iostream>

int main() {
  int n;
  int x[200000];
  int max = -1e9;
  int min = 1e9;

  std::cin >> n;
  std::cin >> x[0];
  min = x[0];

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


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

int main() {
  int n;
  int x[200000];
  long max = -1e9;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> x[i];
    for (int j = 0; j < i; j++) {
      long temp = x[i] - x[j]; // 仮の最大値
      max = std::max(temp, max);
    }
  }
  std::cout << max << std::endl;
}
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 n;
  int R[200000];
  int dif, maxdif = -999999999;

  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++) {
      dif = R[j] - R[i];
      if (dif > maxdif)
        maxdif = dif;
    }
  }
  cout << maxdif << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
int main(void) {
  std::ios::sync_with_stdio(false);
  long long n;
  std::cin >> n;
  std::vector<long long> ValueOnTime(n, 0);
  for (int i = 0; i < ValueOnTime.size(); ++i) {
    std::cin >> ValueOnTime[i];
  }
  int MaxSub = -1e9, Min = ValueOnTime[0];
  for (int i = 1; i < ValueOnTime.size(); ++i) {
    if (MaxSub < ValueOnTime[i] - Min) {
      MaxSub = ValueOnTime[i] - Min;
    }
    if (Min > ValueOnTime[i]) {
      Min = ValueOnTime[i];
    }
  }
  std::cout << MaxSub << std::endl;
  //	system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
int main(void) {
  std::ios::sync_with_stdio(false);
  long long n;
  std::cin >> n;
  std::vector<long long> ValueOnTime(n, 0);
  for (int i = 0; i < ValueOnTime.size(); ++i) {
    std::cin >> ValueOnTime[i];
  }
  int MaxSub = -1e9;
  for (int i = 1; i < ValueOnTime.size(); ++i) {
    long long criterion =
        *max_element((ValueOnTime.begin() + i), ValueOnTime.end()) -
        *min_element(ValueOnTime.begin(), (ValueOnTime.begin() + i));
    if (MaxSub < criterion) {
      MaxSub = criterion;
    }
  }
  std::cout << MaxSub << std::endl;
  // system("pause");
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a, b[200000], c = -2000000000, d;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  d = b[0];
  for (int i = 0; i < a - 1; i++) {
    if (d > b[i])
      d = b[i];
    if (b[i + 1] - d > c)
      c = b[i + 1] - d;
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int a, b[200000], c = -2000000000;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < a; i++) {
    sort(b, b + i);
    if (i == 0) {
      continue;
    } else if (b[i] - b[0] > c)
      c = b[i] - b[0];
  }
  cout << c << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

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

  int profit = numeric_limits<int>::min();
  for (int j = 0; j < n - 1; ++j) {
    for (int k = j + 1; k < n; ++k) {
      profit = max(profit, R[k] - R[j]);
    }
  }
  cout << profit << endl;

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

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

int main() {
  int n, tmp;
  cin >> n;
  vector<int> rt(n);
  for (int i = 0; i < n; i++) {
    cin >> rt[i];
  }
  int ans = INT_MIN;
  int minv = rt[0];
  for (int j = 1; j < n; j++) {
    ans = (ans >= rt[j] - minv) ? ans : rt[j] - minv;
    minv = (minv <= rt[j]) ? minv : rt[j];
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, tmp;
  cin >> n;
  vector<int> rt(n);
  for (int i = 0; i < n; i++) {
    cin >> rt[i];
  }
  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i < j) {
        tmp = rt[j] - rt[i];
        if (ans < tmp)
          ans = tmp;
      }
    }
  }
  cout << ans << endl;

  return 0;
}