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

#include <stdio.h>

int main() {

  int n, i, j;
  int r, rmax, rmin;

  scanf("%d", &n);

  int R[200000];

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

  rmax = -1000000000;
  rmin = R[0];

  for (i = 1; i < n; i++) {
    int x;
    x = R[i] - rmin;
    if (x > rmax)
      rmax = x;

    if (rmin > R[i])
      rmin = R[i];
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
static const int MAX = 200000;

int main() {

  int n, i, j;
  int r, rmax;

  scanf("%d", &n);

  int R[MAX];

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

  rmax = -1000000000;

  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (j > i) {
        r = R[j] - R[i];
        if (r > rmax)
          rmax = r;
      }
    }
  }

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ma, mi;
  cin >> n;
  long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  mi = a[0];
  ma = a[1] - a[0];
  for (int i = 1; i < n; i++) {
    if (mi > a[i])
      mi = a[i];
    else if (ma < a[i] - mi)
      ma = a[i] - mi;
  }
  cout << ma << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ma;
  cin >> n;
  long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ma = a[1] - a[0];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (ma < a[j] - a[i])
        ma = a[j] - a[i];
    }
  }
  cout << ma << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX_VALUE = 200000;
int R[MAX_VALUE];

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

  int min_val = R[0];
  int max_val = -1000000001;
  for (int j = 1; j < n; j++) {
    max_val = max(max_val, R[j] - min_val);
    min_val = min(min_val, R[j]);
  }
  printf("%d\\n", max_val);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX_VALUE = 200000;
int R[MAX_VALUE];

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

  int max_income = -1000000001;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
      max_income = max(max_income, R[j] - R[i]);

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

#include <algorithm>
#include <iostream>

using namespace std;

int n;
const int MAX_N = 200000;
int R[MAX_N];

int main() {

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  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>

using namespace std;

int n;
const int MAX_N = 200000;
int R[MAX_N];

int main() {

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

#include <bits/stdc++.h>
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 <bits/stdc++.h>
using namespace std;
int main() {
  int r[200001];
  int n;
  cin >> n;
  int max = -1000000001;
  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

#include <iostream>
#include <limits.h>
using namespace std;

int main(void) {
  int N, *A, dif, max, minID, maxID;

  cin >> N;

  A = new int[N];
  maxID = 1;
  minID = 0;

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

  max = A[maxID] - A[minID];
  if (A[maxID] < A[minID]) {
    minID = 1;
  }

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

    if (minID > maxID || A[maxID] < A[i]) {
      maxID = i;
      if (max < A[maxID] - A[minID] && maxID > minID)
        max = A[maxID] - A[minID];
    }
    if (A[minID] > A[i]) {
      minID = i;
    }
  }

  cout << max << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
using namespace std;

int main(void) {
  int N, *A, dif, max;

  cin >> N;

  A = new int[N];

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

    if (i == 1) {
      max = A[i] - A[i - 1];
    } else if (i > 0) {
      for (int j = i - 1; j > -1; j--) {
        dif = A[i] - A[j];
        if (dif > max)
          max = dif;
      }
    }
  }

  cout << max << "\\n";

  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 j = 1; j < n; j++) {
    if (maxv < R[j] - minv) {
      maxv = R[j] - minv;
    }
    if (minv > R[j]) {
      minv = R[j];
    }
  }
  cout << maxv << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

#include <iostream>

using namespace std;

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

  int R[n];
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  int max_R = 1;
  int max_val = 1 - 1e9;

  for (int i = n - 2; i >= 0; i--) {
    if (R[i + 1] > max_R)
      max_R = R[i + 1];
    int val = max_R - R[i];
    if (val > max_val)
      max_val = val;
  }

  cout << max_val << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  int R[n];
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }
  int max_val = -1000000000;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int val = R[j] - R[i];
      if (val > max_val)
        max_val = val;
    }
  }
  cout << max_val << endl;

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

#include <algorithm>
#include <iostream>

int main() {
  int n, maxv = -2000000000, minv;
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::cin >> n;
  int rt[n];
  for (int i = 0; i < n; ++i) {
    std::cin >> rt[i];
  }
  minv = rt[0];
  for (int i = 1; i < n; ++i) {
    maxv = std::max(maxv, rt[i] - minv);
    minv = std::min(minv, rt[i]);
  }
  std::cout << maxv << '\\n';
}


Error: TLE

Fixed submission:

#include <iostream>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n, max = -1000000000, x;
  std::cin >> n;
  int rt[n];
  for (int i = 0; i < n; i++) {
    std::cin >> rt[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      x = rt[j] - rt[i];
      if (max < x) {
        max = x;
      }
    }
  }
  std::cout << max << '\\n';
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;
int main(void) {
  int n;
  long long r[200000], maxv, minv;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%ld", &r[i]);
  }
  maxv = r[1] - r[0];
  minv = r[0];
  for (int i = 1; i < n; i++) {
    if (minv > r[i - 1])
      minv = r[i - 1];
    if (maxv < r[i] - minv)
      maxv = r[i] - minv;
  }
  printf("%d\\n", maxv);
  return 0;
}

