Problem p03032 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define reverse(s) reverse(s.begin(), s.end());
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

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

  int ans = 0;
  // i 取り出す回数 k-i回戻す
  rep(i, k + 1) {
    if (n < i) {
      i = n;
    }
    rep(j, i + 1) {

      int score = 0;
      vector<int> boo;

      rep(k, j) {
        score += v[k];
        if (v[k] < 0) {
          boo.push_back(v[k]);
        }
      }
      rep(k, i - j) {
        score += v[n - 1 - k];
        if (v[n - 1 - k] < 0) {
          boo.push_back(v[n - 1 - k]);
        }
      }

      int back = k - i;
      SORT(boo);
      rep(z, back) {
        if (z == boo.size()) {
          break;
        }
        score -= boo[z];
      }
      ans = max(ans, score);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define reverse(s) reverse(s.begin(), s.end());
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

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

  int ans = 0;
  // i 取り出す回数 k-i回戻す
  rep(i0, k + 1) {
    int i = i0;
    if (n < i) {
      i = n;
    }
    rep(j, i + 1) {

      int score = 0;
      vector<int> boo;

      rep(k, j) {
        score += v[k];
        if (v[k] < 0) {
          boo.push_back(v[k]);
        }
      }
      rep(k, i - j) {
        score += v[n - 1 - k];
        if (v[n - 1 - k] < 0) {
          boo.push_back(v[n - 1 - k]);
        }
      }

      int back = k - i;
      SORT(boo);
      rep(z, back) {
        if (z == boo.size()) {
          break;
        }
        score -= boo[z];
      }
      ans = max(ans, score);
    }
  }
  cout << ans << endl;
}