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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int n, k, v[50], dp[50][50][100];

int g(int p, int q, int r) {
  vector<int> x;
  for (int i = 0; i < p; ++i)
    x.push_back(v[i]);
  for (int i = n - 1; i > q; --i)
    x.push_back(v[i]);
  sort(all(x));
  int sum = accumulate(all(x), 0);
  for (int i = 0; i < r && i < x.size() && x[i] < 0; ++i)
    sum -= x[i];
  return sum;
}

int f(int p, int q, int r) {
  if (r == k)
    return g(p, q, 0);
  if (dp[p][q][r] != -1)
    return dp[p][q][r];
  int sol = g(p, q, k - r);
  if (p <= q) {
    sol = max(sol, f(p + 1, q, r + 1));
    sol = max(sol, f(p, q - 1, r + 1));
  }
  return dp[p][q][r] = sol;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  memset(dp, -1, sizeof dp);
  cout << f(0, n - 1, 0);
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int n, k, v[50], dp[50][50][100];

int g(int p, int q, int r) {
  vector<int> x;
  for (int i = 0; i < p; ++i)
    x.push_back(v[i]);
  for (int i = n - 1; i > q; --i)
    x.push_back(v[i]);
  sort(all(x));
  int sum = accumulate(all(x), 0);
  for (int i = 0; i < r && i < x.size() && x[i] < 0; ++i)
    sum -= x[i];
  return sum;
}

int f(int p, int q, int r) {
  if (r == k || p > q)
    return g(p, q, k - r);
  if (dp[p][q][r] != -1)
    return dp[p][q][r];
  int sol = g(p, q, k - r);
  if (p <= q) {
    sol = max(sol, f(p + 1, q, r + 1));
    sol = max(sol, f(p, q - 1, r + 1));
  }
  return dp[p][q][r] = sol;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  memset(dp, -1, sizeof dp);
  cout << f(0, n - 1, 0);
}
Problem p03032 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

bool done[123][55][55];
ll memo[123][55][55], v[55];

ll solve(int t, int l, int r) {
  if (done[t][l][r]) {
    return memo[t][l][r];
  }
  done[t][l][r] = 0ll;
  if (!t) {
    return memo[t][l][r];
  }
  if (l > r) {
    return memo[t][l][r];
  }
  memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l + 1, r) + v[l]);
  memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l, r - 1) + v[r]);
  if (t > 1) {
    memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l + 1, r));
    memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l, r - 1));
  }
  return memo[t][l][r];
}

int main(void) {
  int n, k, i;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    cin >> v[i];
  }
  cout << solve(k, 0, n - 1) << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

bool done[123][55][55];
ll memo[123][55][55], v[55];

ll solve(int t, int l, int r) {
  if (done[t][l][r]) {
    return memo[t][l][r];
  }
  done[t][l][r] = true;
  if (!t) {
    return memo[t][l][r];
  }
  if (l > r) {
    return memo[t][l][r];
  }
  memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l + 1, r) + v[l]);
  memo[t][l][r] = max(memo[t][l][r], solve(t - 1, l, r - 1) + v[r]);
  if (t > 1) {
    memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l + 1, r));
    memo[t][l][r] = max(memo[t][l][r], solve(t - 2, l, r - 1));
  }
  return memo[t][l][r];
}

int main(void) {
  int n, k, i;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    cin >> v[i];
  }
  cout << solve(k, 0, n - 1) << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

