Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define LL long long
#define MOD (1000000007)

// 129
/*
int main() {
        LL H, W;

        scanf("%lld %lld", &H, &W);

        static int hor_label[2020][2020];
        static int var_label[2020][2020];

        static int hor_label_to_count[2020 * 2020];
        static int var_label_to_count[2020 * 2020];

        memset(hor_label, 0, sizeof(hor_label));
        memset(var_label, 0, sizeof(var_label));
        memset(hor_label_to_count, 0, sizeof(hor_label_to_count));
        memset(var_label_to_count, 0, sizeof(var_label_to_count));

        char s[2020];
        int hor_curr_label = 1;
        int var_curr_label = 1;
        for (int j = 0; j < H; j++) {
                scanf("%s", &s);

                for (int i = 0; i < W; i++) {
                        if (s[i] == '.') {
                                hor_label[j][i] = hor_curr_label;
                                hor_label_to_count[hor_curr_label]++;

                                if (j == 0) {
                                        var_label[j][i] = var_curr_label;
                                        var_label_to_count[var_curr_label]++;
                                        var_curr_label++;
                                }
                                else {
                                        int abobe_label = var_label[j - 1][i];
                                        if (abobe_label > 0) {
                                                var_label[j][i] = abobe_label;
                                                var_label_to_count[abobe_label]++;
                                        }
                                        else {
                                                var_label[j][i] =
var_curr_label; var_label_to_count[var_curr_label]++; var_curr_label++;
                                        }
                                }
                        }
                        else {
                                hor_curr_label++;
                        }
                }

                hor_curr_label++;
        }

        LL result = 0;

        for (int j = 0; j < H; j++) {
                for (int i = 0; i < W; i++) {
                        if (hor_label[j][i] == 0) {
                                continue;
                        }

                        LL count = hor_label_to_count[hor_label[j][i]] +
var_label_to_count[var_label[j][i]] - 1;

                        result = max(count, result);
                }
        }

        printf("%lld\n", result);

        return 0;
}
*/

// 128
int main() {
  LL N, K;
  scanf("%lld %lld", &N, &K);

  vector<LL> V(N);
  vector<LL> Lval(N + 1);
  Lval[0] = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    Lval[i + 1] = Lval[i] + V[i];
  }

  vector<LL> Rval(N + 1);
  Rval[0] = 0;
  for (int i = 0; i < N; i++) {
    Rval[i + 1] = Rval[i] + V[N - i - 1];
  }

  vector<vector<vector<LL>>> Dq(N + 1,
                                vector<vector<LL>>(N + 1, vector<LL>(0)));

  for (int i = 0; i <= N; i++) {       // L
    for (int j = 0; j <= N - i; j++) { // R
      for (int l = 0; l < i; l++) {
        if (V[l] < 0) {
          Dq[i][j].push_back(V[l]);
        }
      }

      for (int r = 0; r < j; r++) {
        if (V[N - r - 1] < 0) {
          Dq[i][j].push_back(V[N - r - 1]);
        }
      }

      if (Dq[i][j].size() > 0) {
        std::sort(Dq[i][j].begin(), Dq[i][j].end());
      }
    }
  }

  LL result = 0;

  for (int l = 0; l <= K; l++) {
    for (int r = 0; r <= K - l; r++) {
      for (int d = 0; d <= K - l - r; d++) {
        LL value = Lval[l] + Rval[r];

        int dis = min(d, (int)Dq[l][r].size());
        for (int i = 0; i < dis; i++) {
          value -= Dq[l][r][i];
        }

        result = max(result, value);
      }
    }
  }

  printf("%lld\n", result);

  return 0;
}

