Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define REP(n) for (int i = 0; i < n; i++)

int flowers[101] = {0};

bool pairCompare(const pair<int, long long> &firstElof,
                 const pair<int, long long> &secondElof) {
  return firstElof.second > secondElof.second;
}

int type_cnt[100001] = {0};
int main() {
  long long N, K;
  // vector < pair<int,long long> > all_sushi;
  vector<long long> y0_sushi;
  vector<long long> y1_sushi;

  cin >> N >> K;
  vector<vector<long long>> type_vector(N + 1);
  int type_cnt_ofs = 0;
  REP(N) {
    long long t, d;
    cin >> t >> d;
    type_vector[t].push_back(d);
  }

  REP(N) {
    if (type_vector[i].size() == 0) {
      continue;
    }
    sort(type_vector[i].begin(), type_vector[i].end());
    y1_sushi.push_back(type_vector[i].back());
    type_vector[i].pop_back();
    y0_sushi.insert(y0_sushi.end(), type_vector[i].begin(),
                    type_vector[i].end());
  }

  sort(y1_sushi.rbegin(), y1_sushi.rend());
  sort(y0_sushi.rbegin(), y0_sushi.rend());

  /*
  REP(N + 1) {
          if (i > 0) {
                  type_cnt[i] += type_cnt[i - 1];
                  if (type_cnt[i] == 0) {
                          type_cnt_ofs = i;
                  }
          }
  }
  */

  long long ans = 0;
  long long X = 0;
  long long Y = max(0, (int)(K - y0_sushi.size()));
  REP(Y) { X += y1_sushi[i]; }
  REP(K - Y) { X += y0_sushi[i]; }
  while (true) {
    long long tmp = X + Y * Y;
    if (tmp > ans) {
      ans = tmp;
    }
    if (Y >= K || Y >= y1_sushi.size())
      break;
    X += y1_sushi[Y];
    X -= y0_sushi[K - Y - 1];
    Y++;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define REP(n) for (int i = 0; i < n; i++)

int flowers[101] = {0};

bool pairCompare(const pair<int, long long> &firstElof,
                 const pair<int, long long> &secondElof) {
  return firstElof.second > secondElof.second;
}

int type_cnt[100001] = {0};
int main() {
  long long N, K;
  // vector < pair<int,long long> > all_sushi;
  vector<long long> y0_sushi;
  vector<long long> y1_sushi;

  cin >> N >> K;
  vector<vector<long long>> type_vector(N + 1);
  int type_cnt_ofs = 0;
  REP(N) {
    long long t, d;
    cin >> t >> d;
    type_vector[t].push_back(d);
  }

  REP(N + 1) {
    if (type_vector[i].size() == 0) {
      continue;
    }
    sort(type_vector[i].begin(), type_vector[i].end());
    y1_sushi.push_back(type_vector[i].back());
    type_vector[i].pop_back();
    y0_sushi.insert(y0_sushi.end(), type_vector[i].begin(),
                    type_vector[i].end());
  }

  sort(y1_sushi.rbegin(), y1_sushi.rend());
  sort(y0_sushi.rbegin(), y0_sushi.rend());

  /*
  REP(N + 1) {
          if (i > 0) {
                  type_cnt[i] += type_cnt[i - 1];
                  if (type_cnt[i] == 0) {
                          type_cnt_ofs = i;
                  }
          }
  }
  */

  long long ans = 0;
  long long X = 0;
  long long Y = max(0, (int)(K - y0_sushi.size()));
  REP(Y) { X += y1_sushi[i]; }
  REP(K - Y) { X += y0_sushi[i]; }
  while (true) {
    long long tmp = X + Y * Y;
    if (tmp > ans) {
      ans = tmp;
    }
    if (Y >= K || Y >= y1_sushi.size())
      break;
    X += y1_sushi[Y];
    X -= y0_sushi[K - Y - 1];
    Y++;
  }

  cout << ans << endl;
  return 0;
}