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

#include <iostream>
using namespace std;

int main() {

  int n = -1, max = 0, cum = 0;

  cin >> n;

  int foo[5000];

  while (n) {

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

      cin >> foo[i];

      if (i == 0)
        max = foo[i];

      if (cum < 0)
        cum = 0;

      cum += foo[i];

      if (cum > max)
        max = cum;
    }

    cout << max << endl;

    max = 0;
    cum = 0;

    cin >> n;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int main() {

  int n = -1, max = 0, cum;

  cin >> n;

  while (n != 0) {

    int *foo = new int[n];
    foo[n] = {0};

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

      cin >> foo[i];

      if (i == 0)
        max = foo[i];

      cum += foo[i];

      if (cum < 0)
        cum = 0;

      if (cum > max && cum > 0)
        max = cum;
    }

    cout << max << endl;

    max = 0;
    cum = 0;

    cin >> n;
  }

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

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

int main() {
  int n;
  cin >> n;
  while (n != 0) {
    int a[n];

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

    int maximum = -100000000;
    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += a[j];
        maximum = max(maximum, sum);
      }
    }

    cout << maximum << endl;
    cin >> n;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  while (n != 0) {
    int sum[n][n];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        sum[i][j] = 0;
      }
    }

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

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

    cout << maximum << endl;
    cin >> n;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

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

    long long int dp[5101] = {0}, mx = a[0];

    dp[0] = a[0];
    for (int i = 1; i < n; i++) {
      dp[i] = max(dp[i - 1] + a[i], a[i]);
      mx = max(dp[i], mx);
    }
    cout << mx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

    int dp[5001] = {0}, mx = 0;

    dp[0] = a[0];
    for (int i = 1; i <= n; i++) {
      dp[i] = max(dp[i - 1] + a[i], a[i]);
      mx = max(dp[i], mx);
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

#define MAX 5000

using namespace std;

int main(void) {

  int n, a[5000];
  int wa, max;

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

#define MAX 5000

using namespace std;

int main(void) {

  int n, a[5000];
  int wa, max;

  while (1) {
    cin >> n;
    wa = 0;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    max = a[0];
    for (int i = n - 1; i >= 0; i--) {
      wa += a[i];
      if (wa < 0)
        wa = 0;
      if (wa > max)
        max = wa;
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a, b) (fabs((a) - (b)) < EPS)

int main() {
  int n;
  while (cin >> n && n != 0) {
    int ret = -INF;
    int a[5000];
    int memo[5000];
    for (int i = 0; i < n; ++i)
      cin >> a[i], memo[i] = -INF;
    for (int i = 0; i < n; ++i) {
      memo[i] = max(memo[i], a[i]);
      for (int j = i + 1; j < n; ++j) {
        memo[j] = max(memo[j], memo[j - 1] + a[j]);
      }
    }
    for (int i = 0; i < n; ++i)
      ret = max(ret, memo[i]);
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a, b) (fabs((a) - (b)) < EPS)

int main() {
  int n;
  while (cin >> n && n != 0) {
    int ret = -INF;
    int a[5000];
    for (int i = 0; i < n; ++i)
      cin >> a[i];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        int sum = 0;
        for (int k = i; k <= j; ++k)
          sum += a[k];
        ret = max(ret, sum);
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ull unsigned long long
#define ll long long
#define rep(i, j) for (int i = 0; i < j; ++i)
#define loop(i, a, j) for (int i = a; i < j; ++i)
#define mpi(it, table, T, E)                                                   \\
  for (map<T, E>::iterator it = table.begin(); it != table.end(); ++it)
#define PI 3.141592654
/*
ifstream fin("input.txt");
ofstream fout("output.txt");
#define cin fin
#define cout fout
*/
#define N 5005

bool visited[N][N];
int data[N];

int solve(int i, int j, int sum) {
  if (i == j)
    return sum;
  if (visited[i][j])
    return sum;

  int a = max(sum, solve(i + 1, j, sum - data[i]));
  int b = max(sum, solve(i, j - 1, sum - data[j]));

  visited[i][j] = true;
  return max(a, b);
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(visited, 0, sizeof(visited));
    int sum = 0;
    rep(i, n) {
      scanf("%d", &data[i]);
      sum += data[i];
    }
    sum = solve(0, n - 1, sum);
    printf("%d\\n", sum);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ull unsigned long long
#define ll long long
#define rep(i, j) for (int i = 0; i < j; ++i)
#define loop(i, a, j) for (int i = a; i < j; ++i)
#define mpi(it, table, T, E)                                                   \\
  for (map<T, E>::iterator it = table.begin(); it != table.end(); ++it)
#define PI 3.141592654
/*
ifstream fin("input.txt");
ofstream fout("output.txt");
#define cin fin
#define cout fout
*/
#define N 5005

int sol[N][N];
bool visited[N][N];
int data[N];

int solve(int i, int j, int sum) {
  if (i == j)
    return sum;
  if (visited[i][j])
    return sol[i][j];

  int a = max(sum, solve(i + 1, j, sum - data[i]));
  int b = max(sum, solve(i, j - 1, sum - data[j]));
  sol[i][j] = max(a, b);

  visited[i][j] = true;
  return sol[i][j];
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(visited, 0, sizeof(visited));
    memset(sol, 0, sizeof(visited));
    int sum = 0;
    rep(i, n) {
      scanf("%d", &data[i]);
      sum += data[i];
    }
    sum = solve(0, n - 1, sum);
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n;
  while (true) {
    cin >> n;
    if (!n)
      break;
    int seq[n];
    for (int i = 0; i < n; i++)
      cin >> seq[i];
    int max = -1e9;
    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += seq[j];
        if (sum > max)
          max = sum;
      }
    }
    cout << max << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  while (true) {
    cin >> n;
    if (!n)
      break;
    int seq[n];
    for (int i = 0; i < n; i++)
      cin >> seq[i];
    int max = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        int sum = 0;
        for (int k = i; k <= j; k++)
          sum += seq[k];
        if (sum > max)
          max = sum;
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
using namespace std;

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    int submax;
    scanf("%d", &submax);
    int maxval = submax;
    for (int i = 1; i < n; i++) {
      int a;
      scanf("%d", &a);
      submax = max(a, submax + a);
      maxval = max(maxval, submax);
    }
    printf("%d\\n", maxval);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int n;
  while (true) {
    scanf("%d", &n);
    if (n == 0) {
      return 0;
    }

    if (n >= 1 && n <= 5000) {
      int a[5000] = {0}, max = 0;
      for (int x = 0; x < n; x++) {
        scanf("%d", &a[x]);
        if (a[x] <= 10000 || a[x] >= -100000) {
          x--;
        } else {
          for (int y = 0; y <= x; y++) {
            if (y != x) {
              a[y] += a[x];
            }
            if (max < a[y]) {
              max = a[y];
            }
          }
        }
      }
      printf("%d\\n", max);
    }
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  int sum, max_sum;

  while (cin >> n, n) {
    vector<int> a;
    for (int i = 0; i < n; i++) {
      int temp;
      cin >> temp;
      a.push_back(temp);
    }

    max_sum = a[0];

    for (int begin = 0; begin < n; begin++) {
      sum = 0;
      for (int end = begin; end < n; end++) {
        sum += a[end];
        if (max_sum < sum) {
          max_sum = sum;
        }
      }
    }

    cout << max_sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int n;
  std::vector<int> a;
  while (std::cin >> n, n) {
    for (; n--;) {
      int ai;
      std::cin >> ai;
      a.push_back(ai);
    }
    long long int max_sum = a[0];
    for (int len = 1; len <= a.size(); len++) {
      for (int begin = 0; begin < a.size() - len + 1; begin++) {
        long long int sum = 0;
        for (int i = begin; i < begin + len; i++) {
          sum += a[i];
        }
        if (sum > max_sum) {
          /*std::cout << begin << ',' << len << std::endl;*/
          max_sum = sum;
        }
      }
    }
    std::cout << max_sum << std::endl;
    a.clear();
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {

  int n;
  while (cin >> n) {
    if (n == 0)
      break;

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

    vector<int> dp(n + 5, 0);
    for (int i = 1; i <= n; i++) {
      dp[i] = max(a[i - 1], dp[i - 1] + a[i - 1]);
    }
    int ans = dp[1];
    for (int i = 1; i <= n; i++) {
      ans = max(ans, dp[i]);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {

  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<int> a(n + 1, 0);
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      a[i + 1] += sum;
    }

    int mx = a[1];
    for (int i = 0; i < n + 1; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        cerr << a[j] - a[i] << " ";
        mx = max(mx, a[j] - a[i]);
      }
    }
    cout << mx << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n != 0) {
    vector<int> a(n);
    vector<int> S(n + 1);
    S.at(0) = 0;
    for (int i = 0; i < n; i++) {
      cin >> a.at(i);
    }
    for (int i = 0; i < n; i++) {
      S.at(i + 1) = S.at(i) + a.at(i);
    }
    int ans = -99999999990990999;
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n - k; i++) {
        if (S.at(n - k) - S.at(i) > ans) {
          ans = S.at(n - k) - S.at(i);
        }
      }
    }
    cout << ans << endl;
    cin >> n;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n != 0) {
    vector<int> a(n);
    vector<int> S(n + 1);
    S.at(0) = 0;
    for (int i = 0; i < n; i++) {
      cin >> a.at(i);
    }
    for (int i = 0; i < n; i++) {
      S.at(i + 1) = S.at(i) + a.at(i);
    }
    int ans = 0;
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        if (S.at(n - k) - S.at(i + 1) > ans) {
          ans = S.at(n - k) - S.at(i + 1);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  for (;;) {
    long long int n, a[5001] = {}, ma = 0, sum = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ma = -1e9;
    for (int i = 0; i < n; i++) {
      sum = 0;
      for (int j = i; j < n; j++) {
        sum += a[j];
        ma = max(ma, sum);
      }
    }
    cout << ma << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  for (;;) {
    long long int n, a[5001] = {}, ma = 0, sum = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ma = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        sum = 0;
        for (int k = i; k <= j; k++) {
          sum += a[k];
        }
        ma = max(ma, sum);
      }
    }
    cout << ma << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  int n, maximum, sum;
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    int array[n];
    for (int i = 0; i < n; i++)
      scanf("%d", &array[i]);
    maximum = -600000000;
    for (int k = 0; k < n; k++) { //?§????????????????
      sum = 0;
      for (int p = k; p <= n - 1; p++) { //??????????????????
        sum += array[p];
        maximum = (maximum >= sum) ? maximum : sum;
      }
    }
    printf("%d\\n", maximum);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  int n, maximum, sum;
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    int array[n];
    for (int i = 0; i < n; i++)
      scanf("%d", &array[i]);
    maximum = -600000000;
    for (int k = 0; k < n; k++) {   //?§????????????????
      for (int j = k; j < n; j++) { //??????????????????
        sum = 0;
        for (int p = k; p <= j; p++) { //??????????????????
          sum += array[p];
        }
        maximum = (maximum >= sum) ? maximum : sum;
      }
    }
    printf("%d\\n", maximum);
  }

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

#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
int main() {
  int n, k, p, m;
  bool isPlus = true;
  while (cin >> n) {
    if (0 == n)
      break;
    vector<int> A(n, 0);
    m = INT_MIN;
    p = 0;
    for (int i = 0; i < n; ++i) {
      cin >> k;
      if (m < k)
        m = k;
      if (0 == i) {
        A[p] = k;
        isPlus = (k > 0);
      } else {
        if (isPlus == (k > 0)) {
          A[p] += k;
        } else {
          ++p;
          A[p] = k;
          isPlus = !isPlus;
        }
      }
    }
    vector<int> B(p + 1, 0);
    vector<int> C(p + 1, 0);
    for (int i = 0; i <= p; ++i) {
      if (0 == i)
        C[0] = B[0] = A[0];
      else {
        int s = B[i - 1];
        for (int j = 0; j <= i; ++j) {
          C[j] += A[i];
          if (s < C[j])
            s = C[j];
        }
        B[i] = s;
      }
    }
    if (m < B[p])
      m = B[p];
    cout << m << "\\n";
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
int main() {
  int n, k, p = 0;
  bool isPlus = true;
  while (cin >> n) {
    if (0 == n)
      break;
    vector<int> A(n, 0);
    int m = INT_MIN;
    for (int i = 0; i < n; ++i) {
      cin >> k;
      if (0 == i) {
        A[p] = k;
        isPlus = (k > 0);
      } else {
        if (isPlus == (k > 0)) {
          A[p] += k;
        } else {
          if (isPlus == (k > 0)) {
            A[p] += k;
          } else {
            ++p;
            A[p] = k;
            isPlus = !isPlus;
          }
          while (1 < p && isPlus) {
            if (m < A[p - 2])
              m = A[p - 2];
            if (m < A[p])
              m = A[p];
            int a = A[p - 2] + A[p - 1] + A[p];
            if (a > A[p - 2] && a > A[p]) {
              A[p - 2] += A[p - 1] + A[p];
              A[p - 1] = A[p] = 0;
              p -= 2;
              isPlus = true;
            } else
              break;
          }
          if (m < A[p])
            m = A[p];
        }
      }
    }
    if (m < A[p])
      m = A[p];
    cout << m << "\\n";
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;

int main() {
  int n;
  while (scanf("%d", &n) && n != 0) {
    long long int a[n];
    for (int i = 0; i < n; i++)
      scanf("%lld", &a[i]);
    long long int dp[n];
    dp[0] = a[0];
    long long int res = dp[0];
    for (int i = 1; i < n; i++) {
      dp[i] = max(a[i], dp[i - 1] + a[i]);
      res = max(res, dp[i]);
    }
    printf("%lld\\n", res);
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n;
  while (cin >> n && n != 0) {
    vector<int> num(n);
    for (int i = 0; i < n; i++) {
      cin >> num[i];
    }
    int m = 0, sum = 0;
    for (int i = 0; i < n; i++) {
      sum = num[i];
      for (int j = i + 1; j < n; i++) {
        sum += num[j];
        m = max(m, sum);
      }
    }
    cout << m << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>

using namespace std;
int main(void) {
  int n, a[10000];
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    int goukei = 0, led = -10000000;
    for (int i = 1; i <= n; i++) {
      goukei = 0;
      for (int j = i; j <= n; j++) {
        goukei = goukei + a[j];
        led = max(goukei, led);
      }
    }
    cout << led << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>

using namespace std;
int main(void) {
  int n, a[1000];
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    int goukei = 0, led = -10000000;
    for (int i = 1; i <= n; i++) {
      goukei = 0;
      for (int j = i; j <= n; j++) {
        goukei = goukei + a[j];
        led = max(goukei, led);
      }
    }
    cout << led << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (i > 0)
        a[i] += a[i - 1];
    }
    a[0] = 0;
    int ans = a[1];
    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;
}

Error: 0

Fixed submission:

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

int main() {

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> a(n);
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (i > 0)
        a[i] += a[i - 1];
    }
    a[0] = 0;
    int ans = a[1];
    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 p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int num[5001];
/*
int sum(int a,int b){
  int s=0;
  for(int i=0;i<b;i++){
    s+=num[a+i];
  }
  return s;
}
*/
int main() {
  int max;
  int n, sum;

  while (cin >> n, n) {
    max = -10000000;
    // int sum=-10000000;
    for (int i = 0; i < n; i++) {
      cin >> num[i];
    }
    for (int i = 0; i < n; i++) {
      sum = 0;
      for (int j = i; j < n; j++) {
        sum += num[j];
        if (max < sum)
          max = sum;
      }
    }
    cout << max << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int num[5001];

int sum(int a, int b) {
  int s = 0;
  for (int i = 0; i < b; i++) {
    s += num[a + i];
  }
  return s;
}

int main() {
  int max;
  int n;

  while (cin >> n, n) {
    max = 0;
    for (int i = 0; i < n; i++) {
      cin >> num[i];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 2; j <= n - i; j++) {
        if (max < sum(i, j))
          max = sum(i, j);
      }
    }
    cout << max << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#define REP(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
using namespace std;

int main() {
  int N;
  while (cin >> N && N > 0) {
    int a[N];
    REP(i, N) { cin >> a[i]; }

    int maximum = a[0];
    REP(i, N) {
      int sum = 0;
      for (int j = i; j < N; j++) {
        sum += a[j];
        maximum = max(sum, maximum);
      }
    }
    cout << maximum << endl;
  }
}

Error: TLE

Fixed submission:

#define REP(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
using namespace std;

int main() {
  int N;
  while (cin >> N && N > 0) {
    int a[N];
    REP(i, N) { cin >> a[i]; }

    int maximum = 0;
    REP(i, N) {
      int maximum_i = 0;
      REP(j, N - i) {
        int sum = 0;
        REP(k, i + 1) { sum += a[j + k]; }
        maximum_i = max(sum, maximum_i);
      }
      maximum = max(maximum_i, maximum);
    }
    cout << maximum << endl;
  }
}