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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  while (1) {
    int n, k;
    int sum = 0, mx = 0;
    int ans[100000] = {0};
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> ans[i];
    for (int i = 0; i < k; i++) {
      sum += ans[i];
    }
    mx = sum;
    for (int i = k; i < n; i++) {
      sum += ans[i];
      sum -= ans[i - k];
      mx = max(mx, sum);
    }
    cout << mx << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  while (1) {
    int n, k;
    int sum = 0, mx = 0;
    int ans[10000] = {0};
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> ans[i];
    for (int i = 0; i < k; i++) {
      sum += ans[i];
    }
    mx = sum;
    for (int i = k; i < n; i++) {
      sum += ans[i];
      sum -= ans[i - k];
      mx = max(mx, sum);
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

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

int main(void) {
  int n, k;
  while (cin >> n >> k, n + k) {
    vector<int> a(n);

    rep(i, n) cin >> a[i];
    int ret = -100000;
    int sum = 0;
    rep(i, k) sum += a[i];
    for (int i = k; i < n; i++) {
      sum = sum + a[i] - a[i - k];
      ret = max(ret, sum);
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

int main(void) {
  int n, k;
  while (cin >> n >> k, n + k) {
    vector<int> a(n);

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

#include <iostream>

using namespace std;

int main() {

  int n, k;
  int a[100001];

  int max;
  int sum_seq;

  while (cin >> n >> k) {

    if (n == 0 && k == 0)
      break;

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

    max = 0;

    sum_seq = 0;

    for (int i = 0; i < k; i++)
      sum_seq += a[i];

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

      if (max < sum_seq)
        max = sum_seq;

      if (i != n - k)
        sum_seq += a[i + k] - a[i];
    }

    cout << max << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int n, k;
  int a[100001];

  int max;
  int sum_seq;

  while (cin >> n >> k) {

    if (n == 0 && k == 0)
      break;

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

    max = 0;

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

      sum_seq = 0;

      for (int j = i; j < i + k; j++)
        sum_seq += a[j];

      if (max < sum_seq)
        max = sum_seq;
    }

    cout << max << endl;
  }

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

#include <stdio.h>
int main() {
  while (1) {
    int k, n, i, j, x[100000];
    long long int max = 0, sum = 0;
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &x[i]);
    }
    for (i = 0; i < k; i++) {
      sum += x[i];
    }
    for (i = 1; i <= n - k; i++) {
      if (x[i - 1] < x[i + k - 1]) {
        sum = 0;
        for (j = i; j < i + k; j++) {
          sum += x[j];
        }
        if (max < sum)
          max = sum;
      }
    }
    printf("%lld\\n", max);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  while (1) {
    int k, n, i, j, x[100000], max = 0, sum = 0;
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &x[i]);
    }
    for (i = 0; i <= n - k; i++) {
      sum = 0;
      for (j = i; j < i + k; j++) {
        sum += x[j];
      }
      if (max < sum)
        max = sum;
    }
    printf("%d\\n", max);
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Runtime Error

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int main() {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (!n)
      return 0;
    int ret = -INF;
    int res = 0;
    queue<int> que;
    for (int i = 0; i < k; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      que.push(s);
    }
    ret = max(ret, res);
    for (int i = k; i < n; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      res -= (que.front());
      ret = max(ret, res);
      que.pop();
      que.push(s);
    }
    printf("%d\\n", ret);
  }
}

Error: WA

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int main() {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (!n)
      return 0;
    int ret = -INF;
    int res = 0;
    queue<int> que;
    for (int i = 0; i < k; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      que.push(s);
    }
    ret = max(ret, res);
    for (int i = k; i <= n; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      res -= (que.front());
      ret = max(ret, res);
      que.pop();
      que.push(s);
    }
    printf("%d\\n", ret);
  }
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (!n && !k)
      break;

    int sum = 0;
    int ans = 0;
    queue<int> q;
    for (int i = 0; i < n; ++i) {
      int num;
      scanf("%d", &num);

      sum += num;
      if (i == k - 1)
        ans = sum;
      if (i >= k) {
        sum -= q.front();
        q.pop();
        ans = max(ans, sum);
      }
      q.push(num);
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    int sum = 0;
    int ans = 0;
    queue<int> q;
    for (int i = 0; i < n; ++i) {
      int num;
      scanf("%d", &num);

      sum += num;
      if (i == k - 1)
        ans = sum;
      if (i >= k) {
        sum -= q.front();
        q.pop();
        ans = max(ans, sum);
      }
      q.push(num);
    }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main() {
  while (true) {
    int a[100000] = {0};
    int n, k;
    cin >> n >> k;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int ans = 0;
    for (int j = 0; j < k; j++) {
      ans += a[j];
    }
    int ab = ans;
    // cout << "f:" << aa << endl;
    for (int i = k; i < n; i++) {
      int aa = ab - a[i - k] + a[i];
      ab = aa;
      ans = aa > ans ? aa : ans;
      // cout << aaa << ":" << aa << " - " << a[i - k] << " + " << a[i] << endl;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main() {
  while (true) {
    int a[100000] = {0};
    int n, k;
    cin >> n >> k;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int ans = 0;

    // cout << "f:" << aa << endl;
    for (int i = 0; i < n - k; i++) {
      int aa = 0;
      for (int j = 0; j < k; j++) {
        aa += a[j + i];
      }
      ans = aa > ans ? aa : ans;
      // cout << aaa << ":" << aa << " - " << a[i - k] << " + " << a[i] << endl;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(int argc, char const *argv[]) {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0) {
      break;
    }
    vector<int> vec(n);
    int sum_max = 0;
    int j;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> vec[i];

      if (k == i + 1) {
        for (j = 0; j < k; j++) {
          sum += vec[i - j];
        }
        sum_max = max(sum, sum_max);
        sum -= vec[i - k + 1];
      } else if (k < i + 1) {
        sum += vec[i];
        sum_max = max(sum, sum_max);
        sum -= vec[i - k + 1];
      }
    }
    cout << sum_max << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(int argc, char const *argv[]) {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0) {
      break;
    }
    vector<int> vec(n);
    int sum_max = 0;
    for (int i = 0; i < n; i++) {
      int sum = 0;
      cin >> vec[i];
      if (k <= i + 1) {
        for (int j = 0; j < k; j++) {
          sum += vec[i - j];
        }
        sum_max = max(sum, sum_max);
      }
    }
    cout << sum_max << endl;
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  int num[100005];
  while (cin >> n >> k && n && k) {
    for (int i = 0; i < n; i++)
      cin >> num[i];
    int ans = 0;
    for (int i = 0; i < k; i++)
      ans += num[i];
    int sum = ans;
    for (int i = 1; i < n - k + 1; i++) {
      sum -= num[i - 1];
      sum += num[i + k - 1];
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;

  while (1) {
    cin >> n >> k;

    if (n == 0 && k == 0) {
      break;
    } // end if

    vector<int> a(n + 1, 0);
    int res = 0, s = 0;

    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      if (i < k) {
        s += a[i];
      } else if (i == k) {
        s += a[i];
        res = s;
      } else { // if (i > k ){
        s -= a[i - k];
        s += a[i];
        res = max(res, s);
      } // end if
    }   // end for

    cout << res << endl;
  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
// #include <set>
#include <algorithm>
// #include <fstream>

using namespace std;

int main() {
  //	cut here before submit
  //	freopen ("testcase.maximumsum", "r", stdin );
  int n, k;

  while (1) {
    cin >> n >> k;

    if (n == 0 && k == 0) {
      break;
    } // end if
    vector<int> a(n, 0);
    //		vector <int> sum (n - k + 1, 0 );
    //		set <int, greater<int> > sum;
    int res = 0;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    } // end for

    for (int i = 0; i < n - k + 1; ++i) {
      int s = 0;
      for (int j = i; j < i + k; ++j) {
        s += a[j];
      } // end for
      res = max(res, s);
    } // end for

    cout << res << endl;
  } // end loop

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

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

int num[100000];

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

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

    int ans = 0;
    for (int i = k; i < n; i++) {
      cin >> num[i];
      sum -= num[i - k];
      sum += num[i];
      if (ans < sum)
        ans = sum;
    }

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

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, k, num[100000];
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> num[i];

    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (i + k > n)
        break;
      int row = 0;
      for (int j = 0; j < k; j++) {
        row += num[i + j];
      }
      if (ans < row)
        ans = row;
    }

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

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;

    if (n == 0 && k == 0) {
      break;
    } else {
      int i, inp;
      int *a;
      a = new int[n];

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

      int sum1 = 0, sum2 = 0, max = 0;

      for (i = 0; i < k; i++) {
        sum1 += a[i];
      }
      max = sum1;

      for (i = k; i < n; i++) {
        sum2 = sum1 + a[i] - a[i - k];
        if (sum2 > max) {
          max = sum2;
        }
        sum1 = sum2;
      }

      cout << max << endl;

      delete[] a;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;

    if (n == 0 && k == 0) {
      break;
    } else {
      int i, j, inp, max = 0;
      int a[10000];

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

      for (i = 0; i < n - k + 1; i++) {
        int sum = 0;

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

        if (sum > max) {
          max = sum;
        }
      }

      cout << max << endl;
    }
  }

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

#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int a, b, c[100000] = {0}, d = 0, e = 0, max = 0;
    cin >> a >> b;
    if (a == 0 && b == 0)
      break;
    for (int A = 0; A < a; A++) {
      cin >> c[A];
      if (A >= b) {
        for (int B = 0; B < b; B++) {
          d = d + c[A + e];
          e--;
        }
        if (max < d)
          max = d;
        d = 0;
        e = 0;
      }
    }
    cout << max << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int a, b, c[100000] = {0}, d = 0, e = 0, max = 0;
    cin >> a >> b;
    if (a == 0 && b == 0)
      break;
    for (int A = 0; A < a; A++) {
      cin >> c[A];
      if (A >= b) {
        for (int B = 0; B < b; B++) {
          d = d + c[A + e];
          e--;
        }
        if (max < d)
          max = d;
        d = 0;
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int i, n, k;

  while (scanf("%d%d", &n, &k), n, k) {
    int max = 0, temp = 0;
    int val[100000] = {0};

    for (i = 0; i < n; i++) {
      scanf("%d", &val[i]);
      temp += val[i];
      if (i >= k)
        temp -= val[i - k];
      if (i > k - 1 && temp > max)
        max = temp;
    }

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

Error: TLE

Fixed submission:

#include <stdio.h>
int array[100000], n, k, max;

void func(int now) {
  int i, j, temp = 0;

  if (now == n - k)
    return;

  for (i = now; i < now + k; i++) {
    temp += array[i];
  }
  if (temp > max)
    max = temp;
  func(now + 1);
}

int main(void) {
  int i;

  while (scanf("%d", &n), n) {
    max = 0;

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

    printf("%d\\n", max);
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n2[100001];
int main() {
  int n, k;
  int cnt = 0, cnt_max = -10001;
  while (1) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      return 0;
    for (int i = 1; i <= n; i++) {
      cin >> n2[i];
    }
    for (int i = 1; i <= n - k + 1; i++) {
      cnt = n2[i];
      for (int j = 1; j < k; j++) {
        cnt += n2[i + j];
      }
      if (cnt_max < cnt) {
        cnt_max = cnt;
      }
    }
    cout << cnt_max << endl;
    cnt_max = 0;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, k;
  int n2[5001];
  int cnt = 0, cnt_max = -10001;
  while (1) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      return 0;
    for (int i = 1; i <= n; i++) {
      cin >> n2[i];
    }
    for (int i = 1; i <= n - k + 1; i++) {
      cnt = n2[i];
      for (int j = 1; j < k; j++) {
        cnt += n2[i + j];
      }
      if (cnt_max < cnt) {
        cnt_max = cnt;
      }
    }
    cout << cnt_max << endl;
    cnt_max = 0;
  }
}
Problem p00439 (C++) — buggy submission, verdict: Runtime Error

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


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


Fixed submission:

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {

  cout << "56\\n788\\n620404\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a[100000], n, k, s[100000];
  while (1) {
    cin >> n >> k;
    if ((n == 0) && (k == 0)) {
      return 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      s[i] = 0;
    }

    for (int i = 0; i < n - k; i++) {
      for (int j = i; j < i + k; j++) {
        s[i] += a[j];
      }
    }

    for (int i = 1; i < n; i++) {
      for (int j = 1; j < n - i; j++) {
        if (s[j - 1] < s[j]) {
          k = s[j - 1];
          s[j - 1] = s[j];
          s[j] = k;
        }
      }
    }

    cout << s[0] << "\\n";
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a || b) {
    vector<int> in(a);
    rep(i, a) cin >> in[i];
    int sum = 0;
    rep(i, b) sum += in[i];
    int ma = sum;
    rep(i, a - b) {
      sum -= in[i];
      sum += in[i + b];
      ma = max(ma, sum);
    }
    cout << ma << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a || b) {
    vector<int> in(a);
    rep(i, a) cin >> in[i];
    int ma = 0;
    rep(i, a - b + 1) {
      int sum = 0;
      loop(j, i, i + b) sum += in[j];
      ma = max(ma, sum);
    }
    cout << ma << endl;
  }
}
Problem p00439 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int main(void) {
  int i, n, k, sum, max;
  int N[150000];

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

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

    for (i = 0; i < k; i++)
      sum += N[i];

    max = sum;

    for (i = k; i < n; i++) {
      sum += N[i];
      sum -= N[i - k];
      if (sum > max)
        max = sum;
    }

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

Error: 0

Fixed submission:

#include <stdio.h>

int main(void) {
  int i, n, k, sum, max;
  int N[15000];

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

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

    for (i = 0; i < k; i++)
      sum += N[i]; // 初期値

    max = sum;

    for (i = k; i < n; i++) {
      sum += N[i];
      sum -= N[i - k];
      if (sum > max)
        max = sum;
    }

    printf("%d\\n", max);
  }
  return 0;
}
Problem p00439 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

ll a[100001];
ll memo[100001]; // i???????????§?¶?????????????????¨?(i???????????????)
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k), n) {
    rep(i, n) scanf("%lld", &a[i]);
    ll sum = 0;
    rep(i, n + 1) {
      memo[i] = sum;
      sum += a[i];
    }
    ll Max = -INF;
    rep(i, n - k + 1) {
      sum = memo[i + k] - memo[i];
      Max = max(Max, sum);
    }
    printf("%lld\\n", Max);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int a[100000];
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k), n) {
    rep(i, n) scanf("%d", &a[i]);
    int Max = -INF;
    rep(i, n - k + 1) {
      int sum = 0;
      rep(j, k) { sum += a[i + j]; }
      Max = max(Max, sum);
    }
    printf("%d\\n", Max);
  }
  return 0;
}