prithivMLmods/Coder-Stat huggingface dataset:
Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long BIG = 1000000007;

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  long N, K;
  long long ans = 0;
  cin >> N >> K;

  vector<long> vec(N + 10, -1);
  for (int i = K; i > 0; i--) {
    vec[i] = modpow(K / i, N, BIG);
    for (int j = i * 2; j <= K; j += i) {
      vec[i] = (vec[i] - vec[j] + BIG) % BIG;
    }
    ans = (ans % BIG + vec[i] * i % BIG) % BIG;
    // cout << ans << endl;
  }

  cout << ans << endl;
}

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

long BIG = 1000000007;

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  long N, K;
  long long ans = 0;
  cin >> N >> K;

  vector<long> vec(K + 10, -1);
  for (int i = K; i > 0; i--) {
    vec[i] = modpow(K / i, N, BIG);
    for (int j = i * 2; j <= K; j += i) {
      vec[i] = (vec[i] - vec[j] + BIG) % BIG;
    }
    ans = (ans % BIG + vec[i] * i % BIG) % BIG;
    // cout << ans << endl;
  }

  cout << ans << endl;
}
Problem p02715 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
ll kpow(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans *= a;
    ans %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ans;
}
ll dp[maxn];
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    dp[i] = kpow(k / i, n); // 表示最小公因数为i的排列组合的数量
  }

  for (int i = k; i >= 1; i--) {
    for (int j = i * 2; j <= k; j + i) {
      dp[i] -= dp[j];
      dp[i] = (dp[i] + mod) % mod;
    }
  }
  ll ans = 0;
  for (int i = k; i >= 1; i--) {
    ans += (i * dp[i]);
    ans %= mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
ll kpow(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans *= a;
    ans %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ans;
}
ll dp[maxn];
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    dp[i] = kpow(k / i, n); // 表示最小公因数为i的排列组合的数量
  }

  for (int i = k; i >= 1; i--) {
    for (int j = i * 2; j <= k; j = j + i) {
      dp[i] -= dp[j];
      dp[i] = (dp[i] + mod) % mod;
    }
  }
  ll ans = 0;
  for (int i = k; i >= 1; i--) {
    ans += (i * dp[i]);
    ans %= mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
Problem p02715 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll M = 1e9 + 7;
ll mypw(ll n, ll m) {
  ll k = 1;
  for (ll i = 0; i < m; i++) {
    k = (k * (n % M)) % M;
  }
  return k;
}
int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  vector<ll> c(k + 1);
  for (ll i = k; i >= 1; i--) {
    c[i] = mypw(k / i, n);
    for (ll j = i * 2; j <= k; j += i) {
      c[i] -= c[j];
      if (c[i] < 0)
        c[i] += M;
    }
    ans = (ans + (c[i] * i) % M) % M;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll M = 1e9 + 7;
ll mypw(ll n, ll m) {
  if (m <= 1)
    return n;
  ll k = mypw(n, m / 2) % M;
  k = (k * k) % M;
  if (m & 1)
    return (k * n) % M;
  else
    return k;
}
int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  vector<ll> c(k + 1);
  for (ll i = k; i >= 1; i--) {
    c[i] = mypw(k / i, n);
    for (ll j = i * 2; j <= k; j += i) {
      c[i] -= c[j];
      if (c[i] < 0)
        c[i] += M;
    }
    ans = (ans + (c[i] * i) % M) % M;
  }
  cout << ans << endl;
  return 0;
}
Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll MOD = 1000000007;

ll modpow(ll a, ll n = MOD - 2) {
  ll r = 1;
  a %= MOD;
  while (n) {
    r = r * ((n % 2) ? a : 1) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }

  return r;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;

  ll sum = 0;
  vector<ll> dp(10001, 0);
  for (int i = K; i >= 1; i--) {
    dp[i] = modpow(K / i, N);
    for (int j = i * 2; j <= K; j += i)
      dp[i] -= dp[j];
    sum += i * dp[i] % MOD;
  }
  cout << sum % MOD << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll MOD = 1000000007;

ll modpow(ll a, ll n = MOD - 2) {
  ll r = 1;
  a %= MOD;
  while (n) {
    r = r * ((n % 2) ? a : 1) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }

  return r;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;

  ll sum = 0;
  vector<ll> dp(100001, 0);
  for (int i = K; i >= 1; i--) {
    dp[i] = modpow(K / i, N);
    for (int j = i * 2; j <= K; j += i)
      dp[i] -= dp[j];
    sum += i * dp[i] % MOD;
  }
  cout << sum % MOD << endl;

  return 0;
}
Problem p02715 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
typedef long long ll;
typedef unsigned long long ull;
const double EPS = (1e-9);
using namespace std;
int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(int num, int idx) { return num ^ (1 << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 100000 + 9, mod = 1000000000 + 7;
ll cnt[N], fres[N];
int go(int n) {

  for (int i = 1; i * i <= n; i++) {

    if (n % i == 0) {
      cnt[i]++;
      if (n / i != i)
        cnt[n / i]++;
    }
  }
}
ll power(ll b, ll p) {

  if (p == 0)
    return 1;
  if ((p & (1ll))) {
    return b % mod * power((b * b) % mod, p / 2) % mod;
  } else {
    return power((b * b) % mod, p / 2);
  }
}
int main() {
  GO();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    go(i);
  }
  ll ans = 0;
  for (int i = k; i >= 1; i--) {
    fres[i] = power(cnt[i], n);
    for (int j = i * 2; j <= k; j += i) {
      fres[i] = ((fres[i] - fres[j]) + mod) % mod;
    }
    ans += (1ll * fres[i] * i) % mod;
    if (ans >= mod)
      ans -= mod;
  }
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
typedef long long ll;
typedef unsigned long long ull;
const double EPS = (1e-9);
using namespace std;
int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(int num, int idx) { return num ^ (1 << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 100000 + 9, mod = 1000000000 + 7;
ll cnt[N], fres[N];
void go(int n) {

  for (int i = 1; i * i <= n; i++) {

    if (n % i == 0) {
      cnt[i]++;
      if (n / i != i)
        cnt[n / i]++;
    }
  }
}
ll power(ll b, ll p) {

  if (p == 0)
    return 1;
  if ((p & (1ll))) {
    return b % mod * power((b * b) % mod, p / 2) % mod;
  } else {
    return power((b * b) % mod, p / 2);
  }
}
int main() {
  GO();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    go(i);
  }
  ll ans = 0;
  for (int i = k; i >= 1; i--) {
    fres[i] = power(cnt[i], n);
    for (int j = i * 2; j <= k; j += i) {
      fres[i] = ((fres[i] - fres[j]) + mod) % mod;
    }
    ans += (1ll * fres[i] * i) % mod;
    if (ans >= mod)
      ans -= mod;
  }
  cout << ans << "\n";
}
Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
const ll mod = 1000000007;

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> d(N + 1);
  for (int i = K; i >= 1; i--) {
    ll tmp = modpow(K / i, N);
    for (int j = 2; j * i <= K; j++) {
      tmp = (tmp + mod - d[j * i]) % mod;
    }
    d[i] = tmp;
  }

  ll ans = 0;
  for (int i = 1; i <= K; i++) {
    ans = (ans + i * d[i]) % mod;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
const ll mod = 1000000007;

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, K;
  cin >> N >> K;

  vector<ll> d(K + 1);
  for (int i = K; i >= 1; i--) {
    ll tmp = modpow(K / i, N);
    for (int j = 2; j * i <= K; j++) {
      tmp = (tmp + mod - d[j * i]) % mod;
    }
    d[i] = tmp;
  }

  ll ans = 0;
  for (int i = 1; i <= K; i++) {
    ans = (ans + i * d[i]) % mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 10000000000000007;
const int MOD = 1000000007;
int dp[5][2][300000];
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 5, -inf);
  rep1(i, n) cin >> a[i];
  rep1(i, n) { a[i] += MOD; }
  int res = 0;
  rep(i, 5) {
    rep(j, 300000) {
      dp[i][0][j] = 0;
      dp[i][1][j] = 0;
    }
  }

  rep1(i, n) {
    dp[0][0][i] = dp[0][1][i - 1];
    dp[0][1][i] = dp[0][0][i - 1] + a[i];
    dp[1][0][i] = max(dp[0][0][i - 1], dp[1][1][i - 1]);
    dp[1][1][i] = dp[1][0][i - 1] + a[i];
    dp[2][0][i] = max(dp[2][1][i - 1], max(-inf, dp[1][0][i - 1]));
    dp[2][1][i] = dp[2][0][i - 1] + a[i];
    if (i <= 1) {
      dp[1][1][i] = 0;
      // dp[1][0][i]=0;
    }
    if (i <= 1) {
      dp[2][1][i] = 0;
      // dp[2][0][i]=0;
    }
  }
  if (n % 2 == 0) {
    cout << max(dp[0][1][n], max(dp[0][0][n], dp[1][1][n])) - MOD * n / 2
         << endl;
  } else {
    // exit(1);
    res = max(-max(dp[2][0][n], dp[2][1][n]), max(dp[1][1][n], dp[1][0][n]));
    if (res == dp[2][0][n])
      exit(1);
    cout << res - n / (int)2 * MOD << endl;
    // cout<<dp[2][0][n]<<"\n";
    // cout<<dp[2][1][n]<<"\n";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 10000000000000007;
const int MOD = 1000000007;
int dp[5][2][300000];
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 5, -inf);
  rep1(i, n) cin >> a[i];
  rep1(i, n) { a[i] += MOD; }
  int res = 0;
  rep(i, 5) {
    rep(j, 300000) {
      dp[i][0][j] = 0;
      dp[i][1][j] = 0;
    }
  }

  rep1(i, n) {
    dp[0][0][i] = dp[0][1][i - 1];
    dp[0][1][i] = dp[0][0][i - 1] + a[i];
    dp[1][0][i] = max(dp[0][0][i - 1], dp[1][1][i - 1]);
    dp[1][1][i] = dp[1][0][i - 1] + a[i];
    dp[2][0][i] = max(dp[2][1][i - 1], max(-inf, dp[1][0][i - 1]));
    dp[2][1][i] = dp[2][0][i - 1] + a[i];
    if (i <= 1) {
      dp[1][1][i] = 0;
      // dp[1][0][i]=0;
    }
    if (i <= 1) {
      dp[2][1][i] = 0;
      // dp[2][0][i]=0;
    }
  }
  if (n % 2 == 0) {
    cout << max(dp[0][1][n], max(dp[0][0][n], dp[1][1][n])) - MOD * n / 2
         << endl;
  } else {
    // exit(1);
    res = max(max(-dp[2][0][n], dp[2][1][n]), max(dp[1][1][n], dp[1][0][n]));
    if (res == -dp[2][0][n])
      exit(1);
    cout << res - n / (int)2 * MOD << endl;
    // cout<<dp[2][0][n]<<"\n";
    // cout<<dp[2][1][n]<<"\n";
  }
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int MaxN = 1e5 + 10;
int N;
int A[MaxN];

void solve() {
  vector<ll> prefix(N + 1);
  vector<ll> f(N + 1);
  prefix[1] = A[1];
  for (int i = 3; i <= N; i += 2)
    prefix[i] = prefix[i - 2] + A[i];
  for (int n = 2; n <= N; n++) {
    if (n & 1)
      f[n] = max(f[n - 1], A[n] + f[n - 2]);
    else
      f[n] = max(prefix[n - 1], A[n] + f[n - 2]);
  }
  cout << f[N] << endl;
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  solve();
  return 0;
}


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

using ll = long long;
const int MaxN = 2e5 + 10;
int N;
int A[MaxN];

void solve() {
  vector<ll> prefix(N + 1);
  vector<ll> f(N + 1);
  prefix[1] = A[1];
  for (int i = 3; i <= N; i += 2)
    prefix[i] = prefix[i - 2] + A[i];
  for (int n = 2; n <= N; n++) {
    if (n & 1)
      f[n] = max(f[n - 1], A[n] + f[n - 2]);
    else
      f[n] = max(prefix[n - 1], A[n] + f[n - 2]);
  }
  cout << f[N] << endl;
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  solve();
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll maxs = 2e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'

void pre() {}

ll n;
ll a[maxs];
ll dp[maxs][4];
bool vis[maxs][4];

ll go(ll pos, ll have) {
  if (pos >= n) {
    if (have <= 1)
      return 0;
    return -oo;
  }
  ll &ans = dp[pos][have];
  if (vis[pos][have])
    return ans;
  vis[pos][have] = true;

  if (!have)
    ans = a[pos] + go(pos + 2, have);
  else
    ans = max(a[pos] + go(pos + 2, have), go(pos + 1, have - 1));
}

void solve() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  ll ans;
  if (n & 1) {
    ans = max(go(0, 2), go(1, 1));
  } else {
    ans = max(go(0, 1), go(1, 0));
  }
  cout << ans << endl;
}

int main() {
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll maxs = 2e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'

void pre() {}

ll n;
ll a[maxs];
ll dp[maxs][4];
bool vis[maxs][4];

ll go(ll pos, ll have) {
  if (pos >= n) {
    if (have <= 1)
      return 0;
    return -oo;
  }
  ll &ans = dp[pos][have];
  if (vis[pos][have])
    return ans;
  vis[pos][have] = true;

  if (!have)
    ans = a[pos] + go(pos + 2, have);
  else
    ans = max(a[pos] + go(pos + 2, have), go(pos + 1, have - 1));

  return ans;
}

void solve() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  ll ans;
  if (n & 1) {
    ans = max(go(0, 2), go(1, 1));
  } else {
    ans = max(go(0, 1), go(1, 0));
  }
  cout << ans << endl;
}

int main() {
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int j, n, m, po, no, flag, cnt = 0, cnt2, ar[100000], x, y, i,
                                       co[100000], sum = 0, k, ans = INT_MIN,
                                       sum1 = 0;
  string str[1000], ptr, sr, tr, rtr, rt;
  vector<long long int> v1, v2;
  set<string> s;
  queue<long long int> qu;
  set<string>::iterator it;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  co[0] = 0;
  co[1] = 0;
  x = ar[1];
  for (i = 2; i <= n; i++) {
    if (i % 2 == 0)
      co[i] = max((co[i - 2] + ar[i]), x);
    else {
      co[i] = max((co[i - 2] + ar[i]), co[i - 1]);
      x += ar[i];
    }
  }

  cout << co[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int j, n, m, po, no, flag, cnt = 0, cnt2, ar[200001], x, y, i,
                                       co[200001], sum = 0, k, ans = INT_MIN,
                                       sum1 = 0;
  string str[1000], ptr, sr, tr, rtr, rt;
  vector<long long int> v1, v2;
  set<string> s;
  queue<long long int> qu;
  set<string>::iterator it;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  co[0] = 0;
  co[1] = 0;
  x = ar[1];
  for (i = 2; i <= n; i++) {
    if (i % 2 == 0)
      co[i] = max((co[i - 2] + ar[i]), x);
    else {
      co[i] = max((co[i - 2] + ar[i]), co[i - 1]);
      x += ar[i];
    }
  }

  cout << co[n] << endl;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 1e5 + 7;

LL dp[maxn];
LL a[maxn];
LL sum[maxn];

int main() {
  int n;
  while (~sd(n)) {
    rep(i, 1, n) slld(a[i]);
    sum[1] = a[1];
    rep(i, 1, n) {
      if (i & 1)
        sum[i] = sum[i - 1] + a[i];
      else
        sum[i] = sum[i - 1];
    }
    mem(dp);
    rep(i, 2, n) {
      if (i & 1)
        dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
      else
        dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
    }
    plld(dp[n]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 7;

LL dp[maxn];
LL a[maxn];
LL sum[maxn];

int main() {
  int n;
  while (~sd(n)) {
    rep(i, 1, n) slld(a[i]);
    sum[1] = a[1];
    rep(i, 1, n) {
      if (i & 1)
        sum[i] = sum[i - 1] + a[i];
      else
        sum[i] = sum[i - 1];
    }
    mem(dp);
    rep(i, 2, n) {
      if (i & 1)
        dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
      else
        dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
    }
    plld(dp[n]);
  }
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
const ll MOD = 1000000007, MOD2 = 998244353, INF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  vector<vector<ll>> dp(n, vector<ll>(5, -INF));
  dp.at(0).at(0) = a.at(0);
  dp.at(1).at(1) = a.at(1);
  dp.at(2).at(2) = a.at(2);
  int yutori;
  if (n % 2 == 0) {
    yutori = 1;
  } else {
    yutori = 2;
  }
  rep(i, n) {
    rep(j, 3) {
      if (i + 2 <= n - 1)
        chmax(dp.at(i + 2).at(j), dp.at(i).at(j) + a.at(i + 2));
      if (i + 3 <= n - 1)
        chmax(dp.at(i + 3).at(j + 1), dp.at(i).at(j) + a.at(i + 3));
      if (i + 4 <= n - 1)
        chmax(dp.at(i + 4).at(j + 2), dp.at(i).at(j) + a.at(i + 4));
    }
  }
  ll ans = dp.at(n - 1).at(yutori);
  chmax(ans, dp.at(n - 2).at(yutori - 1));
  if (yutori == 2)
    chmax(ans, dp.at(n - 3).at(yutori - 2));
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
const ll MOD = 1000000007, MOD2 = 998244353, INF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  vector<vector<ll>> dp(n, vector<ll>(5, -INF));
  dp.at(0).at(0) = a.at(0);
  dp.at(1).at(1) = a.at(1);
  if (n > 2)
    dp.at(2).at(2) = a.at(2);
  int yutori;
  if (n % 2 == 0) {
    yutori = 1;
  } else {
    yutori = 2;
  }
  rep(i, n) {
    rep(j, 3) {
      if (i + 2 <= n - 1)
        chmax(dp.at(i + 2).at(j), dp.at(i).at(j) + a.at(i + 2));
      if (i + 3 <= n - 1)
        chmax(dp.at(i + 3).at(j + 1), dp.at(i).at(j) + a.at(i + 3));
      if (i + 4 <= n - 1)
        chmax(dp.at(i + 4).at(j + 2), dp.at(i).at(j) + a.at(i + 4));
    }
  }
  ll ans = dp.at(n - 1).at(yutori);
  chmax(ans, dp.at(n - 2).at(yutori - 1));
  if (yutori == 2)
    chmax(ans, dp.at(n - 3).at(yutori - 2));
  cout << ans << endl;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define enld '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int k = 1 + N % 2;
  vector<vector<ll>> dp(N + 1, vector<ll>(3, -INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < k + 1; j++) {
      chmax(dp[i + 1][j + 1], dp[i][j]);
      ll now = dp[i][j];
      if ((i + j) % 2 == 0)
        now += A[i];
      chmax(dp[i + 1][j], now);
    }
  }
  cout << dp[N][k] << enld;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define enld '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int k = 1 + N % 2;
  vector<vector<ll>> dp(N + 1, vector<ll>(4, -INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < k + 1; j++) {
      chmax(dp[i + 1][j + 1], dp[i][j]);
      ll now = dp[i][j];
      if ((i + j) % 2 == 0)
        now += A[i];
      chmax(dp[i + 1][j], now);
    }
  }
  cout << dp[N][k] << enld;
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

typedef long long ll;
const int maxn = 4e5 + 3;
const ll ninf = std::numeric_limits<ll>::min();
int n, a[maxn];
std::map<int, ll> s[maxn];

void filter(int i) {
  static std::map<int, ll> t;
  t.clear();
  int r = (i + 1) / 2 + 5, l = r - 100;
  for (auto &&[u, v] : s[i]) {
    if (u < l || u > r)
      continue;
    t.emplace(u, v);
  }
  t.swap(s[i]);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  s[0][0] = 0;
  s[1][0] = 0;
  s[1][1] = a[0];
  s[2][0] = 0;
  s[2][1] = a[1];
  for (int i = 3; i <= n; ++i) {
    auto &cs = s[i];
    for (int j = std::max(i - 4, 0); j <= i - 2; ++j) {
      for (auto &&[x, y] : s[j]) {
        if (cs.find(x + 1) == cs.end())
          cs[x + 1] = y + a[i - 1];
        else
          cs[x + 1] = std::max(cs[x + 1], y + a[i - 1]);
      }
    }
    filter(i);
  }
  ll ans = ninf;
  for (int i = 0; i <= n; ++i) {
    if (s[i].find(n / 2) != s[i].end()) {
      ans = std::max(ans, s[i][n / 2]);
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;
const int maxn = 4e5 + 3;
const ll ninf = std::numeric_limits<ll>::min();
int n, a[maxn];
std::map<int, ll> s[maxn];

void filter(int i) {
  static std::map<int, ll> t;
  t.clear();
  int r = (i + 1) / 2 + 5, l = r - 20;
  for (auto &&[u, v] : s[i]) {
    if (u < l || u > r)
      continue;
    t.emplace(u, v);
  }
  t.swap(s[i]);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  s[0][0] = 0;
  s[1][0] = 0;
  s[1][1] = a[0];
  s[2][0] = 0;
  s[2][1] = a[1];
  for (int i = 3; i <= n; ++i) {
    auto &cs = s[i];
    for (int j = std::max(i - 4, 0); j <= i - 2; ++j) {
      for (auto &&[x, y] : s[j]) {
        if (cs.find(x + 1) == cs.end())
          cs[x + 1] = y + a[i - 1];
        else
          cs[x + 1] = std::max(cs[x + 1], y + a[i - 1]);
      }
    }
    filter(i);
  }
  ll ans = ninf;
  for (int i = 0; i <= n; ++i) {
    if (s[i].find(n / 2) != s[i].end()) {
      ans = std::max(ans, s[i][n / 2]);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
ll dp[N], sum[N], a[N];
int main() {
  int n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i == 1)
      sum[i] = a[i];
    else
      sum[i] = sum[i - 2] + a[i];
  }
  for (int i = 2; i <= n; i++) {
    if (i & 1)
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    else
      dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
  }
  printf("%lld\n", dp[n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
ll dp[N], sum[N], a[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i == 1)
      sum[i] = a[i];
    else
      sum[i] = sum[i - 2] + a[i];
  }
  for (int i = 2; i <= n; i++) {
    if (i & 1)
      dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);
    else
      dp[i] = max(dp[i - 2] + a[i], sum[i - 1]);
  }
  printf("%lld\n", dp[n]);
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[100010], n;
map<int, ll> mem[100010];
ll dp(int x, int nm) {
  if (nm == 1) {
    if (x >= n)
      return -1e17;
    return a[x];
  }
  if (n - x < (nm - 1) * 2 + 1) {
    return -1e17;
  }
  if (mem[x].count(nm)) {
    return mem[x][nm];
  }
  ll res = a[x];
  res = a[x] + dp(x + 2, nm - 1);
  // if((n-x-2)>2*(nm-1)){
  res = max(res, a[x] + dp(x + 3, nm - 1));
  // }
  mem[x][nm] = res;
  return res;
}
int main() {
  while (cin >> n) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      mem[i].clear();
      scanf("%d", a + i);
    }
    ans = max(dp(0, n / 2), dp(1, n / 2));
    ans = max(ans, dp(2, n / 2));
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[200010], n;
map<int, ll> mem[200010];
ll dp(int x, int nm) {
  if (nm == 1) {
    if (x >= n)
      return -1e17;
    return a[x];
  }
  if (n - x < (nm - 1) * 2 + 1) {
    return -1e17;
  }
  if (mem[x].count(nm)) {
    return mem[x][nm];
  }
  ll res = a[x];
  res = a[x] + dp(x + 2, nm - 1);
  // if((n-x-2)>2*(nm-1)){
  res = max(res, a[x] + dp(x + 3, nm - 1));
  // }
  mem[x][nm] = res;
  return res;
}
int main() {
  while (cin >> n) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      mem[i].clear();
      scanf("%d", a + i);
    }
    ans = max(dp(0, n / 2), dp(1, n / 2));
    ans = max(ans, dp(2, n / 2));
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main() {

  long long int n;
  cin >> n;

  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    v.push_back(in);
  }

  long long int d[3][114514] = {};
  d[0][0] = v[0];
  d[1][1] = v[1];

  for (long long int i = 1; i < n; i++) {
    if (i >= 2) {
      d[0][i] = d[0][i - 2] + v[i];
      d[1][i] = d[1][i - 2] + v[i];
      d[2][i] = d[2][i - 2] + v[i];
    }
    if (i >= 3) {
      d[1][i] = max(d[0][i - 3] + v[i], d[1][i]);
      d[2][i] = max(d[1][i - 3] + v[i], d[2][i]);
    }
    if (i >= 4) {
      d[2][i] = max(d[0][i - 4] + v[i], d[2][i]);
    }
  }

  long long int ans = 0;
  if ((n % 2) == 0) {
    ans = max(d[0][n - 2], d[1][n - 1]);
  } else {
    ans = max(d[0][n - 3], d[1][n - 2]);
    ans = max(ans, d[2][n - 1]);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main() {

  long long int n;
  cin >> n;

  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    v.push_back(in);
  }

  long long int d[3][214514] = {};
  d[0][0] = v[0];
  d[1][1] = v[1];

  for (long long int i = 1; i < n; i++) {
    if (i >= 2) {
      d[0][i] = d[0][i - 2] + v[i];
      d[1][i] = d[1][i - 2] + v[i];
      d[2][i] = d[2][i - 2] + v[i];
    }
    if (i >= 3) {
      d[1][i] = max(d[0][i - 3] + v[i], d[1][i]);
      d[2][i] = max(d[1][i - 3] + v[i], d[2][i]);
    }
    if (i >= 4) {
      d[2][i] = max(d[0][i - 4] + v[i], d[2][i]);
    }
  }

  long long int ans = 0;
  if ((n % 2) == 0) {
    ans = max(d[0][n - 2], d[1][n - 1]);
  } else {
    ans = max(d[0][n - 3], d[1][n - 2]);
    ans = max(ans, d[2][n - 1]);
  }
  cout << ans << endl;

  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1001001001001001;

ll dp[200001];
ll a[200001];
ll dpall[200001];
int n;

ll init() {
  rep(i, 200001) dp[i] = -INF;
  rep(i, 200001) dpall[i] = -INF;
}

ll rec(int x, int k) {
  // cout << "x:" << x << "k:" << k << endl;
  if (k * 2 > x)
    return dpall[x];
  if (k == 0)
    return ll(0);
  if (dp[x] > -INF)
    return dp[x];

  ll res = -INF;

  ll temp = 0;
  temp += a[x - 1];
  temp += rec(x - 2, k - 1);
  // cout << "x-2:k-1 " << x-2 << " : "  << k-1 << "  " << temp << endl;
  res = max(res, temp);

  temp = 0;
  temp += a[x - 2];
  temp += rec(x - 3, k - 1);
  /*
  cout << "x-3:k-1 " << x-3 << " : "  << k-1 << "  " << temp << endl;
  cout << a[x-2] << " " << rec(x-3,k-1) << endl;
  */
  res = max(res, temp);

  if (x % 2 == 0)
    return dp[x] = res;

  temp = 0;
  temp = dpall[x - 2];
  // cout << "x-2:k " << x-2 << " : "  << k << "  " << temp << endl;
  res = max(res, temp);

  return dp[x] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  init();
  for (int i = 1; i <= n / 2; ++i) {
    if (i == 1) {
      dpall[1] = a[0];
      continue;
    }
    dpall[2 * i - 1] = dpall[2 * i - 3] + a[2 * i - 2];
  }

  /*
  cout << "dpall" << endl;
  for(int i=1; i<=n; i+=2){
      cout <<"i" << i <<" : "<<dpall[i] << endl;
  }*/

  cout << rec(n, n / 2) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1001001001001001;

ll dp[200001];
ll a[200001];
ll dpall[200001];
int n;

ll init() {
  rep(i, 200001) dp[i] = -INF;
  rep(i, 200001) dpall[i] = -INF;
  return 0;
}

ll rec(int x, int k) {
  // cout << "x:" << x << "k:" << k << endl;
  if (k * 2 > x)
    return dpall[x];
  if (k == 0)
    return ll(0);
  if (dp[x] > -INF)
    return dp[x];

  ll res = -INF;

  ll temp = 0;
  temp += a[x - 1];
  temp += rec(x - 2, k - 1);
  // cout << "x-2:k-1 " << x-2 << " : "  << k-1 << "  " << temp << endl;
  res = max(res, temp);

  temp = 0;
  temp += a[x - 2];
  temp += rec(x - 3, k - 1);
  /*
  cout << "x-3:k-1 " << x-3 << " : "  << k-1 << "  " << temp << endl;
  cout << a[x-2] << " " << rec(x-3,k-1) << endl;
  */
  res = max(res, temp);

  if (x % 2 == 0)
    return dp[x] = res;

  temp = 0;
  temp = dpall[x - 2];
  // cout << "x-2:k " << x-2 << " : "  << k << "  " << temp << endl;
  res = max(res, temp);

  return dp[x] = res;
}

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  init();
  for (int i = 1; i <= n / 2; ++i) {
    if (i == 1) {
      dpall[1] = a[0];
      continue;
    }
    dpall[2 * i - 1] = dpall[2 * i - 3] + a[2 * i - 2];
  }

  /*
  cout << "dpall" << endl;
  for(int i=1; i<=n; i+=2){
      cout <<"i" << i <<" : "<<dpall[i] << endl;
  }*/

  cout << rec(n, n / 2) << endl;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
const ll INF = 9999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vl A;
  ll dp[n][4]; // どこまで見たか・i番目まで見たときにi-(使った数)*2+2
  rep(i, n) {
    cin >> a;
    A.push_back(a);
  }

  rep(i, n + 1) {
    if (i == 0) {
      dp[0][0] = A[0];
      dp[0][1] = -INF;
      dp[0][2] = -INF;
    } else {
      if (i == 1)
        dp[i][0] = -INF;
      else
        dp[i][0] = dp[i - 2][0] + A[i];
      if (i == 1)
        dp[i][1] = A[i];
      else
        dp[i][1] = dp[i - 2][1] + A[i];
      if (i > 2)
        dp[i][1] = max(dp[i - 3][0] + A[i], dp[i][1]);
      if (i == 1)
        dp[i][2] = -INF;
      else if (i == 2)
        dp[i][2] = A[i];
      else
        dp[i][2] = dp[i - 2][2] + A[i];
      if (i > 2)
        dp[i][2] = max(dp[i - 3][1] + A[i], dp[i][2]);
      if (i > 3)
        dp[i][2] = max(dp[i - 4][0] + A[i], dp[i][2]);
    }
  }

  if (n % 2 == 0)
    ans = max(dp[n - 2][0], dp[n - 1][1]);
  else
    ans = max({dp[n - 3][0], dp[n - 2][1], dp[n - 1][2]});

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
const ll INF = 9999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vl A;
  ll dp[n][4]; // どこまで見たか・i番目まで見たときにi-(使った数)*2+2
  rep(i, n) {
    cin >> a;
    A.push_back(a);
  }

  rep(i, n) {
    if (i == 0) {
      dp[0][0] = A[0];
      dp[0][1] = -INF;
      dp[0][2] = -INF;
    } else {
      if (i == 1)
        dp[i][0] = -INF;
      else
        dp[i][0] = dp[i - 2][0] + A[i];
      if (i == 1)
        dp[i][1] = A[i];
      else
        dp[i][1] = dp[i - 2][1] + A[i];
      if (i > 2)
        dp[i][1] = max(dp[i - 3][0] + A[i], dp[i][1]);
      if (i == 1)
        dp[i][2] = -INF;
      else if (i == 2)
        dp[i][2] = A[i];
      else
        dp[i][2] = dp[i - 2][2] + A[i];
      if (i > 2)
        dp[i][2] = max(dp[i - 3][1] + A[i], dp[i][2]);
      if (i > 3)
        dp[i][2] = max(dp[i - 4][0] + A[i], dp[i][2]);
    }
  }

  if (n % 2 == 0)
    ans = max(dp[n - 2][0], dp[n - 1][1]);
  else
    ans = max({dp[n - 3][0], dp[n - 2][1], dp[n - 1][2]});

  cout << ans << endl;
}
Problem p02716 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define fi first
#define sd second
#define lson (nd << 1)
#define rson (nd + nd + 1)
#define PB push_back
#define mid (l + r >> 1)
#define MP make_pair
#define SZ(x) (int)x.size()

using namespace std;

typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;

inline LL read() {
  LL res = 0, f = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res * f;
}

const int MAXN = 200'005;

const int MOD = 1000000007;

const LL inf = (LL)1e18;

void addmod(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
int mulmod(int a, int b) { return 1ll * a * b % MOD; }

template <typename T> void chmin(T a, T b) {
  if (a > b)
    a = b;
}

template <typename T> void chmax(T a, T b) {
  if (b > a)
    a = b;
}

LL a[MAXN], n;
map<int, long long> H[MAXN];

LL dfs(int idx, int cnt) {
  if (idx >= n + 1) {
    if (cnt == n / 2)
      return 0;
    else
      return -inf;
  }

  if (H[idx].count(cnt))
    return H[idx][cnt];

  if (idx - cnt - 1 > (n + 1) / 2)
    return -inf;

  return H[idx][cnt] = max(dfs(idx + 1, cnt), dfs(idx + 2, cnt + 1) + a[idx]);
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  cout << dfs(1, 0);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define fi first
#define sd second
#define lson (nd << 1)
#define rson (nd + nd + 1)
#define PB push_back
#define mid (l + r >> 1)
#define MP make_pair
#define SZ(x) (int)x.size()

using namespace std;

typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;

inline LL read() {
  LL res = 0, f = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res * f;
}

const int MAXN = 200'005;

const int MOD = 1000000007;

const LL inf = (LL)1e18;

void addmod(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
int mulmod(int a, int b) { return 1ll * a * b % MOD; }

template <typename T> void chmin(T a, T b) {
  if (a > b)
    a = b;
}

template <typename T> void chmax(T a, T b) {
  if (b > a)
    a = b;
}

LL a[MAXN], n;
map<int, long long> H[MAXN];

LL dfs(int idx, int cnt) {
  if (idx >= n + 1) {
    if (cnt == n / 2)
      return 0;
    else
      return -inf;
  }

  if (H[idx].count(cnt))
    return H[idx][cnt];

  if ((cnt + (n - idx + 2) / 2) < n / 2)
    return -inf;

  return H[idx][cnt] = max(dfs(idx + 1, cnt), dfs(idx + 2, cnt + 1) + a[idx]);
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  cout << dfs(1, 0);

  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N, vector<ll>(3, -(1LL << 61))); // N/2, N/2+1
  dp[0][2] = 0;
  if (N % 2 == 1) {
    dp[0][0] = A[0];
    dp[1][1] = A[1];
  } else {
    dp[0][1] = A[0];
    dp[1][2] = A[1];
  }
  rep(i, 2, N) {
    rep(j, 3) dp[i][j] = dp[i - 2][j] + A[i];
    if (i >= 3) {
      rep(j, 2) dp[i][j + 1] = max(dp[i][j + 1], dp[i - 3][j] + A[i]);
    }
  }
  output(max({dp[N - 1][2], dp[N - 2][1], dp[N - 3][0]}));

  return 0;
}


Fixed submission:


#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<vector<ll>> dp(N, vector<ll>(3, -(1LL << 61))); // N/2, N/2+1
  dp[0][2] = 0;
  if (N % 2 == 1) {
    dp[0][0] = A[0];
    dp[1][1] = A[1];
  } else {
    dp[0][1] = A[0];
    dp[1][2] = A[1];
  }
  rep(i, 2, N) {
    rep(j, 3) dp[i][j] = dp[i - 2][j] + A[i];
    if (i >= 3) {
      rep(j, 2) dp[i][j + 1] = max(dp[i][j + 1], dp[i - 3][j] + A[i]);
    }
  }
  ll ans = max(dp[N - 1][2], dp[N - 2][1]);
  if (N >= 3)
    ans = max(ans, dp[N - 3][0]);
  output(ans);
  return 0;
}
Problem p02716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;
const ll INF = 1e16;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

template <typename A, size_t N, typename T>
void st_fill(A (&arr)[N], const T &val) {
  fill((T *)arr, (T *)(arr + N), val);
}
void chmax(ll &a, ll b) { a = max(a, b); }

ll dp[100010][2][3];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  st_fill(dp, -INF);

  int r = 1 + (N % 2);
  dp[0][0][r - 1] = 0;
  dp[0][1][r] = A[0];
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      chmax(dp[i][1][j], dp[i - 1][0][j] + A[i]);
      chmax(dp[i][0][j], dp[i - 1][1][j]);
      if (j > 0)
        chmax(dp[i][0][j - 1], dp[i - 1][0][j]);
    }
  }

  ll res = -INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      res = max(res, dp[N - 1][i][j]);
    }
  }
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;
const ll INF = 1e16;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

template <typename A, size_t N, typename T>
void st_fill(A (&arr)[N], const T &val) {
  fill((T *)arr, (T *)(arr + N), val);
}
void chmax(ll &a, ll b) { a = max(a, b); }

ll dp[200010][2][3];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  st_fill(dp, -INF);

  int r = 1 + (N % 2);
  dp[0][0][r - 1] = 0;
  dp[0][1][r] = A[0];
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      chmax(dp[i][1][j], dp[i - 1][0][j] + A[i]);
      chmax(dp[i][0][j], dp[i - 1][1][j]);
      if (j > 0)
        chmax(dp[i][0][j - 1], dp[i - 1][0][j]);
    }
  }

  ll res = -INF;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      res = max(res, dp[N - 1][i][j]);
    }
  }
  cout << res << endl;

  return 0;
}