Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
#define mop 1000000007
using namespace std;

struct items {
  ll t, v;
};
static const ll MAX = 100001;

bool asc(const items &left, const items &right) {
  return left.v == right.v ? left.t < right.t : left.v < right.v;
}
int main() {

  ll n, k;
  cin >> n >> k;
  vector<items> lis, rem;
  for (ll i = 0; i < n; i++) {
    items p;
    cin >> p.t >> p.v;
    lis.push_back(p);
  }
  sort(lis.begin(), lis.end(), asc);
  reverse(lis.begin(), lis.end());
  ll eachmax[MAX] = {};
  ll per = 0;
  for (ll i = 0; i < n; i++) {
    if (eachmax[lis[i].t] < lis[i].v) {
      eachmax[lis[i].t] = lis[i].v;
    }
  }
  ll numt = 0;
  ll exist[MAX] = {};
  for (ll i = 0; i < k; i++) {
    per += lis[i].v;
    if (exist[lis[i].t] == 0) {
      numt++;
    } else {
      rem.push_back(lis[i]);
    }
    exist[lis[i].t] = 1;
    //        cout << lis[i].t << endl;
  }
  reverse(rem.begin(), rem.end());
  ll ans = 0;
  if (ans < per + numt * numt) {
    ans = per + numt * numt;
  }

  //    cout << ans << endl;
  vector<ll> memo;
  for (ll i = 0; i < n; i++) {
    if (exist[i] == 0 and eachmax[i] > 0) {
      memo.push_back(eachmax[i]);
    }
  }
  sort(memo.begin(), memo.end());
  reverse(memo.begin(), memo.end());
  //    cout << rem.size() << endl;
  //    cout << memo.size() << endl;
  for (ll i = 0; i < memo.size(); i++) {
    per -= rem[i].v;
    per += memo[i];
    numt++;
    if (ans < per + numt * numt) {
      ans = per + numt * numt;
    }
    //        cout << ans << endl;
  }
  //    cout << "mofu" << endl;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
#define mop 1000000007
using namespace std;

struct items {
  ll t, v;
};
static const ll MAX = 100001;

bool asc(const items &left, const items &right) {
  return left.v == right.v ? left.t < right.t : left.v < right.v;
}
int main() {

  ll n, k;
  cin >> n >> k;
  vector<items> lis, rem;
  for (ll i = 0; i < n; i++) {
    items p;
    cin >> p.t >> p.v;
    lis.push_back(p);
  }
  sort(lis.begin(), lis.end(), asc);
  reverse(lis.begin(), lis.end());
  ll eachmax[MAX] = {};
  ll per = 0;
  for (ll i = 0; i < n; i++) {
    if (eachmax[lis[i].t] < lis[i].v) {
      eachmax[lis[i].t] = lis[i].v;
    }
  }
  ll numt = 0;
  ll exist[MAX] = {};
  for (ll i = 0; i < k; i++) {
    per += lis[i].v;
    if (exist[lis[i].t] == 0) {
      numt++;
    } else {
      rem.push_back(lis[i]);
    }
    exist[lis[i].t] = 1;
    //        cout << lis[i].t << endl;
  }
  reverse(rem.begin(), rem.end());
  ll ans = 0;
  if (ans < per + numt * numt) {
    ans = per + numt * numt;
  }

  //    cout << ans << endl;
  vector<ll> memo;
  for (ll i = 0; i < n; i++) {
    if (exist[i] == 0 and eachmax[i] > 0) {
      memo.push_back(eachmax[i]);
    }
  }
  sort(memo.begin(), memo.end());
  reverse(memo.begin(), memo.end());
  //    cout << rem.size() << endl;
  //    cout << memo.size() << endl;
  for (ll i = 0; i < min(memo.size(), rem.size()); i++) {
    per -= rem[i].v;
    per += memo[i];
    numt++;
    if (ans < per + numt * numt) {
      ans = per + numt * numt;
    }
    //        cout << ans << endl;
  }
  //    cout << "mofu" << endl;
  cout << ans << endl;
}