int main(void) {
  int n, k, i, j, s, t, v[50];

  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> v[i];
  }
  int r = min(n, k);

  int suma[51], sumb[51], sum, max = 0;
  suma[0] = sumb[0] = 0;
  for (i = 0; i < n; i++) {
    suma[i + 1] = suma[i] + v[i];
    sumb[i + 1] = sumb[i] + v[n - 1 - i];
  }

  int m[51], mn;
  for (i = 0; i <= r; i++) {
    for (j = 0; j <= r; j++) {
      t = k - (i + j);
      if (t < 0) {
        break;
      }
      mn = 0;
      sum = suma[i] + sumb[j];

      for (s = 0; s < i; s++) {
        if (v[s] < 0) {
          m[mn] = v[s];
          mn++;
        }
      }
      for (s = 0; s < j; s++) {
        if (v[n - 1 - s] < 0) {
          m[mn] = v[n - 1 - s];
          mn++;
        }
      }
      sort(m, m + mn);
      for (s = 0; s < t && s < mn; s++) {
        sum -= m[s];
      }

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

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

int main(void) {
  int n, k, i, j, s, t, v[50];

  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> v[i];
  }
  int r = min(n, k);

  int suma[51], sumb[51], sum, max = 0;
  suma[0] = sumb[0] = 0;
  for (i = 0; i < n; i++) {
    suma[i + 1] = suma[i] + v[i];
    sumb[i + 1] = sumb[i] + v[n - 1 - i];
  }

  int m[51], mn;
  for (i = 0; i <= r; i++) {
    for (j = 0; j <= r; j++) {
      t = k - (i + j);
      if (t < 0 || (i + j) > n) {
        break;
      }
      mn = 0;
      sum = suma[i] + sumb[j];

      for (s = 0; s < i; s++) {
        if (v[s] < 0) {
          m[mn] = v[s];
          mn++;
        }
      }
      for (s = 0; s < j; s++) {
        if (v[n - 1 - s] < 0) {
          m[mn] = v[n - 1 - s];
          mn++;
        }
      }
      sort(m, m + mn);
      for (s = 0; s < t && s < mn; s++) {
        sum -= m[s];
      }

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

  cout << max << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using Lf = long double;
using plong = pair<ll, ll>;
const int mod = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  deque<ll> _V;
  for (ll i = 0; i < N; i++) {
    ll v;
    cin >> v;
    _V.push_front(v);
  }
  const ll INF = 99999999999;
  ll res = -INF;
  for (ll k = 0; k <= K; k++) {
    for (ll a = 0; a <= k; a++) {
      for (ll b = 0; a + b <= k; b++) {
        ll tmp = 0;
        vector<ll> have;
        deque<ll> V = _V;
        for (ll i = 0; i < a; i++) {
          have.push_back(V.front());
          V.pop_front();
        }
        for (ll i = 0; i < b; i++) {
          have.push_back(V.back());
          V.pop_back();
        }
        sort(have.begin(), have.end());
        ll ind = 0;
        if (have.size() != 0) {
          for (ll i = 0; i < K - k; i++) {
            if (i == have.size() - 1)
              break;
            if (have[i] < 0) {
              ind++;
            }
          }
        }
        for (ll i = ind; i < have.size(); i++) {
          tmp += have[i];
        }
        // cout<<"a:"<<a<<", b:"<<b<<":"<<tmp<<endl;
        res = max(res, tmp);
      }
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using Lf = long double;
using plong = pair<ll, ll>;
const int mod = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  deque<ll> _V;
  for (ll i = 0; i < N; i++) {
    ll v;
    cin >> v;
    _V.push_front(v);
  }
  const ll INF = 99999999999;
  ll res = -INF;
  for (ll k = 0; k <= K; k++) {
    for (ll a = 0; a <= min(N, k); a++) {
      for (ll b = 0; a + b <= min(N, k); b++) {
        ll tmp = 0;
        vector<ll> have;
        deque<ll> V = _V;
        for (ll i = 0; i < a; i++) {
          have.push_back(V.front());
          V.pop_front();
        }
        for (ll i = 0; i < b; i++) {
          have.push_back(V.back());
          V.pop_back();
        }
        sort(have.begin(), have.end());
        ll ind = 0;
        if (have.size() != 0) {
          for (ll i = 0; i < K - k; i++) {
            if (i == have.size() - 1)
              break;
            if (have[i] < 0) {
              ind++;
            }
          }
        }
        for (ll i = ind; i < have.size(); i++) {
          tmp += have[i];
        }
        // cout<<"a:"<<a<<", b:"<<b<<":"<<tmp<<endl;
        res = max(res, tmp);
      }
    }
  }
  cout << res << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000000
#define MAX 200001
#define MOD 1000000007

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};

int main() {
  int N, K;
  ll res = 0;
  cin >> N >> K;
  deque<int> dq;
  for (int i = 0; i < N; i++) {
    int v;
    cin >> v;
    dq.push_back(v);
  }
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j <= K; j++) {
      if (i + j > K)
        continue;
      auto dq_copy = dq;
      int re = K - i - j;
      vector<int> hou;
      int idx = 0;
      while (i != idx) {
        hou.push_back(dq_copy.front());
        dq_copy.pop_front();
        idx++;
      }
      idx = 0;
      while (j != idx) {
        hou.push_back(dq_copy.back());
        dq_copy.pop_back();
        idx++;
      }
      ll ans = 0;
      sort(hou.begin(), hou.end());
      int s = hou.size();
      for (int k = 0; k < s; k++) {
        if (hou[k] >= 0 || re == 0)
          ans += hou[k];
        else
          re--;
      }
      res = max(res, ans);
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000000
#define MAX 200001
#define MOD 1000000007

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};

int main() {
  int N, K;
  ll res = 0;
  cin >> N >> K;
  deque<int> dq;
  for (int i = 0; i < N; i++) {
    int v;
    cin >> v;
    dq.push_back(v);
  }
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j <= K; j++) {
      if (i + j > K || i + j > N)
        continue;
      auto dq_copy = dq;
      int re = K - i - j;
      vector<int> hou;
      int idx = 0;
      while (i != idx) {
        hou.push_back(dq_copy.front());
        dq_copy.pop_front();
        idx++;
      }
      idx = 0;
      while (j != idx) {
        hou.push_back(dq_copy.back());
        dq_copy.pop_back();
        idx++;
      }
      ll ans = 0;
      sort(hou.begin(), hou.end());
      int s = hou.size();
      for (int k = 0; k < s; k++) {
        if (hou[k] >= 0 || re == 0)
          ans += hou[k];
        else
          re--;
      }
      res = max(res, ans);
    }
  }
  cout << res << endl;
}
Problem p03032 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  int uu = 0;
  for (int i = 0; i < k + 1; i++) {
    for (int j = 0; j < k - i + 1; j++) {
      vector<int> a;
      for (int l = 0; l < i; l++) {
        a.push_back(v.at(l));
      }
      for (int l = 0; l < j; l++) {
        a.push_back(v.at(n - l - 1));
      }
      sort(a.begin(), a.end());
      int yyyy = a.size();
      int f = min(k - i - j, yyyy);
      for (int l = 0; l < f; l++) {
        if (a.at(l) < 0) {
          a.at(l) = 0;
        } else {
          break;
        }
      }
      int r = 0;
      for (int l = 0; l < a.size(); l++) {
        r += a.at(l);
      }
      uu = max(uu, r);
    }
  }
  cout << uu << endl;
}

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  int uu = 0;
  for (int i = 0; i < min(k + 1, n + 1); i++) {
    for (int j = 0; j < min(k - i + 1, n + 1 - i); j++) {
      vector<int> a;
      for (int l = 0; l < i; l++) {
        a.push_back(v.at(l));
      }
      for (int l = 0; l < j; l++) {
        a.push_back(v.at(n - l - 1));
      }
      sort(a.begin(), a.end());
      int yyyy = a.size();
      int f = min(k - i - j, yyyy);
      for (int l = 0; l < f; l++) {
        if (a.at(l) < 0) {
          a.at(l) = 0;
        } else {
          break;
        }
      }
      int r = 0;
      for (int l = 0; l < a.size(); l++) {
        r += a.at(l);
      }
      uu = max(uu, r);
    }
  }
  cout << uu << endl;
}
Problem p03032 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  int v[55], sort_lr[55];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int ans = 0;
  for (int left = 0; left <= k; left++) {
    for (int right = 0; right <= k - left; right++) {
      for (int i = 0; i <= left - 1; i++) {
        sort_lr[i] = v[i];
      }
      for (int i = 0; i <= right - 1; i++) {
        sort_lr[i + left] = v[n - 1 - i];
      }
      sort(sort_lr, sort_lr + (left + right));
      int have = 0;
      for (int i = 0; i < min(left + right, k - (left + right)); i++) {
        if (sort_lr[i] > 0)
          have += sort_lr[i];
      }
      for (int i = k - (left + right); i <= left + right - 1; i++) {
        have += sort_lr[i];
      }
      ans = max(ans, have);
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  int v[55], sort_lr[55];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int ans = 0;
  for (int left = 0; left <= k; left++) {
    for (int right = 0; right <= min(n - left, k - left); right++) {
      for (int i = 0; i <= left - 1; i++) {
        sort_lr[i] = v[i];
      }
      for (int i = 0; i <= right - 1; i++) {
        sort_lr[i + left] = v[n - 1 - i];
      }
      sort(sort_lr, sort_lr + (left + right));
      int have = 0;
      for (int i = 0; i < min(left + right, k - (left + right)); i++) {
        if (sort_lr[i] > 0)
          have += sort_lr[i];
      }
      for (int i = k - (left + right); i <= left + right - 1; i++) {
        have += sort_lr[i];
      }
      ans = max(ans, have);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'

int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); }

int n, k;

ll v[55];

ll dp[55][55][105];

ll solve(int l, int r, int x) {
  if (x < 0)
    return -1e9;
  if (x == 0 || l > r)
    return 0;
  if (dp[l][r][x] != -1)
    return dp[l][r][x];
  ll op1 = solve(l + 1, r, x - 1) + v[l];
  ll op2 = solve(l, r - 1, x - 1) + v[r];
  ll op3 = solve(l + 1, r, x - 2);
  ll op4 = solve(l, r + 1, x - 2);
  ll op5 = 0;
  ll ret = max({op1, op2, op3, op4, op5});
  return dp[l][r][x] = ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  rep(i, n) cin >> v[i];
  cout << solve(0, n - 1, k) << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/


Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'

int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); }

int n, k;

ll v[55];

ll dp[55][55][105];

ll solve(int l, int r, int x) {
  if (x < 0)
    return -1e9;
  if (x == 0 || l > r)
    return 0;
  if (dp[l][r][x] != -1)
    return dp[l][r][x];
  ll op1 = solve(l + 1, r, x - 1) + v[l];
  ll op2 = solve(l, r - 1, x - 1) + v[r];
  ll op3 = solve(l + 1, r, x - 2);
  ll op4 = solve(l, r - 1, x - 2);
  ll op5 = 0;
  ll ret = max({op1, op2, op3, op4, op5});
  return dp[l][r][x] = ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  rep(i, n) cin >> v[i];
  cout << solve(0, n - 1, k) << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int n;
vector<int> shift(int x, vector<int> v) {
  vector<int> ans(n);
  ans = v;
  int tmp = ans[n - 1];
  for (int i = x; i <= n - 1; i++)
    ans[i] = v[i - 1];
  ans[x - 1] = tmp;
  return ans;
}
void print(vector<int> a) {
  rep(i, n) printf("%d ", a[i]);
  puts("");
}
int main() {
  int k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  int ans = 0;
  rep(x, k) {
    if (x != 0)
      v = shift(x, v);
    // print(v);
    for (int i = 1; i <= k && i <= n; ++i) {
      int tmp = 0;
      priority_queue<int, vector<int>, greater<int>> q;
      for (int j = i; j > 0; j--) {
        q.push(v[j - 1]);
        tmp += v[j - 1];
      }
      for (int j = k - i; j > 0; j--) {
        int bk = q.top();
        if (!q.empty() && bk < 0) {
          q.pop();
          tmp -= bk;
        } else
          break;
      }
      ans = max(ans, tmp);
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int n;
vector<int> shift(int x, vector<int> v) {
  vector<int> ans(n);
  ans = v;
  int tmp = ans[n - 1];
  for (int i = x; i <= n - 1; i++)
    ans[i] = v[i - 1];
  ans[x - 1] = tmp;
  return ans;
}
void print(vector<int> a) {
  rep(i, n) printf("%d ", a[i]);
  puts("");
}
int main() {
  int k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  int ans = 0;
  rep(x, min(k, n)) {
    if (x != 0)
      v = shift(x, v);
    // print(v);
    for (int i = 1; i <= k && i <= n; ++i) {
      int tmp = 0;
      priority_queue<int, vector<int>, greater<int>> q;
      for (int j = i; j > 0; j--) {
        q.push(v[j - 1]);
        tmp += v[j - 1];
      }
      for (int j = k - i; j > 0; j--) {
        int bk = q.top();
        if (!q.empty() && bk < 0) {
          q.pop();
          tmp -= bk;
        } else
          break;
      }
      ans = max(ans, tmp);
    }
  }
  cout << ans;
}
Problem p03032 (C++)

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

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

  vector<ll> D;

  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    D.push_back(tmp);
  }

  ll max;
  max = -INT_MAX;

  for (int a = 0; a <= k; a++) {
    for (int b = 0; b <= k - a; b++) {
      vector<ll> V;
      vector<ll> D_tmp;
      D_tmp = D;
      for (int i = 0; i < a; i++) {
        V.push_back(D_tmp.front());
        D_tmp.erase(D_tmp.begin());
      }
      for (int j = 0; j < b; j++) {
        V.push_back(D_tmp.back());
        D_tmp.pop_back();
      }
      if (V.empty() == 1) {
        continue;
      }
      sort(V.begin(), V.end());
      for (int i = 0; i < k - a - b; i++) {
        if (V.empty() == 1) {
          break;
        }
        if (V.front() < 0) {
          V.erase(V.begin());
        }
      }
      ll tmp;
      tmp = accumulate(V.begin(), V.end(), 0);
      if (max < tmp) {
        max = tmp;
      }
    }
  }

  cout << max;

  return 0;
}


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

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

  vector<ll> D;

  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> tmp;
    D.push_back(tmp);
  }

  ll max;
  max = -INT_MAX;

  int m;
  m = min(n, k);

  for (int a = 0; a <= m; a++) {
    for (int b = 0; b <= m - a; b++) {
      vector<ll> V;
      vector<ll> D_tmp;
      D_tmp = D;
      for (int i = 0; i < a; i++) {
        V.push_back(D_tmp.front());
        D_tmp.erase(D_tmp.begin());
      }
      for (int j = 0; j < b; j++) {
        V.push_back(D_tmp.back());
        D_tmp.pop_back();
      }
      if (V.empty() == 1) {
        continue;
      }
      sort(V.begin(), V.end());
      for (int i = 0; i < k - a - b; i++) {
        if (V.empty() == 1) {
          break;
        }
        if (V.front() < 0) {
          V.erase(V.begin());
        }
      }
      ll tmp;
      tmp = accumulate(V.begin(), V.end(), 0);
      if (max < tmp) {
        max = tmp;
      }
    }
  }

  cout << max;

  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
const int INF = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vec v(n);
  rep(i, n) { cin >> v[i]; }

  int m = 0;

  vec o;
  int sum_a = 0;
  rep(a, min(n, k) + 1) {
    int sum_b = 0;
    vec o1 = o;
    rep(b, min(n, k) - a + 1) {
      sort(all(o1));

      int sum_c = 0;
      rep(i, min(k - a - b, a + b) + 1) {
        m = max(m, sum_a + sum_b - sum_c);
        sum_c += o1[i];
      }
      if (n - b - 1 >= 0) {
        o1.push_back(v[n - b - 1]);
        sum_b += v[n - b - 1];
      }
    }
    if (a < n) {
      o.push_back(v[a]);
      sum_a += v[a];
    }
  }

  cout << m << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
const int INF = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vec v(n);
  rep(i, n) { cin >> v[i]; }

  int m = 0;

  vec o;
  int sum_a = 0;
  rep(a, min(n, k) + 1) {
    int sum_b = 0;
    vec o1 = o;
    rep(b, min(n, k) - a + 1) {
      sort(all(o1));

      int sum_c = 0;
      rep(i, min(k - a - b, a + b) + 1) {
        m = max(m, sum_a + sum_b - sum_c);
        if (i < a + b)
          sum_c += o1[i];
      }
      if (n - b - 1 >= 0) {
        o1.push_back(v[n - b - 1]);
        sum_b += v[n - b - 1];
      }
    }
    if (a < n) {
      o.push_back(v[a]);
      sum_a += v[a];
    }
  }

  cout << m << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <vector>

int main(void) {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> v(n);
  for (int i = 0; i < n; i++)
    std::cin >> v[i];
  std::vector<int> hand(n);
  int64_t ans = 0;
  for (int popleft = 0; popleft <= k; popleft++) {
    for (int popright = 0; popright <= k - popleft; popright++) {
      for (int i = 0; i < popleft; i++) {
        hand[i] = v[i];
      }
      for (int i = 0; i < popright; i++) {
        hand[i + popleft] = v[n - 1 - i];
      }
      std::sort(hand.begin(), hand.begin() + popleft + popright);
      for (int push = 0; push <= k - popleft - popright; push++) {
        int64_t sum = 0;
        for (int i = push; i < popleft + popright; i++) {
          sum += hand[i];
        }
        ans = std::max(sum, ans);
      }
    }
  }
  std::cout << ans << std::endl;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <vector>

int main(void) {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> v(n);
  for (int i = 0; i < n; i++)
    std::cin >> v[i];
  std::vector<int> hand(n);
  int64_t ans = 0;
  for (int popleft = 0; popleft <= k && popleft <= n; popleft++) {
    for (int popright = 0; popright <= k - popleft && popleft + popright <= n;
         popright++) {
      for (int i = 0; i < popleft; i++) {
        hand[i] = v[i];
      }
      for (int i = 0; i < popright; i++) {
        hand[i + popleft] = v[n - 1 - i];
      }
      std::sort(hand.begin(), hand.begin() + popleft + popright);
      for (int push = 0; push <= k - popleft - popright; push++) {
        int64_t sum = 0;
        for (int i = push; i < popleft + popright; i++) {
          sum += hand[i];
        }
        ans = std::max(sum, ans);
      }
    }
  }
  std::cout << ans << std::endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll> P;
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define fr(i, n) for (int i = 0; i < (n); i++)
#define Fr(i, n) for (int i = 0; i++ < (n);)
#define ifr(i, n) for (int i = (n)-1; i >= 0; i--)
#define iFr(i, n) for (int i = (n); i > 0; i--)

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  deque<ll> v(n);
  fr(i, n) cin >> v[i];
  fr(i, k + 1) {
    fr(j, i + 1) {
      ll kari = 0;
      priority_queue<ll> pq;
      fr(l, j) {
        kari += v[l];
        if (v[l] < 0)
          pq.push(-v[l]);
      }
      fr(l, i - j) {
        kari += v[n - 1 - l];
        if (v[n - 1 - l] < 0)
          pq.push(-v[n - 1 - l]);
      }
      fr(l, k - i) {
        if (pq.empty())
          break;
        kari += pq.top();
        pq.pop();
      }
      ans = max(ans, kari);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll> P;
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define fr(i, n) for (int i = 0; i < (n); i++)
#define Fr(i, n) for (int i = 0; i++ < (n);)
#define ifr(i, n) for (int i = (n)-1; i >= 0; i--)
#define iFr(i, n) for (int i = (n); i > 0; i--)

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  deque<ll> v(n);
  fr(i, n) cin >> v[i];
  fr(i, min(k + 1, n + 1)) {
    fr(j, i + 1) {
      ll kari = 0;
      priority_queue<ll> pq;
      fr(l, j) {
        kari += v[l];
        if (v[l] < 0)
          pq.push(-v[l]);
      }
      fr(l, i - j) {
        kari += v[n - 1 - l];
        if (v[n - 1 - l] < 0)
          pq.push(-v[n - 1 - l]);
      }
      fr(l, k - i) {
        if (pq.empty())
          break;
        kari += pq.top();
        pq.pop();
      }
      ans = max(ans, kari);
    }
  }
  cout << ans << endl;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n), range(k * 2, 0), vals(0);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }

  for (int i = 0; i < k; i++) {
    if (k - 1 - i < n) {
      range.at(i) = v.at(k - 1 - i);
    }
    if (n - 1 - i >= 0) {
      range.at(k + i) = v.at(n - 1 - i);
    }
  }

  for (int cd = 0; cd <= k / 2; cd++) {
    int x = min(n, k - cd);
    for (int offset = k - x; offset <= k; offset++) {
      vector<int> hand(x, 0);
      int val = 0;
      for (int pos = 0; pos < x; pos++) {
        hand.at(pos) = range.at(pos + offset);
        val += hand.at(pos);
      }
      vals.push_back(val);
      sort(hand.begin(), hand.end());
      for (int cnt = 0; cnt < cd; cnt++) {
        val -= hand.at(cnt);
        vals.push_back(val);
      }
    }
  }

  int val_max = 0;
  for (int i = 0; i < vals.size(); i++) {
    if (vals.at(i) > val_max) {
      val_max = vals.at(i);
    }
  }

  cout << val_max << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n), range(k * 2, 0), vals(0);
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }

  for (int i = 0; i < k; i++) {
    if (k - 1 - i < n) {
      range.at(i) = v.at(k - 1 - i);
    }
    if (n - 1 - i >= 0) {
      range.at(k + i) = v.at(n - 1 - i);
    }
  }

  for (int cd = 0; cd <= k / 2; cd++) {
    int x = min(n, k - cd);
    for (int offset = k - x; offset <= k; offset++) {
      vector<int> hand(x, 0);
      int val = 0;
      for (int pos = 0; pos < x; pos++) {
        hand.at(pos) = range.at(pos + offset);
        val += hand.at(pos);
      }
      vals.push_back(val);
      sort(hand.begin(), hand.end());
      int y = min(x, cd);
      for (int cnt = 0; cnt < y; cnt++) {
        val -= hand.at(cnt);
        vals.push_back(val);
      }
    }
  }

  int val_max = 0;
  for (int i = 0; i < vals.size(); i++) {
    if (vals.at(i) > val_max) {
      val_max = vals.at(i);
    }
  }

  cout << val_max << endl;
}
Problem p03032 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll N, K;
  cin >> N >> K;
  deque<ll> V(N);
  rep(i, N) cin >> V[i];
  ll ans = 0;
  rep(i, K + 1) {
    ll remain = K - i;
    rep(j, remain + 1) {
      auto X = V;
      vector<ll> S;
      rep(x, i) {
        S.push_back(X.front());
        X.pop_front();
      }
      rep(x, j) {
        S.push_back(X.back());
        X.pop_back();
      }
      ll s = accumulate(S.begin(), S.end(), 0LL);
      sort(S.begin(), S.end());
      ll r = K - i - j;
      rep(k, min(r, (ll)S.size())) {
        if (S[k] < 0)
          s -= S[k];
      }
      ans = max(ans, s);
    }
  }
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr);
  // ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  solve();
  return 0;
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll N, K;
  cin >> N >> K;
  deque<ll> V(N);
  rep(i, N) cin >> V[i];
  ll ans = 0;
  rep(i, K + 1) {
    ll remain = K - i;
    rep(j, remain + 1) {
      auto X = V;
      vector<ll> S;
      if (i + j > N)
        continue;
      rep(x, i) {
        S.push_back(X.front());
        X.pop_front();
      }
      rep(x, j) {
        S.push_back(X.back());
        X.pop_back();
      }
      ll s = accumulate(S.begin(), S.end(), 0LL);
      sort(S.begin(), S.end());
      ll r = K - i - j;
      rep(k, min(r, (ll)S.size())) {
        if (S[k] < 0)
          s -= S[k];
      }
      ans = max(ans, s);
    }
  }
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr);
  // ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