Error: TLE

Fixed submission:

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

// include "stdafx.h"
#include <algorithm>
#include <iostream>
using namespace std;
void maximumProfit();

int main() {
  maximumProfit();
  return 0;
}

void maximumProfit() {
  const int MAX = 200000;
  int R[MAX];
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> R[i];
  }
  int mi = R[0];
  int ma = -2000000000;
  for (int i = 1; i < N; i++) {
    ma = max(ma, R[i] - mi);
    mi = min(mi, R[i]);
  }

  cout << ma << endl;
}

Error: TLE

Fixed submission:

// #include "stdafx.h"
#include <iostream>
using namespace std;
static const int MAX = 200000;
int main() {
  int R[MAX];
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> R[i];
  }
  int max = -999999999;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (max < R[j] - R[i]) {
        max = R[j] - R[i];
      }
    }
  }

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

#include <iostream>
using namespace std;

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

  int min = r[0], maxv = r[1] - r[0];

  for (int i = 1; i < n; i++) {
    maxv = (maxv < r[i] - min) ? r[i] - min : maxv;
    min = (min > r[i]) ? r[i] : min;
  }
  cout << maxv << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int *r = new int[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 = n - 1 - i; j > 0; j--)
      max = (max < r[i + j] - r[i]) ? r[i + j] - r[i] : max;

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

#include <iostream>
using namespace std;

string list[32];
int res = 0;
int sa[1000];

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

string list[32];
int res = 0;
int sa[1000];

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