// 127
/*
struct IntPair {
        LL B, C;
};

int main() {
        LL N, M;

        scanf("%lld %lld", &N, &M);

        vector<LL> A;

        for (int i = 0; i < N; i++) {
                LL val;
                scanf("%lld", &val);
                A.push_back(val);
        }

        vector<IntPair> P;

        for (int i = 0; i < M; i++) {
                LL val1, val2;
                scanf("%lld %lld", &val1, &val2);
                IntPair pair;
                pair.B = val1;
                pair.C = val2;
                P.push_back(pair);
        }

        std::sort(A.begin(), A.end());

        std::sort(P.begin(), P.end(), [](const IntPair &a, const IntPair &b) {
                return a.C > b.C;
        });

        LL indexA = 0;
        bool finish = false;
        for (auto item : P) {
                for (int i = 0; i < item.B; i++) {
                        if (item.C > A[indexA]) {
                                A[indexA] = item.C;
                        }
                        else {
                                finish = true;
                                break;
                        }

                        indexA++;

                        if (indexA >= N) {
                                finish = true;
                                break;
                        }
                }

                if (finish) {
                        break;
                }
        }

        LL result = 0;
        for (auto item : A) {
                result += item;
        }

        printf("%lld\n", result);

        return 0;
}
*/

// 126
/*
struct Node {
        unsigned int connect;
        unsigned int weight;
};

int main() {
        LL N;

        scanf("%lld", &N);

        static vector<Node> node[100010];
        static char color[100010];

        for (int i = 0; i < N - 1; i++) {
                unsigned int u, v, w;

                scanf("%d %d %d", &u, &v, &w);

                Node n1;
                n1.connect = v;
                n1.weight = w;
                node[u].push_back(n1);

                Node n2;
                n2.connect = u;
                n2.weight = w;
                node[v].push_back(n2);
        }

        for (int i = 0; i <= N; i++) {
                color[i] = -1;
        }

        std::queue<unsigned int> q;

        color[1] = 0;
        q.push(1);

        //���D��T��
        while (q.size() > 0) {
                unsigned int index = q.front();
                q.pop();

                char c = color[index];

                for (auto item : node[index]) {
                        if (color[item.connect] < 0) {
                                //�܂��ʂ��Ă��Ȃ��m�[�h
                                color[item.connect] = item.weight % 2 == 0 ? c :
1 - c; q.push(item.connect);
                        }
                }
        }

        for (int i = 1; i <= N; i++) {
                printf("%d\n", color[i]);
        }

        return 0;
}
*/

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define LL long long
#define MOD (1000000007)

// 129
/*
int main() {
        LL H, W;

        scanf("%lld %lld", &H, &W);

        static int hor_label[2020][2020];
        static int var_label[2020][2020];

        static int hor_label_to_count[2020 * 2020];
        static int var_label_to_count[2020 * 2020];

        memset(hor_label, 0, sizeof(hor_label));
        memset(var_label, 0, sizeof(var_label));
        memset(hor_label_to_count, 0, sizeof(hor_label_to_count));
        memset(var_label_to_count, 0, sizeof(var_label_to_count));

        char s[2020];
        int hor_curr_label = 1;
        int var_curr_label = 1;
        for (int j = 0; j < H; j++) {
                scanf("%s", &s);

                for (int i = 0; i < W; i++) {
                        if (s[i] == '.') {
                                hor_label[j][i] = hor_curr_label;
                                hor_label_to_count[hor_curr_label]++;

                                if (j == 0) {
                                        var_label[j][i] = var_curr_label;
                                        var_label_to_count[var_curr_label]++;
                                        var_curr_label++;
                                }
                                else {
                                        int abobe_label = var_label[j - 1][i];
                                        if (abobe_label > 0) {
                                                var_label[j][i] = abobe_label;
                                                var_label_to_count[abobe_label]++;
                                        }
                                        else {
                                                var_label[j][i] =
var_curr_label; var_label_to_count[var_curr_label]++; var_curr_label++;
                                        }
                                }
                        }
                        else {
                                hor_curr_label++;
                        }
                }

                hor_curr_label++;
        }

        LL result = 0;

        for (int j = 0; j < H; j++) {
                for (int i = 0; i < W; i++) {
                        if (hor_label[j][i] == 0) {
                                continue;
                        }

                        LL count = hor_label_to_count[hor_label[j][i]] +
var_label_to_count[var_label[j][i]] - 1;

                        result = max(count, result);
                }
        }

        printf("%lld\n", result);

        return 0;
}
*/