#define F first
#define S second
#define ld long double
#define rep(i, a, b) for (ll i = a; i < b; i++)

const int N = 55, M = 110;
ll n, t, a[N], dp[N][N][M];

ll recur(ll l, ll r, ll k) {
  if (k <= 0)
    return 0;
  if (l == r)
    return a[l];
  ll &ans = dp[l][r][k];
  if (ans != -1)
    return ans;
  /*ll op1 = recur(l+1, r, k-1) + a[l];
  ll op2 = recur(l, r-1, k-1) + a[r];
  ll op3 = recur(l+1, r, k-2);
  ll op4 = recur(l, r-1, k-2);
  ll op5 = 0;
  ans = max(op1, op2);
  ans = max(ans, op3);
  ans = max(ans, op4);
  ans = max(ans, op5);*/
  ans = max(ans, a[l] + recur(l + 1, r, k - 1));
  ans = max(ans, a[r] + recur(l, r - 1, k - 1));
  ans = max(ans, recur(l + 1, r, k - 2));
  ans = max(ans, recur(l, r - 1, k - 2));

  // return ans;
  return ans;
}

void solve() {
  cin >> n >> t;
  memset(dp, -1, sizeof dp);
  rep(i, 1, n + 1) cin >> a[i];
  ll ans = recur(1, n, t);
  cout << ans << endl;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
#define F first
#define S second
#define ld long double
#define rep(i, a, b) for (ll i = a; i < b; i++)

const int N = 55, M = 110;
ll n, t, a[N], dp[N][N][M];

ll recur(ll l, ll r, ll k) {
  if (k <= 0)
    return 0;
  if (l == r)
    return a[l];
  ll &ans = dp[l][r][k];
  if (ans != -1)
    return ans;
  /*ll op1 = recur(l+1, r, k-1) + a[l];
  ll op2 = recur(l, r-1, k-1) + a[r];
  ll op3 = recur(l+1, r, k-2);
  ll op4 = recur(l, r-1, k-2);
  ll op5 = 0;
  ans = max(op1, op2);
  ans = max(ans, op3);
  ans = max(ans, op4);
  ans = max(ans, op5);*/
  ans = max(ans, a[l] + recur(l + 1, r, k - 1));
  ans = max(ans, a[r] + recur(l, r - 1, k - 1));
  ans = max(ans, recur(l + 1, r, k - 2));
  ans = max(ans, recur(l, r - 1, k - 2));
  ans = max(ans, 0LL);
  // return ans;
  return ans;
}

void solve() {
  cin >> n >> t;
  memset(dp, -1, sizeof dp);
  rep(i, 1, n + 1) cin >> a[i];
  ll ans = recur(1, n, t);
  cout << ans << endl;
}

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

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

typedef long long ll;
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> q(n);
  for (int i = 0; i < n; i++)
    cin >> q[i];
  int ans = 0;
  for (int l = 0; l <= k; l++) {
    for (int r = 0; r <= k - l; r--) {
      if (l + r > n)
        continue;
      int d = k - l - r;
      int cur = 0;
      vector<int> s;
      for (int i = 0; i < l; i++) {
        cur += q[i];
        s.push_back(q[i]);
      }
      for (int i = n - r; i < n; i++) {
        cur += q[i];
        s.push_back(q[i]);
      }
      sort(s.begin(), s.end());
      for (int i = 0; i < d; i++) {
        if (i >= s.size())
          break;
        if (s[i] > 0)
          break;
        cur -= s[i];
      }
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> q(n);
  for (int i = 0; i < n; i++)
    cin >> q[i];
  int ans = 0;
  for (int l = 0; l <= k; l++) {
    for (int r = 0; r <= k - l; r++) {
      if (l + r > n)
        continue;
      int d = k - l - r;
      int cur = 0;
      vector<int> s;
      for (int i = 0; i < l; i++) {
        cur += q[i];
        s.push_back(q[i]);
      }
      for (int i = n - r; i < n; i++) {
        cur += q[i];
        s.push_back(q[i]);
      }
      sort(s.begin(), s.end());
      for (int i = 0; i < d; i++) {
        if (i >= s.size())
          break;
        if (s[i] > 0)
          break;
        cur -= s[i];
      }
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>   //abs()かfabs()で少数絶対値
#include <cstdlib> //abs()で整数絶対値
#include <deque>
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> V(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i];
  }
  int max = 0;
  int sum = 0;
  vector<int> minus;
  for (int i = 0; i <= min(N, K); i++) {
    for (int j = 0; j <= min(N - i, K - i); j++) {
      sum = 0;
      minus.clear();
      for (int k = 0; k < i; k++) {
        sum += V[k];
        if (V[k] < 0) {
          minus.push_back(V[k]);
        }
      }
      for (int k = 0; k < j; k++) {
        sum += V[(N - 1) - k];
        if (V[(N - 1) - k] < 0) {
          minus.push_back(V[(N - 1) - k]);
        }
      }
      sort(minus.begin(), minus.end(), greater<int>());
      if (minus.size() > 0) {
        for (int k = 0; k < K - (i + j) || minus.size() != 0; k++) {
          sum -= minus.back();
          minus.pop_back();
        }
      }
      if (max < sum)
        max = sum;
    }
  }
  cout << max << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>   //abs()かfabs()で少数絶対値
#include <cstdlib> //abs()で整数絶対値
#include <deque>
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> V(N);
  for (int i = 0; i < N; i++) {
    cin >> V[i];
  }
  int max = 0;
  int sum = 0;
  vector<int> minus;
  for (int i = 0; i <= min(N, K); i++) {
    for (int j = 0; j <= min(N - i, K - i); j++) {
      sum = 0;
      minus.clear();
      for (int k = 0; k < i; k++) {
        sum += V[k];
        if (V[k] < 0) {
          minus.push_back(V[k]);
        }
      }
      for (int k = 0; k < j; k++) {
        sum += V[(N - 1) - k];
        if (V[(N - 1) - k] < 0) {
          minus.push_back(V[(N - 1) - k]);
        }
      }
      sort(minus.begin(), minus.end(), greater<int>());
      if (minus.size() > 0) {
        for (int k = 0; k < K - (i + j) && minus.size() != 0; k++) {
          sum -= minus.back();
          minus.pop_back();
        }
      }
      if (max < sum)
        max = sum;
    }
  }
  cout << max << endl;
}
Problem p03032 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> V(N);
  for (int i = 0; i < N; i++)
    cin >> V.at(i);

  int ans = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; i + j <= K; j++) {
      int L = min(i, N);
      int R = min(j, N - i);
      vector<int> tmp;
      while (L)
        tmp.push_back(V.at(--L));
      while (R)
        tmp.push_back(V.at(N - 1 - --R));
      int sum = accumulate(tmp.begin(), tmp.end(), 0);
      ans = max(ans, sum);
      sort(tmp.begin(), tmp.end());
      for (int k = 0; k < min(K - i - j, (int)tmp.size()); k++) {
        sum -= tmp.at(k);
        ans = max(ans, sum);
      }
    }
  }

  cout << ans << "\n";
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> V(N);
  for (int i = 0; i < N; i++)
    cin >> V.at(i);

  int ans = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; i + j <= K; j++) {
      int L = min(i, N);
      int R = min(j, max(0, N - i));
      vector<int> tmp;
      while (L)
        tmp.push_back(V.at(--L));
      while (R)
        tmp.push_back(V.at(N - 1 - --R));
      int sum = accumulate(tmp.begin(), tmp.end(), 0);
      ans = max(ans, sum);
      sort(tmp.begin(), tmp.end());
      for (int k = 0; k < min(K - i - j, (int)tmp.size()); k++) {
        sum -= tmp.at(k);
        ans = max(ans, sum);
      }
    }
  }

  cout << ans << "\n";
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
#include "string"

