Problem p03839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ll long long
#define ld double
#define ull unsigned long long
#define PI pair<int, int>

const int N = 1e4 + 123;
const int M = 123;
const ld Pi = acos(-1);
const ll Inf = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const int Sz = 501;
const int MOD = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int mult(int a, int b) { return 1ll * a * b % mod; }
int sum(int a, int b) {
  add(a, b);
  return a;
}

int n, k, a[N];
ll s[N], p[N], S[N];

void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[i] = p[i - 1] + max(0, a[i]);
    S[i] = S[i - 1] + a[i];
  }
  for (int i = n; i; i--) {
    s[i] = s[i + 1] + max(0, a[i]);
  }
  ll ans = -1e18;
  for (int i = 1; i + k - 1 <= n; i++) {
    ll now = p[i - 1] + s[i + k] + max(0ll, S[i + k - 1] - S[i - 1]);
    ans = max(ans, now);
  }
  cout << ans << endl;
}

int main() {
#ifdef wws
  freopen("in", "r", stdin);
  // freopen("in", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  int tt = 1;
  while (tt--)
    solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ll long long
#define ld double
#define ull unsigned long long
#define PI pair<int, int>

const int N = 1e5 + 123;
const int M = 123;
const ld Pi = acos(-1);
const ll Inf = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const int Sz = 501;
const int MOD = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int mult(int a, int b) { return 1ll * a * b % mod; }
int sum(int a, int b) {
  add(a, b);
  return a;
}

int n, k, a[N];
ll s[N], p[N], S[N];

void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[i] = p[i - 1] + max(0, a[i]);
    S[i] = S[i - 1] + a[i];
  }
  for (int i = n; i; i--) {
    s[i] = s[i + 1] + max(0, a[i]);
  }
  ll ans = -1e18;
  for (int i = 1; i + k - 1 <= n; i++) {
    ll now = p[i - 1] + s[i + k] + max(0ll, S[i + k - 1] - S[i - 1]);
    ans = max(ans, now);
  }
  cout << ans << endl;
}

int main() {
#ifdef wws
  freopen("in", "r", stdin);
  // freopen("in", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  int tt = 1;
  while (tt--)
    solve();
  return 0;
}