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

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

int main() {
  vector<int> nums;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int l;
    cin >> l;
    nums.push_back(l);
  }
  vector<int> candidits;
  int min = -1;
  for (int i = 0; i < n - 1; ++i) {
    if (min == -1 || min > nums[i]) {
      min = nums[i];
      int max = *max_element(nums.begin() + i + 1, nums.end());
      candidits.push_back(max - min);
    }
  }
  int max = *max_element(candidits.begin(), candidits.end());
  cout << max << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  vector<int> nums;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int l;
    cin >> l;
    nums.push_back(l);
  }
  int min = nums[0];
  vector<int> candidits;
  for (int i = 1; i < n; ++i) {
    if (min > nums[i]) {
      min = nums[i];
      int max = *max_element(nums.begin() + i + 1, nums.end());
      candidits.push_back(max - min);
    }
  }
  int max = *max_element(candidits.begin(), candidits.end());
  cout << max << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 200010
int main() {
  int N, r[MAX], min[MAX], max[MAX];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> r[i];
  }
  min[0] = r[0];
  for (int i = 1; i < N; i++) {
    if (r[i] < min[i - 1])
      min[i] = r[i];
    else
      min[i] = min[i - 1];
  }
  max[N - 1] = r[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    if (r[i] > max[i + 1])
      max[i] = r[i];
    else
      max[i] = max[i + 1];
  }
  int ans = max[1] - min[0];
  for (int i = 0; i < N - 1; i++) {
    if (ans < (max[i + 1] - min[i]))
      ans = max[i + 1] - min[i];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 200000

int main() {
  int N, r[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> r[i];
  }
  long long max = -1000000000000;
  for (int j = N - 1; j >= 0; j--) {
    if (max < r[j])
      for (int i = 0; j > i; i++) {
        if (max < r[j] - r[i])
          max = r[j] - r[i];
      }
  }
  cout << max << endl;

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

#include <iostream>
#include <limits>

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

  int min;
  std::cin >> min;
  int max = std::numeric_limits<int>::min();
  for (int i = 1; i < n; ++i) {
    int val;
    std::cin >> val;
    int tmp = val - min;
    if (tmp > max) {
      max = tmp;
    }
    if (val < min) {
      min = val;
    }
  }
  std::cout << max << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

// テストケースで、速度に対してのものがどのようになるか見てみたいので
// 敢えてベタな方法（速度が遅い）で投稿してみる。

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

  std::vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> v[i];
  }
  int max = v[1] - v[0];
  for (int j = 1; j < n; ++j) {
    for (int i = 0; i < j; ++i) {
      int tmp = v[j] - v[i];
      if (tmp > max) {
        max = tmp;
      }
    }
  }
  std::cout << max << std::endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <limits.h>
#include <stdio.h>

int main(void) {
  int n, i, tmp, tmp2;
  int profit = INT_MIN;
  int min = INT_MAX;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &tmp);
    tmp2 = tmp - min;
    if (tmp2 > profit) {
      profit = tmp2;
    }
    if (tmp < min)
      min = tmp;
  }
  printf("%d\\n", profit);
  return 0;
}


Error: 3

Fixed submission:

#include <limits.h>
#include <stdio.h>

int main(void) {
  int n, i, tmp, tmp2;
  int profit = INT_MIN;
  int min = INT_MAX;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &tmp);
    tmp2 = tmp - min;
    if (tmp2 > profit) {
      profit = tmp2;
    }
    if (tmp < min)
      min = tmp;
  }
  return profit;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n, max, max_diff = -2147483648, diff;
  cin >> n;
  // cout << "n = " << n << endl;
  std::vector<int> a(n + 1), b(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // 最大値のリストb[]
  max = a[n - 1];
  for (int i = n - 1; i > 0; i--) {
    if (i == n - 1) {
      // 何もしない
      b[i] = a[i];
    } else {
      if (a[i] > max) {
        max = a[i];
      }
      b[i] = max;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    diff = b[i + 1] - a[i];
    // cout << "diff = " << diff << endl;
    if (diff > max_diff) {
      max_diff = diff;
      // cout << "new max = " << max << endl;
    }
  }

  cout << max_diff << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n, max = -2147483648, diff;
  cin >> n;
  // cout << "n = " << n << endl;
  std::vector<int> a(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      diff = a[j] - a[i];
      if (diff > max) {
        max = diff;
        // cout << "new max = " << max << endl;
      }
    }
  }

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

#include <iostream>
#define MAX_N 200000
using namespace std;

int main(void) {
  int n;
  int profits[MAX_N + 1];

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

  int _max = -2000000000;
  int _min = profits[0];
  for (int i = 1; i < n; i++) {
    _max = max(_max, profits[i] - _min);
    _min = min(_min, profits[i]);
  }

  cout << _max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX_N 200000
using namespace std;

int calc(int a, int b);

int main(void) {
  int n;
  int profits[MAX_N + 1];

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

  int _max = -2000000000;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int val = calc(profits[i], profits[j]);
      _max = max(_max, val);
    }
  }

  cout << _max << endl;

  return 0;
}