int n, k;
int dp[55][55][101], a[55];

int solve(int l, int r, int cnt) {
  if (cnt > k)
    return -inf;
  if (cnt == k or l > r)
    return 0;
  int &tmp = dp[l][r][cnt];
  if (tmp != -1)
    return tmp;
  tmp = 0;
  tmp = max(tmp, solve(l + 1, r, cnt + 1) + a[l]);
  tmp = max(tmp, solve(l, r - 1, cnt + 1) + a[r]);
  if (a[l] < 0)
    tmp = max(tmp, solve(l + 1, r, cnt + 2));
  if (a[r] < 0)
    tmp = max(tmp, solve(l, r - 1, cnt + 2));
  return tmp;
}

int32_t main() {
  int t = 1;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    memset(dp, -1, sizeof dp);
    int ans = solve(0, n - 1, 0);
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
#include "string"

int n, k;
int dp[55][55][101], a[55];

int solve(int l, int r, int cnt) {
  if (cnt > k)
    return -inf;
  if (cnt == k or l > r)
    return 0;
  int &tmp = dp[l][r][cnt];
  if (tmp != -1)
    return tmp;
  tmp = 0;
  tmp = max(tmp, solve(l + 1, r, cnt + 1) + a[l]);
  tmp = max(tmp, solve(l, r - 1, cnt + 1) + a[r]);
  if (a[l] < 0)
    tmp = max(tmp, solve(l + 1, r, cnt + 2));
  if (a[r] < 0)
    tmp = max(tmp, solve(l, r - 1, cnt + 2));
  return tmp;
}

int32_t main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    memset(dp, -1, sizeof dp);
    int ans = solve(0, n - 1, 0);
    cout << ans << endl;
  }
}
Problem p03032 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 50 + 5;
const int M = 100 + 5;
const ll OO = 1e18;
const int mod = 1e9 + 7;

