Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

int N;
std::vector<int> a;
std::priority_queue<int, std::vector<int>, std::greater<int>> a_prime_former;
std::priority_queue<int, std::vector<int>, std::less<int>> a_prime_latter;
long max_score = 0;
long sum_a_prime_former = 0;
long sum_a_prime_latter = 0;
std::vector<long> score;

int main() {
  std::cin >> N;
  a.assign(3 * N, 0);
  score.assign(N + 1, 0);
  int counter = 0;
  for (auto &x : a) {
    std::cin >> x;
    if (counter < N) {
      a_prime_former.push(x);
      sum_a_prime_former += x;
    } else if (counter >= 2 * N) {
      a_prime_latter.push(x);
      sum_a_prime_latter += x;
    }
    ++counter;
  }
  score[0] += sum_a_prime_former;
  score[N] -= sum_a_prime_latter;
  for (int k = N; k < 2 * N; ++k) {
    // As for maximizeing the former, ...
    int min_element, push_element;
    min_element = a_prime_former.top();
    a_prime_former.pop();
    sum_a_prime_former -= min_element;
    push_element = std::max(min_element, a[k]);
    a_prime_former.push(push_element);
    sum_a_prime_former += push_element;
    // Records
    score[k - N + 1] += sum_a_prime_former;
  }
  for (int k = 2 * N - 1; k >= N - 1; --k) {
    // As for minimizing latter, ...
    int max_element, push_element;
    max_element = a_prime_latter.top();
    a_prime_latter.pop();
    sum_a_prime_latter -= max_element;
    push_element = std::min(max_element, a[k]);
    a_prime_latter.push(push_element);
    sum_a_prime_latter += push_element;
    // Records
    score[k - N] -= sum_a_prime_latter;
  }
  max_score = *std::max_element(score.begin(), score.end());
  std::cout << max_score << std::endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

int N;
std::vector<int> a;
std::priority_queue<int, std::vector<int>, std::greater<int>> a_prime_former;
std::priority_queue<int, std::vector<int>, std::less<int>> a_prime_latter;
long max_score = 0;
long sum_a_prime_former = 0;
long sum_a_prime_latter = 0;
std::vector<long> score;

int main() {
  std::cin >> N;
  a.assign(3 * N, 0);
  score.assign(N + 1, 0);
  int counter = 0;
  for (auto &x : a) {
    std::cin >> x;
    if (counter < N) {
      a_prime_former.push(x);
      sum_a_prime_former += x;
    } else if (counter >= 2 * N) {
      a_prime_latter.push(x);
      sum_a_prime_latter += x;
    }
    ++counter;
  }
  score[0] += sum_a_prime_former;
  score[N] -= sum_a_prime_latter;
  for (int k = N; k < 2 * N; ++k) {
    // As for maximizeing the former, ...
    int min_element, push_element;
    min_element = a_prime_former.top();
    a_prime_former.pop();
    sum_a_prime_former -= min_element;
    push_element = std::max(min_element, a[k]);
    a_prime_former.push(push_element);
    sum_a_prime_former += push_element;
    // Records
    score[k - N + 1] += sum_a_prime_former;
  }
  for (int k = 2 * N - 1; k >= N; --k) {
    // As for minimizing latter, ...
    int max_element, push_element;
    max_element = a_prime_latter.top();
    a_prime_latter.pop();
    sum_a_prime_latter -= max_element;
    push_element = std::min(max_element, a[k]);
    a_prime_latter.push(push_element);
    sum_a_prime_latter += push_element;
    // Records
    score[k - N] -= sum_a_prime_latter;
  }
  max_score = *std::max_element(score.begin(), score.end());
  std::cout << max_score << std::endl;
}