int calc(int a, int b) { return b - a; }
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int main() {

  int n;
  while (std::cin >> n) {
    int *array = new int[n];
    for (int i = 0; i < n; i++) {
      std::cin >> array[i];
    }
    int maxv = array[1] - array[0];
    int minj;
    if (maxv < 0) {
      minj = array[1];
    } else {
      minj = array[0];
    }
    for (int j = 2; j < n; j++) {
      if (minj > array[j - 1]) {
        minj = array[j - 1];
      }
      if (maxv < array[j] - minj) {
        maxv = array[j] - minj;
      }
    }
    std::cout << maxv << std::endl;
    delete[] array;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

void call(int n) {
  int max = -1000000000;
  int *array = new int[n];
  for (int i = 0; i < n; i++) {
    std::cin >> array[i];
  }
  for (int j = 1; j < n; j++) {
    for (int i = 0; i < j; i++) {
      if (max < array[j] - array[i]) {
        max = array[j] - array[i];
      }
    }
  }
  std::cout << max << std::endl;
}

int main() {

  int n;
  while (std::cin >> n) {
    call(n);
  }
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long MaxProfit(int n, long long *Rt) {
  long long max = LLONG_MIN;
  long long MinRt = Rt[0];
  for (int j = 1; j < n; j++) {
    long long tmp = Rt[j] - MinRt;
    if (max < tmp)
      max = tmp;
    if (MinRt > Rt[j])
      MinRt = Rt[j];
  }
  return max;
}

int main() {

  int n;
  cin >> n;
  long long Rt[n];
  long long max;
  for (int i = 0; i < n; i++)
    cin >> Rt[i];
  max = MaxProfit(n, Rt);
  cout << max << endl;
  return 0;
};

Error: TLE

Fixed submission:

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

long long MaxProfit(int n, long long *Rt) {
  long long max = LLONG_MIN;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long long tmp = Rt[j] - Rt[i];
      if (max < tmp)
        max = tmp;
    }
  }
  return max;
}

int main() {

  int n;
  cin >> n;
  long long Rt[n];
  long long max;
  for (int i = 0; i < n; i++)
    cin >> Rt[i];
  max = MaxProfit(n, Rt);
  cout << max << endl;
  return 0;
};
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, f, n) for (int i = (f); i <= (n); ++i)
#define outl(x) cout << (x) << '\\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template <class A, class B> inline bool chmax(A &a, B b) {
  return b > a ? a = b, 1 : 0;
}
template <class A, class B> inline bool chmin(A &a, B b) {
  return b < a ? a = b, 1 : 0;
}
inline int updiv(int a, int b) { return ((a + b - 1) / b); }

int main() {
  FAST();
  int N, a;
  int ans = INT_MIN;
  int minv = INT_MAX;
  cin >> N;
  rep(i, N) {
    cin >> a;
    chmax(ans, a - minv);
    chmin(minv, a);
  }
  outl(ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, f, n) for (int i = (f); i <= (n); ++i)
#define outl(x) cout << (x) << '\\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template <class A, class B> inline bool chmax(A &a, B b) {
  return b > a ? a = b, 1 : 0;
}
template <class A, class B> inline bool chmin(A &a, B b) {
  return b < a ? a = b, 1 : 0;
}
inline int updiv(int a, int b) { return ((a + b - 1) / b); }

int N, a[200002];

int main() {
  FAST();
  cin >> N;
  rep(i, N) cin >> a[i];

  int maxv = INT_MIN;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      chmax(maxv, a[j] - a[i]);
    }
  }

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

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

using namespace std;

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

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

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

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
static const int N = 10000;