int n, k, v[N], dp[N][N][M];

int solve(int s, int e, int g) {

  if (s > e)
    return 0;

  int &ret = dp[s][e][g];

  ret = 0;

  if (g >= 1) {

    ret = max(
        {ret, solve(s, e - 1, g - 1) + v[e], solve(s + 1, e, g - 1) + v[s]});
  }

  if (g > 1) {

    ret = max({ret, solve(s, e - 1, g - 2), solve(s + 1, e, g - 2)});
  }

  return ret;
}

void init() {}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;

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

  memset(dp, -1, sizeof dp);

  cout << solve(0, n - 1, k) << '\n';

  return 0;
}

Fixed submission:
// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 50 + 5;
const int M = 100 + 5;
const ll OO = 1e18;
const int mod = 1e9 + 7;

int n, k, v[N], dp[N][N][M];

int solve(int s, int e, int g) {

  if (s > e)
    return 0;

  int &ret = dp[s][e][g];

  if (~ret)
    return ret;

  ret = 0;

  if (g >= 1) {

    ret = max(
        {ret, solve(s, e - 1, g - 1) + v[e], solve(s + 1, e, g - 1) + v[s]});
  }

  if (g > 1) {

    ret = max({ret, solve(s, e - 1, g - 2), solve(s + 1, e, g - 2)});
  }

  return ret;
}

