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

#include <iostream>

using namespace std;

int S[5000];

int main() {
  int n;
  while (cin >> n, n) {
    S[0] = 0;
    int tmp, sum = 0;
    for (int i = 1; i <= n; i++) {
      cin >> tmp;
      sum += tmp;
      S[i] = sum;
    }
    tmp = S[1] - S[0];
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (tmp < S[j] - S[i])
          tmp = S[j] - S[i];
      }
    }
    cout << tmp << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int S[5000];

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

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

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    vector<long long int> hyou(n + 1, 0);
    long long int a;
    long long int MAX = 0;
    int maxn = 0;
    for (int i = 0; i < n; i++) {
      cin >> a;
      hyou[i + 1] = hyou[i] + a;
      if (i == 0) {
        MAX = hyou[1];
      }
    }
    for (int i = 2; i <= n; i++) {
      MAX = max(MAX, hyou[i]);
      for (int j = 1; j < i; j++) {
        if (hyou[j] < 0) {
          MAX = max(MAX, hyou[i] - hyou[j]);
        }
      }
    }
    cout << MAX << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

long long int keisan(int start, int end, vector<int> hyou) {
  long long int sum = 0;
  for (int i = start; i <= end; i++) {
    sum += hyou[i];
  }
  return sum;
}

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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <queue>
#include <sstream>
#include <stack>

typedef long long ll;
using namespace std;

