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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main() {
  int n;
  while (cin >> n, n) {
    vi v(n + 1, 0);
    rep(i, n) cin >> v[i];
    ll ans = -INF, temp;
    for (int i = 0; i < n; i++) {
      temp = 0;
      for (int j = i; j < n; j++) {
        temp += v[j];
        ans = max(ans, temp);
      }
    }
    if (n == 1)
      cout << v[0] << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main() {
  int n;
  while (cin >> n, n) {
    vi v(n + 1, 0);
    rep(i, n) cin >> v[i];
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      ll temp = 0;
      for (int j = i; j < n; j++) {
        for (int k = i; k <= j; k++)
          temp += v[k];
        ans = max(ans, temp);
        temp = 0;
      }
    }
    cout << ans << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long

using namespace std;
const int MOD = 1000000007;
const int INF = 9000000009;
const double eps = 1e-10;

signed main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n + 100);
    REP(i, n) {
      int x;
      cin >> x;
      a[i + 1] = a[i] + x;
    }
    int ans = -INF;
    REP(i, n + 1) LOOP(j, i + 1, n + 1) ans = max(ans, a[j] - a[i]);
    cout << ans << 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>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long

using namespace std;
const int MOD = 1000000007;
const int INF = 9000000009;
const double eps = 1e-10;

signed main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n);
    REP(i, n) {
      int x;
      cin >> x;
      a[i + 1] = a[i] + x;
    }
    int ans = -INF;
    REP(i, n + 1) LOOP(j, i + 1, n + 1) ans = max(ans, a[j] - a[i]);
    cout << ans << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

static const double PI = 6 * asin(0.5);
typedef complex<double> P;
typedef long long ll;

int n;
ll a[6000];

ll sum(int i, int j) {
  ll s = 0;
  rep(k, j) { s += a[i + k]; }
  return s;
}

int main() {
  while (cin >> n, n != 0) {
    int c = 0;
    ll MAX = -1000000000000000000;
    rep(i, n) a[i] = 0;
    /*
    int cou=0;
    rep(i,n){
            cou++;
            cin>>a[0];
            if(a[0]!=0){
                    break;
            }

    }
    n-=cou;
    */

    rep(i, n) cin >> a[i];
    c = n;
    MAX = a[0];
    rep(i, n) {
      ll sum = 0;
      for (int j = i; j < n; j++) {
        sum += a[j];
        MAX = max(MAX, sum);
      }
    }

    /*
    rep(i,n){
            int t;
            cin>>t;
            if(a[c]*t<0){
                    a[++c]=t;
            }
            else a[c]+=t;
    }


    c++;
    for(int i=1;i<=c;i++){
            rep(j,c+1-i){
                    MAX=max(MAX,sum(j,i));
            }
    }
    */
    cout << MAX << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

static const double PI = 6 * asin(0.5);
typedef complex<double> P;
typedef long long ll;

int n;
ll a[5000];

ll sum(int i, int j) {
  ll s = 0;
  rep(k, j) { s += a[i + k]; }
  return s;
}

int main() {
  while (cin >> n, n != 0) {
    int c = 0;
    ll MAX = -100000000;
    rep(i, n) a[i] = 0;
    cin >> a[0];
    rep(i, n - 1) cin >> a[i + 1];
    c = n - 1;
    /*
    rep(i,n-1){
            int t;
            cin>>t;
            if(a[c]*t<0){
                    a[++c]=t;
            }
            else a[c]+=t;
    }
    */
    for (int i = 1; i <= c + 1; i++) {
      rep(j, c + 1 - i) { MAX = max(MAX, sum(j, i)); }
    }
    cout << MAX << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

// start 20:09
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n;
  vector<int> nums(5001);

  while (cin >> n, n) {
    int max_n;

    REP(i, n) cin >> nums[i];

    // cout << nums[0] << " " << nums[1] << " " << nums[2] << endl;
    max_n = nums[0];

    // cout << "m:" << max_n << endl;

    REP(oft, n) {
      int sum = 0;
      for (int i = oft; i < n; i++) {
        sum += nums[i];
        max_n = max(max_n, sum);
      }
    }
    cout << max_n << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// start 20:09
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

int sum(vector<int> &nums, int oft, int len) {
  int ret = 0;
  for (int i = 0; i < len; i++) {
    ret += nums[oft + i];
  }
  return ret;
}

int main() {
  int n;

  while (cin >> n, n) {
    int max_n;
    vector<int> nums(n);

    REP(i, n) cin >> nums[i];

    cout << nums[0] << " " << nums[1] << " " << nums[2] << endl;
    max_n = nums[0];

    cout << "m:" << max_n << endl;

    REP(oft, n) for (int len = 1; len <= n - oft; len++) {
      // cout << oft << " " << len << endl;
      max_n = max(max_n, sum(nums, oft, len));
      cout << max_n << " " << oft << " " << len << endl;
    }
    cout << max_n << endl;
  }

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

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

int main() {
  int n, k = 0;
  int a[5000];
  long long int b[1000] = {0}, c[1000] = {0};
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[k] = b[k] + a[i];
      if (b[k] < 0) {
        b[k] = 0;
      }
      if (b[k] > c[k]) {
        c[k] = b[k];
      }
    }
    if (c[k] == 0) {
      sort(a, a + n);
      c[k] = a[n - 1];
    }
    k++;
  }
  for (int i = 0; i < k; i++) {
    cout << c[i] << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, k = 0;
  int a[1000][5000];
  long long int b[1000] = {0}, c[1000] = {0};
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[k][i];
      b[k] = b[k] + a[k][i];
      if (b[k] < 0) {
        b[k] = 0;
      }
      if (b[k] > c[k]) {
        c[k] = b[k];
      }
    }
    k++;
  }
  for (int i = 0; i < k; i++) {
    cout << c[i] << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n = 1, count = 0;
  int a[10000];
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int sum = 0, max = -100000;
    for (int ai = 0; ai < n; ai++) {
      sum = 0;
      for (int bi = ai; bi < n; bi++) {
        sum = sum + a[bi];
        if (sum > max) {
          max = sum;
        }
      }
    }
    if (n == 0)
      break;
    cout << max << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n = 1, count = 0;
  int a[10000], ans[10000];
  while (n != 0) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int sum = 0, max = -10000;
    for (int ai = 0; ai < n - 1; ai++) {
      sum = 0;
      for (int bi = ai; bi < n; bi++) {
        sum = sum + a[bi];
        if (sum > max) {
          max = sum;
        }
        cout << ai << " " << bi << endl;
      }
    }
    ans[count] = max;
    count++;
  }
  for (int ci = 0; ci < count - 1; ci++) {
    cout << ans[ci] << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, i, j, k, max, sum;
  while (1) {
    int max = -100000;
    cin >> n;
    int a[n];
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (i = 0; i < n; i++) {
      sum = 0;
      for (j = 0; j < n - i; j++) {
        sum = sum + a[i + j];
        if (max < sum)
          max = sum;
      }
    }
    cout << max << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <iostream>
using namespace std;
int a[5000];
int b[5000][5000];
int main() {
  int n;
  while (cin >> n && n != 0) {
    int sum = -100000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (i == 0) {
        b[0][i] = a[i];
        if (sum < b[0][i])
          sum = b[0][i];
      } else {
        b[0][i] = b[0][i - 1] + a[i];
        if (sum < b[0][i])
          sum = b[0][i];
      }
    }

    for (int j = 1; j < n; j++) {
      for (int i = j; i < n; i++) {
        b[j][i] = b[j - 1][i] - a[j - 1];
        if (sum < b[j][i])
          sum = b[j][i];
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  int a[5000];
  while (cin >> n && n != 0) {
    int sum3 = 0;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n; i++) {
      int sum = 0;
      int sum2 = 0;
      for (int j = 0; j <= i; j++) {
        if (a[j] < 0 || (j <= 1 && a[j - 1] < 0 && (a[j - 1] + a[j]) < 0))
          goto stop;
        ;
        sum2 = 0;
        for (int z = j; z <= i; z++) {
          sum2 += a[z];
          if (sum < sum2)
            sum = sum2;
        }
      stop:;
      }
      if (sum3 < sum)
        sum3 = sum;
    }
    cout << sum3 << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  int const N = 5000;
  while (cin >> n) {
    if (n == 0) {
      return 0;
    }
    int a[N], s, num, st, tmp;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    tmp = a[0];
    for (st = 0; st < n; st++) {
      s = 0;
      for (int i = st; i < n; i++) {
        s = s + a[i];
        tmp = max(s, tmp);
      }
    }
    cout << tmp << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int sum(int a[], int st, int num) {
  int s = 0;
  for (int i = 0; i < num; i++) {
    s = s + a[i + st];
  }
  return s;
}

int main() {
  int n;
  int const N = 5000;
  while (cin >> n) {
    if (n == 0) {
      return 0;
    }
    int a[N], s, num, st, tmp = 0;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (num = 1; num <= n; num++) {
      for (st = 0; st < n; st++) {
        s = sum(a, st, num);
        if (s > tmp) {
          swap(s, tmp);
        }
      }
    }
    cout << tmp << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int count, max, i, j, k, l;
  int sum, data_set[5000];
  while (cin >> count) {
    if (!count)
      break;
    cin >> data_set[0];
    max = data_set[0];
    for (i = 1; i < count; i++) {
      cin >> data_set[i];
    }
    //  for(l=1;l<=count;l++){
    for (j = 0; j < count; j++) {
      sum = 0;
      for (k = j; k < count; k++) {
        sum += data_set[k];
        if (max < sum)
          max = sum;
      }
    }
    cout << max << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int count, max;
  int sum, data_set[5000];
  while (cin >> count) {
    if (!count)
      break;
    cin >> data_set[0];
    max = data_set[0];
    for (int i = 1; i < count; i++) {
      cin >> data_set[i];
    }
    for (int i = 1; i <= count; i++) {
      for (int j = 0; j < count; j++) {
        for (int k = j; k < i; k++) {
          sum += data_set[k];
        }
        if (max < sum)
          max = sum;
        sum = 0;
      }
    }
    cout << max << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

    if (n == 0) {
      break;
    }

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a.at(i));
    }

    int maxv = a.at(0);
    vector<int> sum;
    sum.push_back(0);
    for (int i = 0; i < n; i++) {
      int s = sum.back() + a.at(i);
      sum.push_back(s);

      for (int j = 0; j < sum.size() - 1; j++) {
        int x = sum.back() - sum.at(j);
        maxv = max(maxv, x);
      }
    }

    cout << maxv << endl;
  }
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

    if (n == 0) {
      break;
    }

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a.at(i);
    }

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

      for (int j = 0; j < n; j++) {
        int sum = a.at(j);
        for (int k = j + 1; k < i; k++) {
          sum += a.at(k);
        }
        maxv = max(maxv, sum);
      }
    }

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

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

int main() {
  int x, n;
  int a[5000];
  long int sum, nextsum;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> x;
      a[i] = x;
    }
    sum = *max_element(a, a + n);
    for (int i = 0; i < n; i++) {
      if (a[i] >= 0) {
        nextsum = a[i];
        for (int j = 1; j < n - i; j++) {
          nextsum += a[i + j];
          if (nextsum > sum)
            sum = nextsum;
        }
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int x, n, k;
  int sum[100000], a[5000];
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> x;
      a[i] = x;
    }
    for (int j = 0; j < n; j++) {
      for (int i = j + 1; j < n; i++) {
        for (int m = 0; m < i - j; m++) {
          sum[k] += a[j + m];
        }
        k++;
      }
    }
    cout << *max_element(sum, sum + k) << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
using llong = long long;

llong A[5000];
llong P[5001];
int main() {
  int n;
  while (cin >> n && n > 0) {
    for (int i = 0; i < n; ++i) {
      cin >> A[i];
    }

    for (int i = 1; i <= n; ++i) {
      P[i] = P[i - 1] + A[i - 1];
    }

    llong maxsum = -1000000000000;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j <= n; ++j) {
        if (P[j] - P[i] > maxsum) {
          maxsum = P[j] - P[i];
        }
      }
    }
    cout << maxsum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int A[50000] = {0};
int S[50000] = {0};

int main() {
  int n;

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

    long maxsum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        long sum = 0;
        for (int k = i; k <= j; ++k) {
          sum += A[k];
        }
        if (sum > maxsum)
          maxsum = sum;
      }
    }
    cout << maxsum << endl;
  }

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

#include <iostream>
#include <string>
using namespace std;
int a[5000];
int main() {
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    long long best = -1000000000;
    for (int i = 0; i < n; i++)
      cin >> a[i];

    for (int start = 0; start < n; start++) {
      long long count = 0;
      for (int len = start; len < n; len++) {
        count += a[len];
        best = max(best, count);
      }
    }

    cout << best << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, a[5000];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int best = 0;
    for (int lon = 1; lon <= n; lon++) {
      for (int first = 0; first + lon <= n; first++) {
        int count = 0;
        for (int k = 0; k < lon; k++)
          count += a[first + k];
        if (count > best)
          best = count;
      }
    }
    cout << best << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a[10000], b[10000], n, s, m, p, M;
  while (cin >> n, n) {
    m = INT_MIN;
    p = 0;
    M = INT_MIN;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      M = max(M, a[i]);
      if (a[i] >= 0)
        p = 1;
    }
    if (p == 0)
      m = M;
    else
      for (int i = 1; i <= n; i++) {
        for (int j = 0; j < n - i + 1; j++) {
          if (a[j] < 0 || a[j + i - 1] < 0)
            continue;
          s = 0;
          for (int k = 0; k < i; k++) {
            s += a[j + k];
          }
          m = max(m, s);
        }
      }
    cout << m << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int a[10000], n, s, m;
  while (cin >> n, n) {
    m = INT_MIN;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < n - i + 1; j++) {
        s = 0;
        for (int k = 0; k < i; k++) {
          s += a[j + k];
        }
        m = max(m, s);
      }
    }
    cout << m << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _USE_MATH_DEFINES
// #include <cmath>
#include <iostream>
// #include <stdio.h>
// #include <stdlib.h>
// #include <iomanip>
#include <vector>
// #include <string>
// #include <algorithm>
// #include <functional>
// #include <time.h>
// #include <sstream>
// #include <queue>

using namespace std;

int main() {

  int n, buf, min, max;
  long long ans, sum;
  vector<int> p;

  while (1) {
    p.clear();
    sum = 0;
    ans = -100000;

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

    for (int i = 0; i < n; i++) {
      cin >> buf;
      p.push_back(buf);
    }

    for (int left = 0; left < n; left++) {
      if (p[left] >= 0) {
        for (int right = left; right < n; right++) {
          if (p[right] >= 0) {
            for (int i = left; i <= right; i++) {
              sum += p[i];
            }
            if (sum > ans)
              ans = sum;
            sum = 0;
          }
        }
      }
    }
    if (ans == -100000) {
      for (int i = 0; i < n; i++) {
        if (p[i] > ans)
          ans = p[i];
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #define _USE_MATH_DEFINES
// #include <cmath>
#include <iostream>
// #include <stdio.h>
// #include <stdlib.h>
// #include <iomanip>
#include <vector>
// #include <string>
// #include <algorithm>
// #include <functional>
// #include <time.h>
// #include <sstream>
// #include <queue>

using namespace std;

int main() {

  int n, buf;
  long long ans, sum;
  vector<int> p;

  while (1) {
    p.clear();
    sum = 0;
    ans = -100000;

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

    for (int i = 0; i < n; i++) {
      cin >> buf;
      p.push_back(buf);
    }

    for (int left = 0; left < n; left++) {
      for (int right = left; right < n; right++) {
        for (int i = left; i <= right; i++) {
          sum += p[i];
        }
        if (sum > ans)
          ans = sum;
        sum = 0;
      }
    }

    cout << ans << endl;
  }

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

#include <stdio.h>

int main() {
  int n, x, p, i, j, k;

  while (scanf("%d", &n) != 0) {
    if (n == 0)
      break;

    int a[n];

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

    x = a[0];

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

      for (j = i; j < n; j++) {
        p += a[j];
        if (x < p)
          x = p;
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int n, x, p, i, j, k;

  while (scanf("%d", &n) != 0) {
    if (n == 0)
      break;

    int a[n];

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

    x = a[0];

    for (i = 1; i <= n; i++) {
      for (j = 0; j < n - i + 1; j++) {
        if (j == 0 || a[j + i - 1] > a[j - 1]) {
          p = 0;

          for (k = 0; k < i; k++) {
            p += a[j + k];
          }

          if (x < p)
            x = p;
        }
      }
    }

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

  return 0;
}