Problem p03413 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int N;
int A[1005];
bool Special[1005];
vector<pair<int, int>> V[2];
vector<int> Pos;
void Read() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int x;
    cin >> x;
    V[i % 2].push_back(make_pair(x, i));
  }
  sort(V[0].begin(), V[0].end());
  sort(V[1].begin(), V[1].end());
}

void findPos() {
  long long s[] = {0, 0}, point[] = {0, 0};
  for (int ind = 0; ind <= 1; ind++) {
    s[ind] = V[ind].back().first, point[ind] = V[ind].size() - 1;
    for (int i = point[ind] - 1; i >= 0; i--) {
      if (s[ind] + V[ind][i].first > s[ind]) {
        s[ind] += V[ind][i].first;
        point[ind] = i;
      }
    }
  }
  if (s[0] > s[1]) {
    for (int i = V[0].size() - 1; i >= point[0]; i--)
      Pos.push_back(V[0][i].second);
    cout << s[0] << "\n";
  }

  else {
    for (int i = V[1].size() - 1; i >= point[1]; i--)
      Pos.push_back(V[1][i].second);
    cout << s[1] << "\n";
  }
}

void Solve() {
  for (int i = 0; i < Pos.size(); i++)
    Special[Pos[i]] = 1;
  vector<int> Aux, Sol;
  for (int i = 1; i <= N; i++)
    Aux.push_back(i);
  while (Special[Aux[0]] == 0) {
    Aux.erase(Aux.begin());
    Sol.push_back(1);
  }
  while (Special[Aux.back()] == 0) {
    Sol.push_back(Aux.size());
    Aux.erase(prev(Aux.end()));
  }
  while (Aux.size() > 1) {
    auto it = Aux.begin();
    for (int i = 1; i < Aux.size() - 1; i++, it = next(it)) {
      if (Special[Aux[i - 1]] == Special[Aux[i + 1]]) {
        Sol.push_back(i + 1);
        Special[Aux[i]] = Special[Aux[i - 1]];
        Aux.erase(next(it));
        Aux.erase(prev(it));
        break;
      }
    }
  }

  cout << Sol.size() << "\n";
  for (int i = 0; i < Sol.size(); i++)
    cout << Sol[i] << "\n";
}
int main() {
  Read();
  findPos();
  Solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int N;
int A[1005];
bool Special[1005];
vector<pair<int, int>> V[2];
vector<int> Pos;
void Read() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int x;
    cin >> x;
    V[i % 2].push_back(make_pair(x, i));
  }
  sort(V[0].begin(), V[0].end());
  sort(V[1].begin(), V[1].end());
}

void findPos() {
  long long s[] = {0, 0}, point[] = {0, 0};
  for (int ind = 0; ind <= 1; ind++) {
    s[ind] = V[ind].back().first, point[ind] = V[ind].size() - 1;
    for (int i = point[ind] - 1; i >= 0; i--) {
      if (s[ind] + V[ind][i].first > s[ind]) {
        s[ind] += V[ind][i].first;
        point[ind] = i;
      }
    }
  }
  if (s[0] > s[1]) {
    for (int i = V[0].size() - 1; i >= point[0]; i--)
      Pos.push_back(V[0][i].second);
    cout << s[0] << "\n";
  }

  else {
    for (int i = V[1].size() - 1; i >= point[1]; i--)
      Pos.push_back(V[1][i].second);
    cout << s[1] << "\n";
  }
}

void Solve() {
  for (int i = 0; i < Pos.size(); i++)
    Special[Pos[i]] = 1;
  vector<int> Aux, Sol;
  for (int i = 1; i <= N; i++)
    Aux.push_back(i);
  while (Special[Aux[0]] == 0) {
    Aux.erase(Aux.begin());
    Sol.push_back(1);
  }
  while (Special[Aux.back()] == 0) {
    Sol.push_back(Aux.size());
    Aux.erase(prev(Aux.end()));
  }
  while (Aux.size() > 1) {
    auto it = next(Aux.begin());
    for (int i = 1; i < Aux.size() - 1; i++, it = next(it)) {
      if (Special[Aux[i - 1]] == Special[Aux[i + 1]]) {
        Sol.push_back(i + 1);
        Special[Aux[i]] = Special[Aux[i - 1]];
        Aux.erase(next(it));
        Aux.erase(prev(it));
        break;
      }
    }
  }

  cout << Sol.size() << "\n";
  for (int i = 0; i < Sol.size(); i++)
    cout << Sol[i] << "\n";
}
int main() {
  Read();
  findPos();
  Solve();
  return 0;
}