int sum(int i, int *bit) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x, int *bit, int n) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  int T;
  while (~scanf("%d", &T)) {
    if (T == 0)
      break;
    int *bit = new int[T + 1]();
    int res = numeric_limits<int>::min();
    for (int t = 1; t <= T; t++) {
      int tmp;
      scanf("%d", &tmp);
      add(t, tmp, bit, T);
    }

    for (int a = 0; a <= T; a++) {
      for (int b = a + 1; b <= T; b++) {
        res = max(sum(b, bit) - sum(a, bit), res);
      }
      // cout << sum(a,bit) << endl;
    }

    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <queue>
#include <sstream>
#include <stack>

typedef long long ll;
using namespace std;

int S(int a, int b, int *num) {
  int res = 0;
  for (int i = a; i <= b; i++) {
    res += num[i];
  }
  return res;
}

int main() {
  int T;
  while (~scanf("%d", &T)) {
    if (T == 0)
      break;
    int *num = new int[T]();
    int res = numeric_limits<int>::min();
    for (int t = 0; t < T; t++) {
      scanf("%d", num + t);
    }

    for (int a = 0; a < T; a++) {
      for (int b = a; b < T; b++) {
        res = max(S(a, b, num), res);
      }
    }

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

#include <stdio.h>

int main(void) {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }

    int a[5000];
    int max;

    int i, j;
    for (i = 0; i < n; ++i) {
      scanf("%d", &a[i]);
      int sum = 0;
      for (j = i; j >= 0; --j) {
        sum += a[j];
        if ((i == 0 && j == 0) || sum > max) {
          max = sum;
        }
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }

    int a[5000];
    int max;

    int i, j, k;
    for (i = 0; i < n; ++i) {
      scanf("%d", &a[i]);
      for (j = 0; j <= i; ++j) {
        int sum = 0;
        for (k = j; k <= i; ++k) {
          sum += a[k];
        }
        if ((i == 0 && j == 0) || max < sum) {
          max = sum;
        }
      }
    }

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

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

#include <iostream>
#include <vector>

int main() {
  int n;
  while (std::cin >> n) {
    if (n == 0)
      break;
    std::vector<int> a;
    for (int i = 0; i < n; ++i) {
      int ai;
      std::cin >> ai;
      a.push_back(ai);
    }
    // ここをかく
    int dp[6000], max;
    dp[0] = max = a[0];
    for (int i = 1; i < n; i++) {
      if (dp[i - 1] >= 0) {
        dp[i] = dp[i - 1] + a[i];
      } else {
        dp[i] = a[i];
      }
      max = max > dp[i] ? max : dp[i];
    }

    std::cout << max << std::endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int n;
  while (std::cin >> n) {
    if (n == 0)
      break;
    std::vector<int> a;
    for (int i = 0; i < n; i++) {
      int ai;
      std::cin >> ai;
      a.push_back(ai);
    }
    int max = a[0];
    for (std::vector<int>::iterator aitr = a.begin(); aitr != a.end(); aitr++) {
      for (std::vector<int>::iterator bitr = aitr; bitr != a.end(); bitr++) {
        int s = 0;
        for (std::vector<int>::iterator citr = aitr; citr != bitr + 1; citr++) {
          s += *citr;
        }
        max = s > max ? s : max;
      }
    }
    std::cout << max << std::endl;
  }

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

#include <algorithm>
#include <iostream>
const int MAX_N = 5000;
using namespace std;

int main(void) {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int dp[MAX_N];
    cin >> dp[0];

    int max_num = dp[0];
    for (int i = 1; i < n; i++) {
      int tmp;
      cin >> tmp;
      dp[i] = max(dp[i - 1] + tmp, tmp);
      max_num = max(max_num, dp[i]);
    }

    cout << max_num << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
const int MAX_N = 5000;
using namespace std;

int a[MAX_N];

int main(void) {
  int n;

  while (cin >> n) {
    if (n == 0)
      break;
    long long max_num = -500000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i; j++) {
        long long tmp = 0;
        for (int k = i; k <= j; k++) {
          tmp += a[k];
        }
        max_num = max(max_num, tmp);
      }
    }

    cout << max_num << endl;
  }

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


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

int main() {
  int n;
  int a[5000];
  while (cin >> n && n > 0) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    long long sum = 0, tmp = 0, ma = -100000;
    int m = -100000;
    bool flg = true;
    for (int i = 0; i < n; i++) {
      if (flg)
        m = max(m, a[i]);
      if (a[i] >= 0) {
        flg = false;
        sum += a[i];
        continue;
      }
      ma = ma > sum ? ma : sum;
      for (tmp = sum; i < n && a[i] < 0; i++) {
        tmp += a[i];
      }
      i--;
      if (tmp > 0)
        sum = tmp;
      else
        sum = 0;
    }
    ma = ma > sum ? ma : sum;

    cout << (flg ? m : ma) << endl;
  }
}

Error: WA

Fixed submission:

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

int main() {
  int n;
  int a[5000];
  while (cin >> n && n > 0) {
    for (int i = 0; i > n; i++)
      cin >> a[i];
    int sum = 0, tmp = 0, m = -100000;
    bool flg = true;
    for (int i = 0; i < n; i++) {
      if (flg)
        m = max(m, a[i]);
      if (a[i] > 0) {
        flg = false;
        sum += a[i];
        continue;
      }
      for (tmp = sum; a[i] <= 0; i++) {
        tmp += a[i];
      }
      i--;
      if (tmp > 0)
        sum = tmp;
      else
        sum = 0;
    }

    cout << sum;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
#define rep(i, n) for (i = 0; i < n; ++i)
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); itr++)
#define mp make_pair
#define pb push_back
#define fi first
#define sc second

int main(int argc, char const *argv[]) {
  int i, j;

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

    std::vector<long> a(n + 1);
    rep(i, n) scanf(" %ld", &a[i + 1]);

    std::vector<long> sum(n + 1); // a[1]~a[i]??????
    sum[0] = 0;
    for (i = 1; i <= n; ++i)
      sum[i] = sum[i - 1] + a[i];

    long ans = -5000 * 100000 - 1;
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= n; ++j) {
        ans = max(ans, sum[j] - sum[i - 1]);
      }
    }

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

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (i = 0; i < n; ++i)
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); itr++)
#define mp make_pair
#define pb push_back
#define fi first
#define sc second

int main(int argc, char const *argv[]) {
  int i, j;

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

    std::vector<long> a(n + 1);
    rep(i, n) scanf(" %ld", &a[i + 1]);

    std::vector<long> sum(n); // a[1]~a[i]??????
    sum[0] = 0;
    for (i = 1; i <= n; ++i)
      sum[i] = sum[i - 1] + a[i];

    long ans = -5000 * 100000 - 1;
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= n; ++j) {
        ans = max(ans, sum[j] - sum[i - 1]);
      }
    }

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

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

int a[5001] = {0};
int S[5001] = {0};

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

    S[0] = 0;
    for (int i = 1; i <= n; i++) {
      S[i] = S[i - 1] + a[i - 1];
    }

    int maxsum = INT_MIN;
    int sum;
    for (int i = 0; i <= n - 1; i++) {
      for (int j = i + 1; j <= n; j++) {
        sum = S[j] - S[i];
        if (sum > maxsum)
          maxsum = sum;
      }
    }
    cout << maxsum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int a[5001] = {0};
int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }

    int maxsum = 0;
    int sum = 0;
    for (int i = 1; i <= n - 1; i++) {
      for (int j = i + 1; j <= n; j++) {
        for (int k = i; k <= j; k++) {
          sum += a[k];
        }
        if (sum > maxsum)
          maxsum = sum;
        sum = 0;
      }
    }
    cout << maxsum << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<int> in(n);
    rep(i, n) cin >> in[i];
    int max = in[0];
    rep(i, n) {
      int sum = 0;
      loop(j, i, n) {
        sum += in[j];
        if (max < sum)
          max = sum;
      }
    }
    cout << max << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
