Problem p04035 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n;
  ll l;
  cin >> n >> l;

  vector<ll> a(n), sum(n);
  rep(i, 0, n) cin >> a[i];
  sum[0] = a[0];
  rep(i, 1, n) sum[i] = sum[i - 1] + a[i];

  priority_queue<pair<ll, tuple<int, int, int>>,
                 vector<pair<ll, tuple<int, int, int>>>>
      que;
  rep(i, 0, n - 1) if (a[i] + a[i + 1] >= l)
      que.push({a[i] + a[i + 1], make_tuple(i, i + 1, i)});
  vector<int> head(n), tail(n);
  rep(i, 0, n) head[i] = tail[i] = i;
  vector<int> ans;
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    ll len = p.first;
    int s = get<0>(p.second), t = get<1>(p.second);
    if (head[s] != s)
      continue;
    if (tail[t] != t)
      continue;
    if (len < l)
      break;
    // show(s);
    // show(t);
    // show(len);
    // show(get<2>(p.second));
    head[t] = s;
    tail[s] = t;
    // cerr << head << endl;
    // cerr << tail << endl;
    ans.push_back(get<2>(p.second));
    if (s != 0) {
      int s_ = head[s - 1], t_ = s - 1;
      ll len_ = sum[t];
      if (s_ > 0)
        len -= sum[s_ - 1];
      que.push({len_, make_tuple(s_, t, t_)});
    }
    if (t != n - 1) {
      int s_ = t + 1, t_ = tail[t + 1];
      ll len_ = sum[t_];
      if (s > 0)
        len_ -= sum[s - 1];
      que.push({len_, make_tuple(s, t_, t)});
    }
  }
  if (ans.size() != n - 1) {
    cout << "Impossible" << endl;
    return;
  }
  cout << "Possible" << endl;
  for (int i = n - 2; i >= 0; --i)
    cout << ans[i] + 1 << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n;
  ll l;
  cin >> n >> l;

  vector<ll> a(n), sum(n);
  rep(i, 0, n) cin >> a[i];
  sum[0] = a[0];
  rep(i, 1, n) sum[i] = sum[i - 1] + a[i];

  priority_queue<pair<ll, tuple<int, int, int>>,
                 vector<pair<ll, tuple<int, int, int>>>>
      que;
  rep(i, 0, n - 1) if (a[i] + a[i + 1] >= l)
      que.push({a[i] + a[i + 1], make_tuple(i, i + 1, i)});
  vector<int> head(n), tail(n);
  rep(i, 0, n) head[i] = tail[i] = i;
  vector<int> ans;
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    ll len = p.first;
    int s = get<0>(p.second), t = get<1>(p.second);
    if (head[s] != s)
      continue;
    if (tail[t] != t)
      continue;
    if (len < l)
      break;
    // show(s);
    // show(t);
    // show(len);
    // show(get<2>(p.second));
    head[t] = s;
    tail[s] = t;
    // cerr << head << endl;
    // cerr << tail << endl;
    ans.push_back(get<2>(p.second));
    if (s != 0) {
      int s_ = head[s - 1], t_ = s - 1;
      ll len_ = sum[t];
      if (s_ > 0)
        len -= sum[s_ - 1];
      que.push({len_, make_tuple(s_, t, t_)});
    } else if (t != n - 1) {
      int s_ = t + 1, t_ = tail[t + 1];
      ll len_ = sum[t_];
      if (s > 0)
        len_ -= sum[s - 1];
      que.push({len_, make_tuple(s, t_, t)});
    }
  }
  if (ans.size() != n - 1) {
    cout << "Impossible" << endl;
    return;
  }
  cout << "Possible" << endl;
  for (int i = n - 2; i >= 0; --i)
    cout << ans[i] + 1 << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}