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

#include <cstdio>

using namespace std;

double x1, y1, x2, y2, x3, y3, x4, y4;

int main() {
  int n;
  scanf("%d\\n", &n);
  while (n > 0) {
    scanf("%lf %lf %lf %lf %lf %lf %lf %lf\\n", &x1, &y1, &x2, &y2, &x3, &y3,
          &x4, &y4);
    double dx1 = x2 - x1, dx2 = x4 - x3, dy1 = y2 - y1, dy2 = y4 - y3,
           det = dx1 * dy2 - dx2 * dy1;
    if (det < 0) {
      det *= -1;
    }

    if (det < 0.0000000001) {
      printf("YES\\n");
    } else {
      printf("NO\\n");
    }

    /* if( (dx1 == 0 && dx2 == 0) || (dy1 == 0 && dy2 == 0 ) )
    {
        printf( "YES\\n" );
    }
    else if( dx1 != 0 && dx2 != 0 )
    {
        dy1 /= dx1;
        dy2 /= dx2;
        if( dy1 == dy2 || dy1 == -dy2 )
        {
            printf( "YES\\n" );
        }
    }
    else
    {
        printf( "NO\\n" );
    }
     */
    n--;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <cstdio>

using namespace std;

double x1, y1, x2, y2, x3, y3, x4, y4;

int main() {
  int n;
  scanf("%d\\n", &n);
  while (n > 0) {
    scanf("%lf %lf %lf %lf %lf %lf %lf %lf %lf\\n", &x1, &y1, &y2, &y2, &y3, &y3,
          &y4, &y4);
    double dx1 = x2 - x1, dx2 = x4 - x3, dy1 = y2 - y1, dy2 = y4 - y3;
    if ((dx1 == 0 && dx2 == 0) || (dy1 == 0 && dy2 == 0)) {
      printf("YES\\n");
    } else {
      dy1 /= dx1;
      dy2 /= dx2;
      if (dy1 == dy2 || dy1 == -dy2) {
        printf("YES\\n");
      }
    }
    printf("NO\\n");
    n--;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  //	cut here before submit
  //	freopen("testcase.mss", "r", stdin);
  string str = "";
  int n;

  while (1) {
    getline(cin, str);
    stringstream ssn(str);
    ssn >> n;
    if (n == 0) {
      break;
    } // end if
    vector<int> a(n + 1, 0);
    int i, j;

    vector<int> sum(n + 1, 0);

    for (i = 1; i <= n; ++i) {
      getline(cin, str);
      stringstream ss(str);
      ss >> a[i];
    } // end for
    for (i = 1; i <= n; ++i) {
      sum[i] = accumulate(a.begin(), a.begin() + i + 1, 0);
      //			cout << "sum[" << i << "]:" << sum[i] << endl;
    } // end for

    int maxn = INT_MIN;
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= n; ++j) {
        //				cout << " sum[" << j << "]:" << sum[j];
        //				cout << " sum[" << (i-1) << "]:" <<
        //sum[i-1]; 				cout << " sub: " << (sum[j] - sum[i-1]) << endl;
        maxn = max(maxn, sum[j] - sum[i - 1]);
      } // end for
    }   // end for
    cout << maxn << endl;

  } // end while

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  //	cut here before submit
  //	freopen("testcase.mss", "r", stdin);
  string str = "";
  string res = "";
  int n;

  while (1) {
    getline(cin, str);
    stringstream ssn(str);
    ssn >> n;
    if (n == 0) {
      break;
    } // end if
    vector<int> a(n, 0);
    int i, j, k;

    for (i = 0; i < n; ++i) {
      getline(cin, str);
      stringstream ss(str);
      ss >> a[i];
    } // end for
    vector<int> sum;
    for (i = 0; i < n; ++i) {
      for (j = i + 1; j < n; ++j) {
        int s = 0;
        for (k = i; k <= j; ++k) {
          s += a[k];
        } // end for
        sum.push_back(s);
      } // end for
    }   // end for
    vector<int>::iterator itM = max_element(sum.begin(), sum.end());
    cout << *(itM) << endl;

  } // end while

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

#include <iostream>
using namespace std;
int main(void) {

  long n, max = 0;
  long *a;
  long sum;

  do {
    max = -1000000;
    // cout << endl;
    cin >> n;

    if (n == 0)
      break;

    a = new long[n + 1];

    sum = 0;

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

      sum = 0;

      cin >> a[i];

      for (int j = i; j >= 0; j--) {

        sum += a[j];
        if (max < sum)
          max = sum;
      }
    }

    cout << max << endl;
  } while (1);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {

  int n, max = 0;
  int *a;
  int **sum;

  do {
    max = 0;
    // cout << endl;
    cin >> n;

    if (n == 0)
      break;

    a = new int[n];
    sum = new int *[n];
    for (int i = 0; i < n; i++)
      sum[i] = new int[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 >> a[i];

      for (int j = i; j >= 0; j--) {

        for (int k = i; k >= j; k--) {
          sum[i][j] += a[k];
        }
        if (max < sum[i][j])
          max = sum[i][j];
      }
    }

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

#include <iostream>
using namespace std;
int main() {
  int A = 0;
  int n;
  while (cin >> n && n != 0) {
    int a[n];
    int R = -100000;
    for (int h = 0; h < n; h++) {
      cin >> a[h];
    }
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        A = A + a[j];
        if (R < A) {
          R = A;
        }
      }

      A = 0;
    }
    cout << R << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n && n != 0) {
    int a[n];
    int A = 0;
    int R = -100000;
    for (int h = 0; h < n; h++) {
      cin >> a[h];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = i; k <= j; k++) {
          //	cout << k << " ";
          A = A + a[k];
        }
        if (R < A) {
          R = A;
        }
        //	cout<<endl;
        A = 0;
      }
    }
    cout << R << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  while (cin >> n, n) {
    int x[n];
    for (int i = 0; i < n; i++)
      cin >> x[i];

    int ans = -(1 << 30);
    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += x[j];
        ans = max(ans, sum);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    int x[n];
    for (int i = 0; i < n; i++)
      cin >> x[i];

    int ans = 0;
    int dp[n][n]; // dp[i][j] : iツつゥツづァjツづ慊づづ個和
    for (int i = 0; i < n; i++) {
      dp[i][i] = x[i];
      ans = max(ans, dp[i][i]);
      for (int j = i + 1; j < n; j++) {
        dp[i][j] = dp[i][j - 1] + x[j];
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define INF 20000000
int array[5001];
int n, i, sum, k = 0;
void tansaku(int a) {
  k += array[a];
  if (k > sum) {
    sum = k;
  }
  if (a == n - 1)
    return;
  tansaku(a + 1);
}
int main(void) {
  while (1) {
    for (i = 0; i < 5001; i++) {
      array[i] = 0;
    }
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &array[i]);
    }
    sum = -INF;
    for (i = 0; i < n; i++) {
      tansaku(i);
      k = 0;
    }
    printf("%d\\n", sum);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int array[1000];
int n, i, sum = 0, k = 0;
void tansaku(int a);
int main(void) {
  while (1) {
    sum = 0;
    for (i = 0; i < 1000; i++) {
      array[i] = 0;
    }
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &array[i]);
    }
    for (i = 0; i < n; i++) {
      tansaku(i);
      k = 0;
    }
    printf("%d\\n", sum);
  }
  return 0;
}
void tansaku(int a) {
  if (a > n)
    return;
  k += array[a];
  if (k > sum) {
    sum = k;
  }
  if (a == n)
    return;
  tansaku(a + 1);
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int stage[100000] = {0};
    for (int i = 1; i <= n; i++) {
      scanf("%d", &stage[i]);
    }
    int x = 0;
    int ans = -1000000;
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        x += stage[j];
        ans = max(ans, x);
      }
      x = 0;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int stage[1000] = {0};
    for (int i = 1; i <= n; i++) {
      scanf("%d", &stage[i]);
    }
    int dp[1000] = {0};
    dp[1] = stage[1];
    dp[2] = max(stage[2], dp[1] + stage[2]);
    int x = 0;
    int ans = dp[2];
    for (int i = 2; i <= n; i++) {
      x = max(stage[i + 1], stage[i + 1] + dp[i]);
      if (x > ans)
        ans = x;
      dp[i + 1] = x;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

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

int n;
vector<int> v;
long long s[5000];

int main() {
  while (cin >> n, n) {
    v = vector<int>(n);
    REP(i, n) cin >> v[i];
    long long ans = -(1 << 30);
    REP(i, n) {
      s[i] = v[i];
      ans = max(s[i], ans);
      for (int j = i + 1; j < n; j++) {
        s[j] = s[j - 1] + v[j];
        ans = max(s[j], ans);
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

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

int n;
vector<int> v;
map<int, long long> s[5000];

// long long s[5000][5000];

int main() {
  while (cin >> n, n) {
    v = vector<int>(n);
    REP(i, n) cin >> v[i];
    long long ans = -(1 << 30);
    REP(i, n) {
      s[i][i] = v[i];
      ans = max(s[i][i], ans);
      for (int j = i + 1; j < n; j++) {
        s[i][j] = s[i][j - 1] + v[j];
        ans = max(s[i][j], ans);
      }
    }
    cout << ans << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

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

    int data[6000];

    rep(i, n) cin >> data[i];

    ll maxi = -999999999999;

    for (int i = 0; i < n; i++) {
      ll sum[6000] = {};
      sum[i] = data[i];
      maxi = max(maxi, sum[i]);

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

    cout << maxi << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int main() {
  int n;

  while (cin >> n) {

    if (n == 0)
      break;
    int data[10000] = {};
    rep(i, n) cin >> data[i];

    int maxi = -999999999;
    int dp[5000][5001] = {};
    rep(i, n) {
      for (int j = i; j < n + 1; j++) {
        if (j - 1 >= 0) {
          dp[i][j] = dp[i][j - 1] + data[j - 1];
          maxi = max(maxi, dp[i][j]);
        }
      }
    }
    cout << maxi << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int ans;
int sum;
int n;
int p;
int q;
int num[5010];

int main() {
  while (scanf("%d", &n) == 1 && n) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &num[i]);
    }
    ans = -2147483647;
    sum = 0;
    for (int i = 0; i < n; i++) {
      sum += num[i];
      if (sum > ans) {
        ans = sum;
        q = i;
      }
      if (sum < 0) {
        sum = 0;
        p = i + 1;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int n;
int ans;
int numlist[5010];

void proc();

int main() {
  while (scanf("%d", &n) == 1 && n) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &numlist[i]);
    }
    ans = -1;
    proc();
    printf("%d\\n", ans);
  }
  return 0;
}

void proc() {
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int total = 0;
      for (int k = i; k <= j; k++) {
        total += numlist[k];
      }
      ans = (total > ans) ? total : ans;
    }
  }
}
Problem p00022 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

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

int main(void) {
  int n;
  while (cin >> n, n) {
    int maxs;
    int a[5001] = {0};
    REP(i, n) cin >> a[i];
    maxs = a[0];
    FOR(i, 0, n) {
      int sum = 0;
      FOR(j, i, n) {
        sum += a[j];
        maxs = max(sum, maxs);
      }
    }
    cout << maxs << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

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

int main(void) {
  int n;
  while (cin >> n, n) {
    int sum = 0, maxs = 0, a[10000];
    REP(i, n)
    cin >> a[i];
    FOR(i, 0, n)
    FOR(j, i, n) {
      sum = 0;
      FOR(k, i, j) { sum += a[k]; }
      maxs = max(sum, maxs);
    }

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

#include <iostream>
#include <limits>
#include <numeric>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n, n != 0) {
    vector<int> data(n);
    for (int &temp : data) {
      cin >> temp;
    }

    int sum_max = numeric_limits<int>::min();
    for (int i = 0; i < n; ++i) {
      int sum = 0;
      auto it2 = i;
      for (int j = i; j < n; ++j) {
        sum = sum + data[j];

        if (sum_max < sum) {
          sum_max = sum;
        }
      }
    }
    cout << sum_max << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits>
#include <numeric>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n, n != 0) {
    vector<int> data(n);
    for (int &temp : data) {
      cin >> temp;
    }

    int sum_max = numeric_limits<int>::min();
    for (auto it1 = data.begin(); it1 != data.end(); ++it1) {
      int temp = numeric_limits<int>::min();
      auto it2 = it1;
      do {
        ++it2;
        int sum = accumulate(it1, it2, 0);
        if (sum > temp) {
          temp = sum;
        }

        if (sum_max < temp) {
          sum_max = temp;
        }
      } while (it2 != data.end());
    }
    cout << sum_max << endl;
  }

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

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

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

    int max = INT_MIN;
    int maxBfr = INT_MIN;
    int bfr = INT_MIN;
    int before = INT_MIN;

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

      if (maxBfr > 0) {
        maxBfr += bfr;
      } else {
        maxBfr = bfr;
      }

      int sum;
      sum = maxBfr + temp;

      if (maxBfr != INT_MIN && sum > max && sum > temp) {
        max = sum;
      } else if (temp > max) {
        max = temp;
      }

      bfr = temp;
    }

    cout << max << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int sumData(int data[], int s, int n) {
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += data[s + i];
  }
  return sum;
}

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

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

    int max = INT_MIN;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n - i; j++) {
        int temp = sumData(data, j, i);
        if (temp > max)
          max = temp;
      }
    }

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

#include <stdio.h>
#define INF -1000000000
int ar[5000];
int d[5001];

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

int main() {
  int n, i, j;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &ar[i]);
    }
    d[0] = 0;
    int ans = INF;
    for (i = 1; i < n + 1; i++)
      d[i] = d[i - 1] + ar[i - 1];
    for (i = 0; i < n; i++)
      for (j = i + 1; j < n + 1; j++) {
        ans = max(ans, d[j] - d[i]);
      }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int ar[5000];
int d[5000];

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

int main() {
  int n, i, j;
  while (1) {
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%d", &ar[i]);
    }
    d[0] = 0;
    int ans = 0;
    for (i = 1; i < n + 1; i++)
      d[i] = d[i - 1] + ar[i - 1];
    for (i = 0; i < n; i++)
      for (j = i + 1; j < n + 1; j++) {
        ans = max(ans, d[j] - d[i]);
      }
    printf("%d\\n", ans);
  }
}
Problem p00022 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int n, a;
  int i, j;
  int b[5005] = {};

  while (1) {
    priority_queue<int> pq;
    cin >> n;
    if (n == 0)
      break;
    for (i = 1; i <= n; i++) {
      cin >> a;
      b[i] = b[i - 1] + a;
      for (j = i - 1; j >= 0; j--)
        pq.push(b[i] - b[j]);
    }
    cout << pq.top() << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define li long int
using namespace std;

int main() {
  li n, a;
  li i, j;
  li b[5005] = {};

  while (1) {
    priority_queue<li> pq;
    cin >> n;
    if (n == 0)
      break;
    for (i = 1; i <= n; i++) {
      cin >> a;
      b[i] = b[i - 1] + a;
      for (j = i; j >= 0; j--)
        pq.push(b[i] - b[j]);
    }
    cout << pq.top() << endl;
  }
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string.h>

int n;
int a[5001];

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

  return;
}

int sum(int i) {
  if (i == 0)
    return 0;

  int s = 0;
  while (i > 0) {
    s += a[i];
    i -= i & (-i);
  }

  return s;
}

int main() {
  while (1) {
    memset(a, 0, sizeof(a));

    std::cin >> n;
    if (!n)
      break;

    for (int i = 1; i <= n; i++) {
      int x;
      std::cin >> x;
      add(i, x);
    }

    int ans = -100001;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        ans = std::max(ans, sum(j) - sum(i));
      }
    }

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

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>

int n;
int a[1000];

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

  return;
}

int sum(int i) {
  if (i == 0)
    return 0;

  int s = 0;
  while (i > 0) {
    s += a[i];
    i -= i & (-i);
  }

  return s;
}

int main() {
  while (1) {
    memset(a, 0, sizeof(a));

    std::cin >> n;
    if (!n)
      break;

    for (int i = 1; i <= n; i++) {
      int x;
      std::cin >> x;
      add(i, x);
    }

    int ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        ans = std::max(ans, sum(j) - sum(i - 1));
      }
    }

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

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

#include <climits>
#include <iostream>

using namespace std;

inline int max(int a, int b) { return a > b ? a : b; }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int calc(vector<int> a, int i, int n) {
  int s = 0, j;
  for (j = 0; j < n; j++) {
    s += a[i + j];
  }
  return s;
}

inline int max(int a, int b) { return a > b ? a : b; }

int main(void) {
  int n, i, j;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<int> a(n);
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    int sum = 0, s;
    for (i = 1; i <= n; i++) {
      for (j = 0; j + i <= n; j++) {
        s = calc(a, j, i);
        sum = max(sum, s);
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00022 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n;

  while (cin >> n, n) {

    int num[n], sum, maxSum = -100000;

    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];
        maxSum = max(maxSum, sum);
      }
    }

    cout << maxSum << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n;

  while (cin >> n, n) {

    int num[n];
    int dp[n + 1][n + 1];

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

    memset(dp, 0, sizeof(dp));

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

    cout << dp[n][0] << endl;
  }
}