int main() {
  int n;
  vector<int> in(n);
  while (cin >> n, n) {
    rep(i, n) cin >> in[i];
    int max = 0;
    rep(i, n) {
      int sum = 0;
      loop(j, i, n) {
        sum += in[j];
        if (max < sum)
          max = sum;
      }
    }
    cout << max << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, a[5000], s, ans;

  while (1) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (n == 0) {
      break;
    }
    ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n - i; j++) {
        s = 0;
        if ((a[j] <= 0) || (a[j + i - 1] <= 0)) {
          goto A;
        }
        for (int k = j; k < j + i; k++) {
          s += a[k];
        }
        if (ans < s) {
          ans = s;
        }
      A:;
      }
    }
    if (ans == 0) {
      for (int i = 1; i < n; i++) {
        for (int j = 0; j < n - i; j++) {
          if (a[j] < a[j + 1]) {
            s = a[j];
            a[j] = a[j + 1];
            a[j + 1] = s;
          }
        }
      }
      ans = a[0];
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, a[5000], s, ans;

  while (1) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (n == 0) {
      break;
    }
    ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n - i; j++) {
        s = 0;
        if (a[j] <= 0) {
          goto A;
        }
        for (int k = j; k < j + i; k++) {
          s += a[k];
        }
        if (ans < s) {
          ans = s;
        }
      A:;
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[5000];
bool flg[5000];
vector<int> vec;

int sum(int i, int j);

int main() {
  int n, i, j, k, l, s, max;

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

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

    max = -2147483647;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++)
        if ((s = sum(i, j)) > max)
          max = s;
      vec.clear();
      flg[i] = false;
    }
    cout << max << endl;
  }
  return 0;
}