void init() {}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;

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

  memset(dp, -1, sizeof dp);

  cout << solve(0, n - 1, k) << '\n';

  return 0;
}
Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;
typedef long long ll;

int main() {

  ll N, K;
  cin >> N >> K;

  vector<ll> V(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> V[i];
  }

  ll ans = 0;
  for (ll l = 0; l <= min(K, N); l++) {
    for (ll r = 0; l + r <= min(K, N); r++) {
      // ぷらいおりてぃきゅー
      // 昇順
      priority_queue<ll, vector<ll>, greater<ll>> que;
      ll sum = 0;
      for (ll i = 0; i < l; i++) {
        que.push(V[i]);
        sum += V[i];
      }
      for (ll i = 0; i < r; i++) {
        que.push(V[N - i - 1]);
        sum += V[N - i - 1];
      }
      // マイナスの価値の宝石を消す
      for (ll i = 0; i <= min(K - l - r, (ll)que.size()); i++) {
        if (que.top() < 0) {
          sum -= que.top();
          que.pop();
        }
      }
      ans = max(ans, sum);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;
typedef long long ll;

int main() {

  ll N, K;
  cin >> N >> K;

  vector<ll> V(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> V[i];
  }

  ll ans = 0;
  for (ll l = 0; l <= min(K, N); l++) {
    for (ll r = 0; l + r <= min(K, N); r++) {
      // ぷらいおりてぃきゅー
      // 昇順
      priority_queue<ll, vector<ll>, greater<ll>> que;
      ll sum = 0;
      for (ll i = 0; i < l; i++) {
        que.push(V[i]);
        sum += V[i];
      }
      for (ll i = 0; i < r; i++) {
        que.push(V[N - i - 1]);
        sum += V[N - i - 1];
      }
      // マイナスの価値の宝石を消す
      for (ll i = 0; i < K - l - r; i++) {
        if (que.empty())
          break;
        if (que.top() > 0)
          break;
        if (que.top() < 0) {
          sum -= que.top();
          que.pop();
        }
      }
      ans = max(ans, sum);
    }
  }

  cout << ans << endl;
}