// 128
int main() {
  LL N, K;
  scanf("%lld %lld", &N, &K);

  vector<LL> V(N);
  vector<LL> Lval(N + 1);
  Lval[0] = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    Lval[i + 1] = Lval[i] + V[i];
  }

  vector<LL> Rval(N + 1);
  Rval[0] = 0;
  for (int i = 0; i < N; i++) {
    Rval[i + 1] = Rval[i] + V[N - i - 1];
  }

  vector<vector<vector<LL>>> Dq(N + 1,
                                vector<vector<LL>>(N + 1, vector<LL>(0)));

  for (int i = 0; i <= N; i++) {       // L
    for (int j = 0; j <= N - i; j++) { // R
      for (int l = 0; l < i; l++) {
        if (V[l] < 0) {
          Dq[i][j].push_back(V[l]);
        }
      }

      for (int r = 0; r < j; r++) {
        if (V[N - r - 1] < 0) {
          Dq[i][j].push_back(V[N - r - 1]);
        }
      }

      if (Dq[i][j].size() > 0) {
        std::sort(Dq[i][j].begin(), Dq[i][j].end());
      }
    }
  }

  LL result = 0;

  LL max_get = min(N, K);

  for (int l = 0; l <= max_get; l++) {
    for (int r = 0; r <= max_get - l; r++) {
      for (int d = 0; d <= K - l - r; d++) {
        LL value = Lval[l] + Rval[r];

        int dis = min(d, (int)Dq[l][r].size());
        for (int i = 0; i < dis; i++) {
          value -= Dq[l][r][i];
        }

        result = max(result, value);
      }
    }
  }

  printf("%lld\n", result);

  return 0;
}

// 127
/*
struct IntPair {
        LL B, C;
};

int main() {
        LL N, M;

        scanf("%lld %lld", &N, &M);

        vector<LL> A;

        for (int i = 0; i < N; i++) {
                LL val;
                scanf("%lld", &val);
                A.push_back(val);
        }

        vector<IntPair> P;

        for (int i = 0; i < M; i++) {
                LL val1, val2;
                scanf("%lld %lld", &val1, &val2);
                IntPair pair;
                pair.B = val1;
                pair.C = val2;
                P.push_back(pair);
        }

        std::sort(A.begin(), A.end());

        std::sort(P.begin(), P.end(), [](const IntPair &a, const IntPair &b) {
                return a.C > b.C;
        });

        LL indexA = 0;
        bool finish = false;
        for (auto item : P) {
                for (int i = 0; i < item.B; i++) {
                        if (item.C > A[indexA]) {
                                A[indexA] = item.C;
                        }
                        else {
                                finish = true;
                                break;
                        }

                        indexA++;

                        if (indexA >= N) {
                                finish = true;
                                break;
                        }
                }

                if (finish) {
                        break;
                }
        }

        LL result = 0;
        for (auto item : A) {
                result += item;
        }

        printf("%lld\n", result);

        return 0;
}
*/

// 126
/*
struct Node {
        unsigned int connect;
        unsigned int weight;
};

int main() {
        LL N;

        scanf("%lld", &N);

        static vector<Node> node[100010];
        static char color[100010];

        for (int i = 0; i < N - 1; i++) {
                unsigned int u, v, w;

                scanf("%d %d %d", &u, &v, &w);

                Node n1;
                n1.connect = v;
                n1.weight = w;
                node[u].push_back(n1);

                Node n2;
                n2.connect = u;
                n2.weight = w;
                node[v].push_back(n2);
        }

        for (int i = 0; i <= N; i++) {
                color[i] = -1;
        }

        std::queue<unsigned int> q;

        color[1] = 0;
        q.push(1);

        //���D��T��
        while (q.size() > 0) {
                unsigned int index = q.front();
                q.pop();

                char c = color[index];

                for (auto item : node[index]) {
                        if (color[item.connect] < 0) {
                                //�܂��ʂ��Ă��Ȃ��m�[�h
                                color[item.connect] = item.weight % 2 == 0 ? c :
1 - c; q.push(item.connect);
                        }
                }
        }

        for (int i = 1; i <= N; i++) {
                printf("%d\n", color[i]);
        }

        return 0;
}
*/