Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T)                                                        \
  for_each(begin(A), end(A), [&](T i) { cout << i << " "; });                  \
  cout << "\n"
#define MOD 1000000007

typedef long lint;
typedef pair<lint, lint> P;
bool lessPair(const P &l, const P &r) { return l.second < r.second; }
bool morePair(const P &l, const P &r) { return l.second > r.second; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lint N;
  cin >> N;
  lint K;
  cin >> K;

  vector<P> sushi(N);

  REP(i, N) {
    lint t;
    cin >> t;
    lint d;
    cin >> d;

    sushi[i] = P(t, d);
  }

  sort(sushi.begin(), sushi.end(), morePair);

  vector<lint> res(N + 1, 0);
  vector<lint> best_kind(N + 1, 0);
  vector<lint> rest;

  lint min_kind = 0;
  lint res_val = 0;
  REP(i, K) {
    lint kind = sushi[i].first;
    lint val = sushi[i].second;

    if (best_kind[kind] == 0) {
      best_kind[kind] = val;
      res[kind] = val;
      min_kind++;
    } else {
      rest.push_back(val);
    }
    res_val += val;
  }

  res[min_kind] = res_val + min_kind * min_kind;

  lint max_kind = min_kind;
  FOR(i, K, N) {
    lint kind = sushi[i].first;
    lint val = sushi[i].second;

    // PRINT(best_kind[kind]);

    if (best_kind[kind] == 0) {
      best_kind[kind] = val;
      res_val += val;
      res_val -= rest[rest.size() - 1];
      rest.pop_back();
      max_kind++;

      res[max_kind] = res_val + max_kind * max_kind;
      if (rest.size() == 0)
        break;
    } else {
    }
  }

  //    EACHPRINT(res, lint);

  lint m = *max_element(res.begin() + min_kind, res.begin() + max_kind + 1);

  PRINT(m);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T)                                                        \
  for_each(begin(A), end(A), [&](T i) { cout << i << " "; });                  \
  cout << "\n"
#define MOD 1000000007

typedef long lint;
typedef pair<lint, lint> P;
bool lessPair(const P &l, const P &r) { return l.second < r.second; }
bool morePair(const P &l, const P &r) { return l.second > r.second; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lint N;
  cin >> N;
  lint K;
  cin >> K;

  vector<P> sushi(N);

  REP(i, N) {
    lint t;
    cin >> t;
    lint d;
    cin >> d;

    sushi[i] = P(t, d);
  }

  sort(sushi.begin(), sushi.end(), morePair);

  vector<lint> res(N + 1, 0);
  vector<lint> best_kind(N + 1, 0);
  vector<lint> rest;

  lint min_kind = 0;
  lint res_val = 0;
  REP(i, K) {
    lint kind = sushi[i].first;
    lint val = sushi[i].second;

    if (best_kind[kind] == 0) {
      best_kind[kind] = val;
      res[kind] = val;
      min_kind++;
    } else {
      rest.push_back(val);
    }
    res_val += val;
  }

  res[min_kind] = res_val + min_kind * min_kind;

  lint max_kind = min_kind;
  FOR(i, K, N) {
    lint kind = sushi[i].first;
    lint val = sushi[i].second;

    if (rest.size() == 0)
      break;

    // PRINT(best_kind[kind]);

    if (best_kind[kind] == 0) {
      best_kind[kind] = val;
      res_val += val;
      res_val -= rest[rest.size() - 1];
      rest.pop_back();
      max_kind++;

      res[max_kind] = res_val + max_kind * max_kind;
      if (rest.size() == 0)
        break;
    } else {
    }
  }

  //    EACHPRINT(res, lint);

  lint m = *max_element(res.begin() + min_kind, res.begin() + max_kind + 1);

  PRINT(m);

  return 0;
}