Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = pow(10, 9) + 7;
// const int mod = 998244353;
// const int mod = ;
const int INF = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }

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

  int ans = -INF;
  for (int a = 0; a <= K; a++) {
    for (int b = 0; b <= K; b++) {
      if (a + b > K)
        break;
      int aa = min(a, N);
      int bb = min(b, N - aa);
      int cd = min(K / 2, K - a - b);

      vector<int> hv;
      for (int i = 0; i < aa; i++) {
        hv.push_back(V.at(i));
      }
      for (int i = 0; i < bb; i++) {
        hv.push_back(V.at(N - i - 1));
      }

      sort(hv.begin(), hv.end(), greater<int>());
      // cout << hv.size() << " " << a << " " << b << " " << cd << endl;
      // cout << cd << " " << hv.size() << " ";
      cd = min((int)cd, (int)hv.size());

      int val = 0;
      for (int i = 0; i < hv.size() - cd; i++) {
        val += hv.at(i);
      }
      chmax(ans, val);

      cd--;
      // cout << cd << endl;

      val = 0;
      for (int i = 0; i < hv.size() - cd; i++) {
        val += hv.at(i);
      }
      chmax(ans, val);
      // cout << a << " " << b << " " << cd << " " << val << endl;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = pow(10, 9) + 7;
// const int mod = 998244353;
// const int mod = ;
const int INF = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }

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

  int ans = -INF;
  for (int a = 0; a <= K; a++) {
    for (int b = 0; b <= K; b++) {
      if (a + b > K)
        break;
      int aa = min(a, N);
      int bb = min(b, N - aa);
      int cd = min(K / 2, K - a - b);

      vector<int> hv;
      for (int i = 0; i < aa; i++) {
        hv.push_back(V.at(i));
      }
      for (int i = 0; i < bb; i++) {
        hv.push_back(V.at(N - i - 1));
      }

      sort(hv.begin(), hv.end(), greater<int>());
      // cout << hv.size() << " " << a << " " << b << " " << cd << endl;
      // cout << cd << " " << hv.size() << " ";
      cd = min((int)cd, (int)hv.size());

      int val = 0;
      for (int i = 0; i < hv.size() - cd; i++) {
        val += hv.at(i);
      }
      chmax(ans, val);

      cd = max((int)0, cd - 1);
      // cout << cd << endl;

      val = 0;
      for (int i = 0; i < hv.size() - cd; i++) {
        val += hv.at(i);
      }
      chmax(ans, val);
      // cout << a << " " << b << " " << cd << " " << val << endl;
    }
  }

  cout << ans << endl;
}