#include <iostream>
using namespace std;
int R[200000];
int num;
int main() {
  // cout << "????????\\???????????¶??´??????" << endl;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  int minv = R[0];
  for (int j = 1; j < num; j++) {
    if (maxv < R[j] - minv) {
      maxv = R[j] - minv;
    }
    if (minv > R[j]) {
      minv = R[j];
    }
    /*	for (int i = 0; i < j; i++)
            {
                    if (maxv < R[j] - R[i])
                    {
                            maxv = R[j] - R[i];
                    }
            }*/
  }
  cout << maxv << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int R[2000];
int num;
int main() {
  // cout << "????????\\???????????¶??´??????" << endl;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  int minv = R[0];
  for (int j = 1; j < num; j++) {
    if (maxv < R[j] - minv) {
      maxv = R[j] - minv;
    }
    if (minv > R[j]) {
      minv = R[j];
    }
    /*	for (int i = 0; i < j; i++)
            {
                    if (maxv < R[j] - R[i])
                    {
                            maxv = R[j] - R[i];
                    }
            }*/
  }
  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

int main() {
  using namespace std;
  const int MAX = 200000;
  int R[MAX], n;

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

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

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

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

int main() {
  using namespace std;
  const int MAX = 20000000;
  int R[MAX], n;

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

  int maxv = -1000;
  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 <iostream>
using namespace std;
#include <algorithm>

int main(void) {

  int n;
  cin >> n;

  int price[200000];
  int maxi;
  int mini;

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

  mini = price[0];
  maxi = price[1] - mini;

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

    maxi = max(maxi, price[i] - mini);
    if (mini > price[i])
      mini = price[i];
  }

  cout << maxi << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {

  int n;
  cin >> n;

  int price[200000];
  int maxi;

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

  maxi = price[1] - price[0];

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

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

#include <cstdio>
#define MAX 200000
#define MIN -999999999

int maximumProfit(int *profit, int n);

int main() {
  int n;
  scanf("%d", &n);

  int profit[MAX];
  for (int i = 0; i < n; i++)
    scanf("%d", &profit[i]);

  int max = maximumProfit(profit, n);

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

  return 0;
}

int maximumProfit(int *profit, int n) {
  int max = MIN, min = profit[0];
  for (int i = 1; i < n; i++) {
    max = max > profit[i] - min ? max : profit[i] - min;
    min = min < profit[i] ? min : profit[i];
  }
  return max;
}

Error: TLE

Fixed submission:

#include <cstdio>
#define MAX 200000
#define MIN -999999999

int main() {
  int n;
  scanf("%d", &n);

  int profit[MAX];
  for (int i = 0; i < n; i++)
    scanf("%d", &profit[i]);

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

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

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

#include <iostream>
using namespace std;
int main(void) {
  int n, m, r, ans = -1000000001;
  cin >> n;
  cin >> m;
  for (int i = 1; i < n; i++) {
    cin >> r;
    ans = max(ans, r - m);
    m = min(m, r);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#include <iostream>
#include <string.h>
using namespace std;
long a[200001];
int main() {
  int n;
  int i, j;
  int max;
  int small;
  memset(a, 0, sizeof(a));
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  max = a[1] - a[0];
  small = a[0];
  for (i = 1; i < n; i++) {
    if (max < a[i] - small) {
      max = a[i] - small;
    }
    if (small > a[i]) {
      small = a[i];
    }
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;
long a[200001];
int main() {
  long n;
  int i, j;
  long max;
  memset(a, 0, sizeof(a));
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  max = a[1] - a[0];
  for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
      if (max < a[j] - a[i]) {
        max = a[j] - a[i];
      }
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
int main(int const argc, char const **argv) {
  int n;
  std::cin >> n;
  std::vector<int> vec(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> vec[i];
    // scanf("%d\\n", & vec[i]);
  }
  int min = vec[0];
  int max = -2147483647;
  for (int i = 1; i < n; ++i) {
    max = std::max(max, vec[i] - min);
    min = std::min(min, vec[i]);
  }
  std::cout << max << std::endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>
int main(int const argc, char const **argv) {
  int n;
  std::cin >> n;
  std::vector<int> vec(n);
  for (int i = 0; i < n; ++i) {
    // std::cin >> vec[i];
    scanf("%d\\n", &vec[i]);
  }

  int max = -2147483648;
  int r;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      r = vec[j] - vec[i];
      if (max < r) {
        max = r;
      }
    }
  }
  std::cout << max << std::endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

// #define TEST

using namespace std;

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

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

  int profit = r[1] - r[0];
  int r_min = r[0];

#ifdef TEST
  cout << "default : "
       << "r_min = " << r_min << " : "
       << "profit = " << profit << " : \\n";
#endif

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

#ifdef TEST
    cout << "i = " << i << " : ";
#endif

    if (profit < r[i] - r_min) {
      profit = r[i] - r_min;

#ifdef TEST
      cout << "profit = " << profit << " : ";
#endif
    }

    if (r_min > r[i]) {
      r_min = r[i];
#ifdef TEST
      cout << "r_min = " << r_min << " : ";
#endif
    }
#ifdef TEST
    cout << endl;
#endif
  }

#ifdef TEST
  cout << "ans = ";
#endif
  cout << profit << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

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

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

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

  cout << profit << endl;

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

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

int timeSerialDiff(vector<int> &v) {
  int size = v.size();
  if (size < 2) {
    return 0;
  }
  int i, max, min, cnt = 0;
  min = v[0];
  max = v[1] - v[0];
  for (i = 1; i < size; i++) {
    int diff = v[i] - min;
    max = max < diff ? diff : max;
    min = min > v[i] ? v[i] : min;
  }
  return max;
}
int main() {
  int n, i, x, min, max;
  vector<int> v;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  cout << timeSerialDiff(v) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int timeSerialDiff(vector<int> &v) {
  int size = v.size(), i, j, max, cnt = 0;
  for (i = 0; i < size; i++) {
    int start = v[i];
    for (j = i + 1; j < size; j++) {
      int diff = v[j] - v[i];
      max = (cnt == 0 || max < diff) ? diff : max;
      cnt++;
    }
  }
  return max;
}
int main() {
  int n, i, x, min, max;
  vector<int> v;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  cout << timeSerialDiff(v) << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_D
#include <iostream>
using namespace std;

int max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}

int min(int x, int y) {
  if (x > y)
    return y;
  else
    return x;
}

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // A??????????´???????
  int maxv = A[1] - A[0];
  int minv = A[0];

  for (int i = 1; i < N; i++) {
    maxv = max(maxv, A[i] - minv);
    minv = min(minv, A[i]);
    //  cout << minv << " " << maxv << endl;
  }
  cout << maxv << endl;
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_D
#include <iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // A??????????´???????
  int max = A[1] - A[0];
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (max < A[j] - A[i])
        max = A[j] - A[i];
    }
  }
  cout << max << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#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 max = R[1] - R[0];
  int c = max;
  int min = R[0] < R[1] ? R[0] : R[1];

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


Error: TLE

Fixed submission:

#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 max = R[1] - R[0];
  int c = max;

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

#include <iostream>
#include <limits>
#include <math.h>
using namespace std;

int main() {
  int n, min, max, offset;
  //	int offset = numeric_limits<int>::min();
  cin >> n;
  //	int array[n];
  cin >> min;
  cin >> max;
  offset = max - min;
  if (min > max)
    min = max;
  for (int i = 0; i < n - 2; i++) {
    int temp, tempoff;
    cin >> temp;
    if (min > temp) {
      min = temp;

    } else {
      tempoff = temp - min;
      if (offset < tempoff)
        offset = tempoff;
    }
  }

  //		if(i>=1)
  //		{
  //			for(int j = i-1; j>=0; j--) {
  //				if(offset<array[i]-array[j])
  //				offset = array[i]-array[j];
  //			}
  //		}
  //	}

  cout << offset << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits>
#include <math.h>
using namespace std;

int main() {
  int n;
  int offset = numeric_limits<int>::min();
  cin >> n;
  int array[n];
  for (int i = 0; i < n; i++) {
    cin >> array[i];
    if (i >= 1) {
      for (int j = i - 1; j >= 0; j--) {
        if (offset < array[i] - array[j])
          offset = array[i] - array[j];
      }
    }
  }

  cout << offset << endl;

  return 0;
}