int sum(int a, int b) {
  int tmp;
  if (!flg[a]) {
    vec.push_back(::a[a]);
    flg[a] = true;
  }
  if (b - a - 1 >= 0) {
    vec.push_back(vec[b - a - 1] + ::a[b]);
  }
  return vec.back();
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, a[5000], i, j, k, l, sum, max;

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

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

    max = -2147483647;
    for (i = 0; i < n; i++) {
      for (j = 0; j < n - i; j++) {
        for (k = 0; k < n - i; k++) {
          sum = 0;
          for (l = k; l <= i; l++)
            sum += a[l];
          if (sum > max)
            max = sum;
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n = 0, a[5000], saidai = -100000, kazu;
  while (cin >> n && n != 0) {
    saidai = -100000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      kazu = 0;
      for (int x = i; x < n; x++) {
        kazu = kazu + a[x];
        if (saidai < kazu) {
          saidai = kazu;
        }
      }
    }
    cout << saidai << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n = 0, a[5000], saidai = -100000, kazu;
  while (cin >> n && n != 0) {
    saidai = -100000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      kazu = 0;
      for (int x = i; x < n; x++) {
        kazu = kazu + a[x];
        cout << kazu << endl;
        if (saidai < kazu) {
        }
      }
    }
    cout << saidai << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
int main() {
  int a, b, c = 1, d = 0;
  while (c = 1) {
    std::cin >> a;

    int z = -100000;

    if (a == 0) {
      return 0;
    }
    int aa[a];
    for (int t = 1; t <= a; t++) {
      std::cin >> b;

      aa[t] = b;
    }

    for (int s = 1; s <= a; s++) {

      for (int l = s; l <= a; l++) {
        d = d + aa[l];
        if (z < d) {
          z = d;
        }
      }
      d = 0;
    }
    std::cout << z << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n = 0, a[5000] = {0}, saidai = -100000;
  while (cin >> n && n != 0) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; n > i; i++) {
      int kazu = 0;
      for (int x = i; x < n; x++) {
        kazu = kazu + a[x];
        cout << kazu << endl;
        if (kazu > saidai) {
          saidai = kazu;
        }
      }
    }
    cout << saidai << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int maxSum(int *a, int n) {
  int sum, max = a[0];
  for (int i = 0; i < n; ++i) {
    sum = 0;
    for (int j = i; j < n; ++j) {
      sum += a[j];
      if (sum > max) {
        max = sum;
      }
    }
  }
  return max;
}

int main() {
  int n;
  int *a;
  for (;;) {
    std::cin >> n;
    if (n <= 0) {
      break;
    }
    a = new int[n];
    for (int i = 0; i < n; ++i) {
      std::cin >> a[i];
    }
    std::cout << maxSum(a, n) << '\\n';
    delete[] a;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int maxSum(int *a, int n) {
  int sum, max = a[0];
  for (int num = 1; num <= n; ++num) {
    for (int i = 0; i < n - num + 1; ++i) {
      sum = 0;
      for (int j = 0; j < num; ++j) {
        sum += a[i + j];
      }
      if (sum > max) {
        max = sum;
      }
    }
  }
  return max;
}

int main() {
  int n;
  int *a;
  for (;;) {
    std::cin >> n;
    if (n <= 0) {
      break;
    }
    a = new int[n];
    for (int i = 0; i < n; ++i) {
      std::cin >> a[i];
    }
    std::cout << maxSum(a, n) << '\\n';
    delete[] a;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

    int max = vec[0] + vec[1];
    for (int i = 0; i < vec.size(); i++) {
      int add = 0;
      for (int j = i; j < vec.size(); j++) {
        add += vec[j];
        if (max < add)
          max = add;
      }
    }

    cout << max << endl;
  }
}

Error: TLE

Fixed submission:

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

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

    int max = vec[0] + vec[1];
    for (int i = 0; i < vec.size(); i++) {
      for (int j = i + 1; j < vec.size(); j++) {
        int add = 0;
        for (int k = i; k <= j; k++) {
          add += vec[k];
        }
        if (max < add)
          max = add;
      }
    }

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

#include <iostream>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    long long ALL = 0;
    long long a[n], max = -500000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        ALL += a[j];
        if (ALL > max) {
          max = ALL;
        }
      }
      ALL = 0;
    }
    cout << max << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    long long ALL = 0;
    long long a[n], max = -500000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 2; j + i < n + 1; j++) {
        for (int k = i; k - i < j; k++) {
          ALL += a[k];
        }
        if (max < ALL) {
          max = ALL;
        }
        ALL = 0;
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

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


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 7) >= this->size() (which is 7)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0) {
      return 0;
    }
    vector<int> a(n);
    for (int i = 0; 0 < n; i++) {
      cin >> a.at(i);
    }
    vector<int> b(n + 1);
    b.at(0) = 0;
    for (int i = 1; i <= n; i++) {
      b.at(i) = b.at(i - 1) + a.at(i - 1);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans = max(ans, b.at(i) - b.at(j));
      }
    }
    cout << ans << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, num, a, maxx;

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

    num = 0;

    for (int i = 0; i < n; i++) {
      cin >> a;
      num += a;
      if (i == 0)
        maxx = a;
      maxx = max(maxx, num);
      if (num < 0)
        num = 0;
    }
    cout << maxx << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#define N 5000000
using namespace std;

int main() {
  int n, num, a, maxx;
  int ans[N], ac;
  ac = 0;

  while (cin >> n) {
    if (n == 0) {
      ans[ac] = 0;
      break;
    }

    num = maxx = 0;

    for (int i = 0; i < n; i++) {
      cin >> a;
      num += a;
      if (num < 0)
        num = 0;
      maxx = max(maxx, num);
    }
    ans[ac] = maxx;
    ac++;
  }

  for (int i = 0; ans[i] != 0; i++)
    cout << ans[i] << endl;

  return 0;
}