Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#pragma region include
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define MOD 1000000007
#define INF 1000000000
#define LLINF 4000000000000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#pragma endregion

// #define __DEBUG__
#ifdef __DEBUG__
#define dump(x)                                                                \
  cerr << #x << " = " << (x) << " [" << __LINE__ << ":" << __FUNCTION__        \
       << "] " << endl;
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
  os << "}";
  return os;
}
// pair出力
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
// map出力
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << "(" << itr->first << ", " << itr->second << ")";
    itr++;
    if (itr != map_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set 出力
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  VI v1(N), v2(N);
  vector<ll> sum(N + 1);
  REP(i, N) {
    cin >> v1[i];
    v2[N - 1 - i] = v1[i];
  }
  sum[0] = 0;
  REP(i, N) { sum[i + 1] = sum[i] + v1[i]; }
  ll val = 0;
  ll ans = -LLINF;
  for (int a = 0; a <= N; a++) {
    for (int b = 0; b <= K - a; b++) {
      val = sum[a] + sum[N] - sum[N - b];
      vector<int> vec(N, INF);
      REP(i, a) { vec[i] = v1[i]; }
      for (int i = 0; i < b; i++) {
        vec[i + a] = v2[i];
      }
      sort(ALL(vec));
      REP(i, K - a - b) {
        if (vec[i] < 0) {
          val -= vec[i];
        } else {
          break;
        }
      }
      ans = max(ans, val);
    }
  }
  cout << ans << endl;
  getchar();
  getchar();
}

Fixed submission:
#pragma region include
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define MOD 1000000007
#define INF 1000000000
#define LLINF 4000000000000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#pragma endregion

// #define __DEBUG__
#ifdef __DEBUG__
#define dump(x)                                                                \
  cerr << #x << " = " << (x) << " [" << __LINE__ << ":" << __FUNCTION__        \
       << "] " << endl;
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
  os << "}";
  return os;
}
// pair出力
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
// map出力
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << "(" << itr->first << ", " << itr->second << ")";
    itr++;
    if (itr != map_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set 出力
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  VI v1(N), v2(N);
  vector<ll> sum(N + 1);
  REP(i, N) {
    cin >> v1[i];
    v2[N - 1 - i] = v1[i];
  }
  sum[0] = 0;
  REP(i, N) { sum[i + 1] = sum[i] + v1[i]; }
  ll val = 0;
  ll ans = 0;
  int aaa = max(N, K);
  int bbb = min(N, K);
  for (int a = 0; a <= bbb; a++) {
    for (int b = 0; b <= bbb - a; b++) {
      val = sum[a] - sum[N - b] + sum[N];
      vector<int> vec(aaa, INF);
      REP(i, a) { vec[i] = v1[i]; }
      for (int i = 0; i < b; i++) {
        vec[i + a] = v2[i];
      }
      sort(ALL(vec));
      REP(i, K - a - b) {
        if (vec[i] < 0) {
          val -= vec[i];
        } else {
          break;
        }
      }
      ans = max(ans, val);
    }
  }
  cout << ans << endl;
  getchar();
  getchar();
}