Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \
       << (first) << ", " << (last) << ")" << endl;                            \
  for (int i = (first); i < (last); ++i) {                                     \
    cout << #a << "[" << i << "] = " << (a)[i] << endl;                        \
  }
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<pii> v(N);
  REP(i, N) {
    int t, d;
    cin >> t >> d;
    t--;
    v[i] = pii(d, t);
  }
  sort(all(v), greater<pii>());

  vector<int> variety(N + 1);
  vector<int> w;
  variety[0] = 0;
  vector<bool> used(N, false);
  REP(i, N) {
    if (!used[v[i].second]) {
      used[v[i].second] = true;
      variety[i + 1] = variety[i] + 1;
      w.pb(v[i].first);
    } else {
      variety[i + 1] = variety[i];
    }
  }

  vector<ll> acc_v_first(N + 1);
  acc_v_first[0] = 0;
  REP(i, N) { acc_v_first[i + 1] = acc_v_first[i] + v[i].first; }

  vector<ll> acc_w(w.size() + 1);
  acc_w[0] = 0;
  REP(i, w.size()) { acc_w[i + 1] = acc_w[i] + w[i]; }

  ll result = 0;

  REP(l, K + 1) {
    PRINT(l);
    int x0 = variety[l];
    int x1 = K - l;
    PRINT(x0);
    PRINT(x1);
    ll x = x0 + x1;
    ll variety_bonus = x * x;
    ll base_total_deliciousness = acc_v_first[l] + (acc_w[x] - acc_w[x0]);
    PRINT(variety_bonus);
    PRINT(base_total_deliciousness);
    ll satisfaction = base_total_deliciousness + variety_bonus;
    PRINT(satisfaction);
    amax(result, satisfaction);
  }
  cout << result << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \
       << (first) << ", " << (last) << ")" << endl;                            \
  for (int i = (first); i < (last); ++i) {                                     \
    cout << #a << "[" << i << "] = " << (a)[i] << endl;                        \
  }
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<pii> v(N);
  REP(i, N) {
    int t, d;
    cin >> t >> d;
    t--;
    v[i] = pii(d, t);
  }
  sort(all(v), greater<pii>());

  vector<int> variety(N + 1);
  vector<int> w;
  variety[0] = 0;
  vector<bool> used(N, false);
  REP(i, N) {
    if (!used[v[i].second]) {
      used[v[i].second] = true;
      variety[i + 1] = variety[i] + 1;
      w.pb(v[i].first);
    } else {
      variety[i + 1] = variety[i];
    }
  }

  vector<ll> acc_v_first(N + 1);
  acc_v_first[0] = 0;
  REP(i, N) { acc_v_first[i + 1] = acc_v_first[i] + v[i].first; }

  vector<ll> acc_w(w.size() + 1);
  acc_w[0] = 0;
  REP(i, w.size()) { acc_w[i + 1] = acc_w[i] + w[i]; }

  ll result = 0;

  REP(l, K + 1) {
    PRINT(l);
    int x0 = variety[l];
    int x1 = K - l;
    PRINT(x0);
    PRINT(x1);
    ll x = x0 + x1;
    if (x > w.size()) {
      continue;
    }
    ll variety_bonus = x * x;
    ll base_total_deliciousness = acc_v_first[l] + (acc_w[x] - acc_w[x0]);
    PRINT(variety_bonus);
    PRINT(base_total_deliciousness);
    ll satisfaction = base_total_deliciousness + variety_bonus;
    PRINT(satisfaction);
    amax(result, satisfaction);
  }
  cout << result << endl;
  return 0;
}