int main() {

  int a[N], n;
  cin >> n;

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

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

Error: TLE

Fixed submission:

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

#include <iostream>
#include <vector>

class MaximumProfit {
private:
  std::vector<int> dataSet;

public:
  MaximumProfit(std::vector<int> vec);
  int getProfit();
  int getMaximum(int x, int y);
  int getMinimum(int x, int y);
};

MaximumProfit::MaximumProfit(std::vector<int> vec) { this->dataSet = vec; }

/*
int MaximumProfit::getProfit(){
    int maxv = dataSet[1] - dataSet[0];
    for(int j = 1;j < dataSet.size();j++){
        for(int i = 0;i <= j - 1;i++){
            if(maxv < (dataSet[j] - dataSet[i])){
                maxv = dataSet[j] - dataSet[i];
            }
        }
    }
    return maxv;
}
*/

int MaximumProfit::getProfit() {
  int maxv = dataSet[1] - dataSet[0];
  int minv = dataSet[0];
  for (int j = 1; j <= dataSet.size() - 1; j++) {
    maxv = getMaximum(maxv, dataSet[j] - minv);
    minv = getMinimum(minv, dataSet[j]);
  }
  return maxv;
}

int MaximumProfit::getMaximum(int x, int y) {
  if (x > y) {
    return x;
  }
  return y;
}

int MaximumProfit::getMinimum(int x, int y) {
  if (x < y) {
    return x;
  }
  return y;
}

int main() {

  int line;

  std::cin >> line;

  std::vector<int> vec;

  for (int i = 0; i < line; i++) {
    int input;
    std::cin >> input;
    vec.push_back(input);
  }

  MaximumProfit mp = MaximumProfit(vec);

  std::cout << mp.getProfit() << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

class MaximumProfit {
private:
  std::vector<int> dataSet;

public:
  MaximumProfit(std::vector<int> vec);
  int getProfit();
};

MaximumProfit::MaximumProfit(std::vector<int> vec) { this->dataSet = vec; }

int MaximumProfit::getProfit() {
  int maxv = dataSet[1] - dataSet[0];
  for (int j = 1; j < dataSet.size(); j++) {
    for (int i = 0; i <= j - 1; i++) {
      if (maxv < (dataSet[j] - dataSet[i])) {
        maxv = dataSet[j] - dataSet[i];
      }
    }
  }
  return maxv;
}

int main() {

  int line;

  std::cin >> line;

  std::vector<int> vec;

  for (int i = 0; i < line; i++) {
    int input;
    std::cin >> input;
    vec.push_back(input);
  }

  MaximumProfit mp = MaximumProfit(vec);

  std::cout << mp.getProfit() << std::endl;

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

#include <iostream>

using namespace std;

int main() {
  int n;
  int r[200000];
  int max;
  int min;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

  scanf("%d", &n);
  for (int j = 0; j < n; j++) {
    scanf("%d", &r[j]);
    for (int i = 0; i < j; i++) {
      int tmp = r[j] - r[i];
      if (tmp > max) {
        max = r[j] - r[i];
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

/*************************************************************************
    > File Name: 1_1_D-2.cpp
    > Author: Wqr_
    > Mail: xueduanwei@126.com
    > Created Time: Mon Oct 22 19:09:53 2018
 ************************************************************************/

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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


Error: TLE

Fixed submission:

/*************************************************************************
    > File Name: 1_1_D.cpp
    > Author: Wqr_
    > Mail: xueduanwei@126.com
    > Created Time: Mon Oct 22 18:20:20 2018
 ************************************************************************/

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  int in[n];
  int max = -2000000000;
  int min[n];
  for (int i = 0; i < n; i++) {
    cin >> in[i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      int cha;
      cha = in[i] - in[j];
      if (cha > max) {
        min[i] = cha;
        max = min[i];
      }
    }
    max = -2000000000;
  }
  for (int i = 1; i < n; i++) {
    if (min[i] > max) {
      max = min[i];
    }
  }
  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 R[MAX], n;

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

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

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

  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 <iostream>

using namespace std;

static const int MAX = 200000;

int main() {
  int num = 0, maxProfit = -1000000000;
  int value[MAX] = {0};

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

  int minValue = value[0];

  for (int i = 1; i < num; i++) {
    maxProfit = max(maxProfit, value[i] - minValue);
    minValue = min(minValue, value[i]);
  }

  cout << maxProfit << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

static const int MAX = 200000;

int main() {
  int num = 0, maxProfit = -1000000000;
  int value[MAX] = {0};

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

  for (int i = 0; i < num; i++) {
    for (int j = i + 1; j < num; j++) {
      maxProfit = max(maxProfit, value[j] - value[i]);
    }
  }

  cout << maxProfit << 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 a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int maxa = -10e09;
  int minv = a[0];

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

  cout << maxa << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

  int maxa = -10e09;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      maxa = max(maxa, a[j] - a[i]); // 大きい方を
    }
  }
  cout << maxa << endl;

  return 0;
}

// i<jとなるiとj全ても組み合わせに対するRj-Riを調べてる。
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, i, j, r[200000];
  long long v, max = -1000000000, min = 1000000000;

  // get number
  cin >> n;

  // get max1,2&min1,2
  for (i = 0; i < n; i++) {
    cin >> r[i];
  }

  min = r[0];

  for (i = 1; i < n; i++) {
    v = r[i] - min;
    if (v >= max) {
      max = v;
    }
    if (r[i] <= min) {
      min = r[i];
    }
  }

  cout << max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, i, j, r[200000];
  long long v, max = -1000000000;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> r[i];
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      v = r[j] - r[i];
      if (v > max) {
        max = v;
      }
    }
  }
  cout << max << endl;

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

#include <algorithm>
#include <cstdio>

using namespace std;

const int kMax = 200000 + 5;

int num[kMax];

int main() {
  int n, res, min_num;
  scanf("%d", &n);
  scanf("%d", &num[0]);
  min_num = num[0];
  for (int i = 1; i < n; ++i) {
    scanf("%d", &num[i]);
    if (i == 1)
      res = num[1] - min_num;
    else {
      res = max(res, num[i] - min_num);
    }
    min_num = min(min_num, num[i]);
  }
  printf("%d\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

const int kMax = 200000 + 5;

int num[kMax];

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

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

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

  vector<int> v(n);

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


Error: TLE

Fixed submission:

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

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

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

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