Problem:
古本屋(Books)
あなたの町にはJOI 古本店という老舗の古本屋があり，あなたはJOI 古本店をよく利用している．それぞれの本には基準価格が定まっており，JOI 古本店に行けばその価格で買い取ってもらえる．
JOI 古本店では，本を小説,漫画,雑誌など10 種類のジャンルに分類して扱っている．ジャンルには1から10 までの番号が付けられている．JOI 古本店には，同じジャンルの本をまとめて買い取ってもらうと高値で買い取ってくれるというサービスがある．具体的には，同じジャンルの本をまとめてT 冊買い取ってもらう場合，そのジャンルの本の一冊あたりの買取価格が基準価格よりT - 1 円高くなる．例えば，同じジャンルで基準価格100 円，120 円，150 円の本をまとめてJOI 古本店に売ったとすると，買取価格はそれぞれ102 円，122 円，152 円となる．
さて，あなたは一身上の都合で急遽引越しをすることになった．あなたはN 冊の本を持っているが，新しい住居にすべての本を持っていくことは困難なため，
N
冊の本のうち
K
冊をJOI 古本店に売ることにした．
課題
N
冊の本それぞれの基準価格とジャンルの番号が与えられるとき，合計買取価格の最大値を求めるプログラムを作成せよ．
制限
2 ≤
N
≤ 2000    あなたが持っている本の冊数
1 ≤
K
<
N
JOI 古本店に売る本の冊数
1 ≤
C
i
≤ 100000 = 10
5
i
番目の本の基準価格
1 ≤
G
i
≤ 10
i
番目の本のジャンルの番号
入力
標準入力から以下の入力を読み込め．
1 行目には整数
N
,
K
が空白を区切りとして書かれており，あなたの持っている本の冊数が
N
で，そのうち
K
冊をJOI 古本店に売ることを表す．
続く
N
行にはあなたの持っている本の情報が書かれている．
i
+ 1 行目(1 ≤
i
≤
N
) には，整数
C
i
,
G
i
が空白を区切りとして書かれており，
i
番目の本の基準価格が
C
i
で，ジャンルの番号が
G
i
であることを表す．
出力
標準出力に，合計買取価格の最大値を表す整数を1 行で出力せよ．
採点基準
採点用データのうち，
配点の20% 分については，
N
≤ 20 を満たす．
配点の20% 分については，すべての本のジャンルは1 または2 である．
配点の10% 分については，これら2 つの条件の両方を満たす．
配点の30% 分については，これら2 つの条件の少なくとも一方を満たす．
入出力の例
入力例
7 4
14 1
13 2
12 3
14 2
8 2
16 3
11 2
出力例
60
この入力例では，2 番目，4 番目，6 番目，7 番目の4 冊の本を売ったとき，ジャンル2 の本の買取価格が1 冊あたり2 円高くなるので，これらの本の買取価格は以下のようになる．
番号
基準価格
ジャンル
買取価格
2
4
6
7
13
14
16
11
2
2
3
2
15
16
16
13
よって合計買取価格は15 + 16 + 16 + 13 = 60 円である．このとき合計買取価格は最大となる．
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll d[15][2005], dp[2005][2005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, K;
  cin >> n >> K;
  vector<int> id, v[2005];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    id.pb(b - 1);
    v[b - 1].pb(a);
  }
  sort(id.begin(), id.end());
  id.erase(unique(id.begin(), id.end()), id.end());
  rep(i, id.size()) {
    int j = id[i];
    sort(v[j].begin(), v[j].end(), greater<int>());
  }
  memset(d, 0, sizeof(d));
  vector<P> p;
  for (int i = 0; i < id.size(); i++) {
    int j = id[i];
    REP(k, 1, v[j].size() + 1) {
      d[j][k] += d[j][k - 1] + v[j][k - 1] + (k - 1) * 2;
      p.pb(mp(k, d[j][k]));
    }
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < id.size(); i++) {
    for (int j = 0; j <= K; j++) {
      ll res = dp[i][j];
      for (int k = 0; k <= v[id[i]].size(); k++) {
        if (j >= k) {
          res = max(res, dp[i][j - k] + d[id[i]][k]);
        }
      }
      dp[i + 1][j] = res;
    }
  }
  cout << dp[id.size()][K] << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34960
final_rank: 16

code_snippet: #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll d[15][2005], dp[2005][2005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, K;
  cin >> n >> K;
  vector<int> id, v[2005];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    id.pb(b - 1);
    v[b - 1].pb(a);
  }
  sort(id.begin(), id.end());
  id.erase(unique(id.begin(), id.end()), id.end());
  rep(i, id.size()) {
    int j = id[i];
    sort(v[j].rbegin(), v[j].rend());
  }
  memset(d, 0, sizeof(d));
  vector<P> p;
  rep(i, id.size()) {
    int j = id[i];
    REP(k, 1, v[j].size() + 1) {
      d[j][k] += d[j][k - 1] + v[j][k - 1] + (k - 1) * 2;
      p.pb(mp(k, d[j][k]));
    }
  }
  sort(p.rbegin(), p.rend());
  memset(dp, 0, sizeof(dp));
  rep(i, id.size()) {
    rep(j, K + 1) {
      ll res = dp[i][j];
      rep(k, v[id[i]].size() + 1) {
        if (j >= k) {
          res = max(res, dp[i][j - k] + d[id[i]][k]);
        }
      }
      dp[i + 1][j] = res;
    }
  }
  cout << dp[id.size()][K] << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34932
final_rank: 14

code_snippet: #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll d[15][2005], dp[2005][2005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, K;
  cin >> n >> K;
  vector<int> id, v[2005];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    id.pb(b - 1);
    v[b - 1].pb(a);
  }
  sort(id.begin(), id.end());
  id.erase(unique(id.begin(), id.end()), id.end());
  rep(i, id.size()) {
    int j = id[i];
    sort(v[j].begin(), v[j].end(), greater<int>());
  }
  memset(d, 0, sizeof(d));
  vector<P> p;
  rep(i, id.size()) {
    int j = id[i];
    REP(k, 1, v[j].size() + 1) {
      d[j][k] += d[j][k - 1] + v[j][k - 1] + (k - 1) * 2;
      p.pb(mp(k, d[j][k]));
    }
  }
  memset(dp, 0, sizeof(dp));
  rep(i, id.size()) {
    rep(j, K + 1) {
      ll res = dp[i][j];
      rep(k, v[id[i]].size() + 1) {
        if (j >= k) {
          res = max(res, dp[i][j - k] + d[id[i]][k]);
        }
      }
      dp[i + 1][j] = res;
    }
  }
  cout << dp[id.size()][K] << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34956
final_rank: 15

code_snippet: #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll d[15][2005], dp[2005][2005];
int main() {
  int n, K;
  cin >> n >> K;
  vector<int> id, v[2005];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    id.push_back(b - 1);
    v[b - 1].push_back(a);
  }
  sort(id.begin(), id.end());
  id.erase(unique(id.begin(), id.end()), id.end());
  rep(i, id.size()) {
    int j = id[i];
    sort(v[j].begin(), v[j].end(), greater<int>());
  }
  memset(d, 0, sizeof(d));
  rep(i, id.size()) {
    int j = id[i];
    REP(k, 1, v[j].size() + 1) {
      d[j][k] += d[j][k - 1] + v[j][k - 1] + (k - 1) * 2;
    }
  }
  memset(dp, 0, sizeof(dp));
  rep(i, id.size()) {
    rep(j, K + 1) {
      ll res = dp[i][j];
      rep(k, v[id[i]].size() + 1) {
        if (j >= k) {
          res = max(res, dp[i][j - k] + d[id[i]][k]);
        }
      }
      dp[i + 1][j] = res;
    }
  }
  cout << dp[id.size()][K] << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34812
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
//typedef long long ll;
typedef int ll;
typedef pair<ll, ll> P;
const long long INF = 1e9;
template <typename T>
void input_arr(vector<T>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}
}
template <typename T, typename Q>
void input_arr(vector<pair<T, Q>>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i].first >> A[i].second;
	}
}
template <typename T>
void input_arr(vector<vector<T>>& A, ll H, ll W) {
	for (ll i = 0; i < H; i++) {
		for (ll j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
}
vector<ll>num(20, 0);
bool cmp(P a, P b) {
    if (a.first + num[a.second] - 1 != b.first + num[b.second] - 1) {
        return a.first + num[a.second] - 1 > b.first + num[b.second] - 1;
    }
    else return num[a.second] > num[b.second];
}
int main() {
    ll N, M; cin >> N >> M;
    vector<P>A(N); input_arr(A, N);
    sort(A.begin(), A.end(), greater<P>());
    vector<P>ans;
    for (ll i = 0; i < M; i++) {
        ans.push_back(A[i]);
        num[A[i].second]++;
    }
    sort(ans.begin(), ans.end(), cmp);
    for (ll i = M; i < N; i++) {
        ll num1 = ans.back().first + num[ans.back().second] * (num[ans.back().second] - 1);
        ll num2 = A[i].first + (num[A[i].second] + 1) * num[A[i].second];
        if (ans.back().second == A[i].second) {
            num1 = ans.back().first;
            num2 = A[i].first;
        }
        if (num1 < num2) {
            num[ans.back().second]--;
            num[A[i].second]++;
            ans.pop_back();
            ans.push_back(A[i]);
        }
        sort(ans.begin(), ans.end(), cmp);
    }
    ll sum = 0;
    for (ll i = 0; i < M; i++) {
        sum += ans[i].first + num[ans[i].second] - 1;
    }
    cout << sum << endl;
}
accuracy: 0.3
time_ms: 20
memory_kb: 3412
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>
#include <random>
#include <complex>
#include <functional>
#include <tuple>

using namespace std;

#define Rep(i,a,b) for(ll i = a; i < b; ++i)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

namespace {
    template<typename T> void chmax(T& a, T b) { a = std::max(a, b); }
    template<typename T> void chmin(T& a, T b) { a = std::min(a, b); }
    template<typename T> void chadd(T& a, T b) { a = a + b; }

    void Cout(long long x, const char* end = "\n") { std::cout << x << end; }
    template <typename T> void Cout(const T& x, const char* end = "\n") { std::cout << x << end; }
    template <typename T> void Cout(const std::vector<T>& x, const char* sep = " ", const char* end = "\n") { for (std::size_t i = 0, sz = x.size(); i < sz; i++) { std::cout << x[i] << (i == sz - 1 ? end : sep); } }

    // 標準入出力
    struct inp {
        std::size_t sz;
        inp(std::size_t _sz = 1) : sz(_sz) {}
        template <typename T> operator T () const { T a; std::cin >> a; return a; }
        template <typename T> operator std::vector<T>() const { std::vector<T> a(sz); for (std::size_t i = 0; i < sz; i++) std::cin >> a[i]; return a; }
    };

    // 2次元用の標準入出力
    template<typename T>
    struct inpn {
        std::size_t szi, szj;
        inpn(std::size_t _szi, std::size_t _szj) : szi(_szi), szj(_szj) {}
        operator std::vector<std::vector<T>>() const {
            std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
            for (std::size_t i = 0; i < szi; ++i)
                for (std::size_t j = 0; j < szj; ++j) cin >> a[i][j];
            return a;
        }
    };

    inp inp1; // input one
}


int main() {

    int n, k;
    cin >> n >> k;
    vector<pair<int, int>> cg(n);
    rep(i, n) cin >> cg[i].first >> cg[i].second;

    // k冊売る

    vector<vector<ll>> book(10, vector<ll>());
    rep(i, n) {
        book[cg[i].second - 1].push_back(cg[i].first);
    }
    rep(i, 10) {
        sort(allof(book[i]), greater<ll>());
    }

    vector<vector<ll>> sum(10, vector<ll>(k + 1, 0LL));

    rep(g, 10) {
        rep(j, min((size_t)k, book[g].size())) {
            sum[g][j + 1] = sum[g][j] + book[g][j] + j * 2;
        }
    }

    vector<vector<ll>> dp(11, vector<ll>(k + 1, 0LL));
    rep(g, 10) {
        for (int i = 0; i <= k; ++i) { // i冊売った
            for (int j = 0; j <= i; ++j) { // そのうちジャンルgをj冊売った
                chmax(dp[g + 1][i], dp[g][i - j] + sum[g][j]);
            }
        }
    }

    Cout(dp[10][k]);

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3292
final_rank: 5

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>
#include <random>
#include <complex>
#include <functional>
#include <tuple>

using namespace std;

#define Rep(i,a,b) for(ll i = a; i < b; ++i)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

namespace {
    template<typename T> void chmax(T& a, T b) { a = std::max(a, b); }
    template<typename T> void chmin(T& a, T b) { a = std::min(a, b); }
    template<typename T> void chadd(T& a, T b) { a = a + b; }

    void Cout(long long x, const char* end = "\n") { std::cout << x << end; }
    template <typename T> void Cout(const T& x, const char* end = "\n") { std::cout << x << end; }
    template <typename T> void Cout(const std::vector<T>& x, const char* sep = " ", const char* end = "\n") { for (std::size_t i = 0, sz = x.size(); i < sz; i++) { std::cout << x[i] << (i == sz - 1 ? end : sep); } }

    // 標準入出力
    struct inp {
        std::size_t sz;
        inp(std::size_t _sz = 1) : sz(_sz) {}
        template <typename T> operator T () const { T a; std::cin >> a; return a; }
        template <typename T> operator std::vector<T>() const { std::vector<T> a(sz); for (std::size_t i = 0; i < sz; i++) std::cin >> a[i]; return a; }
    };

    // 2次元用の標準入出力
    template<typename T>
    struct inpn {
        std::size_t szi, szj;
        inpn(std::size_t _szi, std::size_t _szj) : szi(_szi), szj(_szj) {}
        operator std::vector<std::vector<T>>() const {
            std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
            for (std::size_t i = 0; i < szi; ++i)
                for (std::size_t j = 0; j < szj; ++j) cin >> a[i][j];
            return a;
        }
    };

    inp inp1; // input one
}


int main() {

    int n, k;
    cin >> n >> k;
    vector<pair<int, int>> cg(n);
    rep(i, n) cin >> cg[i].first >> cg[i].second;

    // k冊売る

    vector<vector<ll>> book(10, vector<ll>());
    rep(i, n) {
        book[cg[i].second - 1].push_back(cg[i].first);
    }
    rep(i, 10) {
        sort(allof(book[i]), greater<ll>());
    }

    vector<vector<ll>> sum(10, vector<ll>(k + 1, 0LL));

    rep(g, 10) {
        rep(j, min((size_t)k, book[g].size())) {
            sum[g][j + 1] = sum[g][j] + book[g][j] + j * 2;
        }
    }

    vector<ll> dp(k + 1, 0LL);
    rep(g, 10) {
        for (int i = k; i >= 0; --i) { // i冊売った
            for (int j = 0; j <= i; ++j) { // そのうちジャンルgをj冊売った
                chmax(dp[i], dp[i - j] + sum[g][j]);
            }
        }
    }

    Cout(dp[k]);

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3284
final_rank: 4

code_snippet: #include<bits/stdc++.h>
#define enld '\n'
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0,1,1};
const int dy[6] = {0, 1, 0, -1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,K;
    cin >> N >> K;
    vector<vector<int>> suzu(11);
    for(int i=0; i<N; i++){
        int a,b;
        cin >> a >> b;
        b--;
        suzu[b].push_back(a);
    }
    for(int i=0; i<10; i++){
        if(!suzu[i].size()) continue;
        sort(suzu[i].begin(),suzu[i].end(),greater<int>());
        for(int j=0; j<suzu[i].size()-1; j++){
            suzu[i][j+1] += suzu[i][j] + (j+1)*2;
        }
    }
    // for(int i=0; i<10; i++){
    //     for(int j=0; j<suzu[i].size(); j++){
    //         cout << suzu[i][j] << " ";
    //     }
    //     cout << enld;
    // }
    vector<int> ken(K+1,0);    
    for(int i=0; i<10; i++){
        if(!suzu[i].size()) continue;
        vector<int> temp = ken;
        for(int j=0; j<K; j++){
            for(int k=0; k<suzu[i].size(); k++){
                if(j+k+1 > K) continue;
                chmax(temp[j+k+1],suzu[i][k] + ken[j]);
            }
        }
        ken = temp;
    }
    cout << ken[K] << enld;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3184
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    vector<ll> mae(2005,2LL);
    mae[0]=0;
    for(int i = 1;i < 2005;++i) {
        mae[i]+=mae[i-1];
    }
    int n,K;
    cin>>n>>K;
    vector<pair<ll,int>> books;
    for(int i = 0;i < n;++i) {
        ll c;
        int g;
        cin>>c>>g;
        --g;
        books.emplace_back(c,g);
    }

    vector<vector<vector<ll>>> dp(2,vector<vector<ll>>(10,vector<ll>(n+1,0)));
    dp[0][books[0].second][1]=books[0].first;
    for(int i = 1;i < n;++i) {
        int dpind=i%2;
        int dppreind=1-dpind;
        for(int j = 0;j < 10;++j) {
            int k=1;
            while(k<=n&&dp[dppreind][j][k]!=0) {
                if(books[i].second==j) {
                    dp[dpind][j][k]=max(dp[dppreind][j][k],dp[dppreind][j][k-1]+books[i].first+mae[k-1]);
                }
                else {
                    dp[dpind][j][k]=dp[dppreind][j][k];
                }
                ++k;
            }
            if(books[i].second==j) {
                dp[dpind][j][k]=max(dp[dppreind][j][k],dp[dppreind][j][k-1]+books[i].first+mae[k-1]);
            }
            else {
                dp[dpind][j][k]=dp[dppreind][j][k];
            }
        }
    }

    int li=(n-1)%2;



    vector<vector<ll>> ep(2,vector<ll>(K+1,0));
    ep[0][1]=dp[li][0][1];
    for(int i = 0;i < 10;++i) {
        for(int j = 1;j <= n;++j) {
            if(i==0&&j==1)continue;
            int dpind=(i*n+j-1)%2;
            int dppreind=1-dpind;
            for(int l = 1;l <= K;++l) {
                ep[dpind][l]=ep[dppreind][l];
                if(l<j)continue;
                if(dp[li][i][j]==0)continue;
                if(l-j!=0&&ep[dppreind][l-j]==0)continue;
                ep[dpind][l]=max(ep[dpind][l],ep[dppreind][l-j]+dp[li][i][j]);
            }


        }
    }

    li = (10*n-1)%2;

    cout<<ep[li][K]<<endl;

    return 0;
}
accuracy: 0.1
time_ms: 10
memory_kb: 3404
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <utility>
using namespace std;
typedef pair<int, int> pii;

int main(){
    int n,k;
    cin >> n >> k;
    vector<vector<int>> val_pre(10);
    for(int i=0; i<n; i++){
        int v,g;
        cin >> v >> g;
        val_pre[g-1].push_back(v);
    }
    vector<int> dp(k+1, 0);
    for(int i=0; i<10; i++){
        sort(val_pre[i].begin(), val_pre[i].end(), greater<int>());
        vector<int> v(k+1, 0);
        int curr = 0;
        for(int j=0; j<min(k, (int)val_pre[i].size()); j++){
            curr += val_pre[i][j];
            v[j+1] = curr +j*(j+1);
        }
        for(int j=k; j>=0; j--){
            for(int s=0; s<=j; s++){
                dp[j] = max(dp[j], dp[s]+v[j-s]);
            }
        }
    }
    cout << dp[k] << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3192
final_rank: 2

code_snippet: #include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int N, K;
    cin >> N >> K;

    unordered_map<int, vector<int>> CG;

    for (int i = 1; i <= 10; i++) {
        CG[i].push_back(0);
    }

    for (int i = 0; i < N; i++) {
        int C, G;
        cin >> C >> G;
        CG[G].push_back(C);
    }

    for (int i = 1; i <= 10; i++) {
        sort(CG[i].begin() + 1, CG[i].end(), greater<>());
        for (int j = 1; j < CG[i].size(); j++) {
            CG[i][j] += CG[i][j - 1] + (j - 1) * 2;
        }
    }

    int dp[11][K + 1];
    fill(dp[0], dp[10] + K + 1, 0);

    for (int i = 1; i <= 10; i++) {
        for (int j = 0; j < CG[i].size(); j++) {
            for (int k = j; k <= K; k++) {
                dp[i][k] = max(dp[i][k], dp[i - 1][k - j] + CG[i][j]);
            }
        }
    }

    cout << dp[10][K] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3188
final_rank: 9

code_snippet: #include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int N, K;
    cin >> N >> K;

    unordered_map<int, vector<int>> CG;

    for (int i = 1; i <= 10; i++) {
        CG[i].push_back(0);
    }

    for (int i = 0; i < N; i++) {
        int C, G;
        cin >> C >> G;
        CG[G].push_back(C);
    }

    for (int i = 1; i <= 10; i++) {
        sort(CG[i].begin() + 1, CG[i].end(), greater<>());
        for (int j = 1; j < CG[i].size(); j++) {
            CG[i][j] += CG[i][j - 1];
        }
        for (int j = 2; j < CG[i].size(); j++) {
            CG[i][j] += j * (j - 1);
        }
    }

    int dp[11][K + 1];
    fill(dp[0], dp[10] + K + 1, 0);

    for (int i = 1; i <= 10; i++) {
        for (int j = 0; j < CG[i].size(); j++) {
            for (int k = j; k <= K; k++) {
                dp[i][k] = max(dp[i][k], dp[i - 1][k - j] + CG[i][j]);
            }
        }
    }

    cout << dp[10][K] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3188
final_rank: 9

code_snippet: #include "bits/stdc++.h"
using namespace std;

using int64 = long long;
using i64 = int64;

template<typename T>
using p_que = priority_queue<T>;
template<typename T>
using rp_que = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = (1 << 30) - 1;
constexpr int64 INF64 = (1ll << 60) - 1;

#define rep(i, N) for(int i=0;i<(int)(N);++i)
#define fs first
#define sc second
#define e_b emplace_back
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	return os << "P(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "[";
	for (auto& e : v) os << e << ", ";
	return os << "]";
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& m) {
	os << "{" << endl;
	for (auto& e : m) os << "(" << e.first << ", " << e.second << ")" << endl;
	return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& s) {
	os << "{" << endl;
	for (auto& e : s) os << ", " << e << endl;
	return os << "}";
}

template<typename T>
vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int64 gcd(int64 x, int64 y) {
	if (x == 0 || y == 0) return 0;
	int64 r;
	while ((r = y % x) != 0) {
		y = x;
		x = r;
	}
	return x;
}
int64 lcm(int64 x, int64 y) {
	if (x == 0 || y == 0) return 0;
	return x / gcd(x, y) * y;
}

int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };

void Main();
signed main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(30);
	Main();
}

/*----------------------------Insert from here!----------------------------*/

template <int mod>
struct ModInt {
	int x;

	ModInt() : x(0) {}

	ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

	ModInt& operator+=(const ModInt& p) {
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}

	ModInt& operator-=(const ModInt& p) {
		if ((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}

	ModInt& operator*=(const ModInt& p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}

	ModInt& operator/=(const ModInt& p) {
		*this *= p.inverse();
		return *this;
	}

	ModInt operator-() const { return ModInt(-x); }

	ModInt operator+(const ModInt& p) const { return ModInt(*this) += p; }

	ModInt operator-(const ModInt& p) const { return ModInt(*this) -= p; }

	ModInt operator*(const ModInt& p) const { return ModInt(*this) *= p; }

	ModInt operator/(const ModInt& p) const { return ModInt(*this) /= p; }

	bool operator==(const ModInt& p) const { return x == p.x; }

	bool operator!=(const ModInt& p) const { return x != p.x; }

	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}

	ModInt pow(int64_t n) const {
		ModInt ret(1), mul(x);
		while (n > 0) {
			if (n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}

	friend ostream& operator<<(ostream& os, const ModInt& p) {
		return os << p.x;
	}

	friend istream& operator>>(istream& is, ModInt& a) {
		int64_t t;
		is >> t;
		a = ModInt< mod >(t);
		return (is);
	}

	static int get_mod() { return mod; }
};

/*----------------------------Insert above here----------------------------*/

//const int MOD = 998244353;
//const int MOD = 1e9 + 7;
//const int MOD = 1234567;

void Main() {
	int N, K; cin >> N >> K;
	vector<vector<int64>> C(10);
	rep(i, N) {
		int c, g; cin >> c >> g;
		g--;
		C[g].emplace_back(c);
	}

	rep(i, 10) sort(rall(C[i]));

	vector<vector<int64>> sum(10);
	rep(i, 10) {
		sum[i] = C[i];
		for (int j = 1; j < sum[i].size(); ++j) {
			sum[i][j] += sum[i][j - 1];
			sum[i][j] += 2ll * j;
		}
	}

	auto dp = make_v(10, K + 1, 0ll);
	rep(i, min(K, (int)sum[0].size() - 1)) dp[0][i + 1] = sum[0][i];
	for (int i = 1; i < 10; ++i) {
		for (int j = 0; j < K + 1; ++j) {
			for (int k = 0; k <= j; ++k) {
				if (j == k) {
					dp[i][j] = max(dp[i][j], dp[i - 1][k]);
					continue;
				}
				if (j - k - 1 >= sum[i].size() || j - k - 1 < 0) {
					dp[i][j] = max(dp[i][j], dp[i - 1][j]);
					continue;
				}
				dp[i][j] = max(dp[i][j], dp[i - 1][k] + sum[i][j - k - 1]);
			}
		}
	}

	cout << dp[9][K] << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3300
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
 
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

/* next combination */
ll next_combination(ll sub) {
    ll x = sub & -sub, y = sub + x;
    return (((sub & ~y) / x) >> 1) | y;
}

int main(){
    ll n, k;
    cin >> n >> k;
    vector<P> p;
    ll c, g;
    REP(i, n){
        cin >> c >> g;
        g--;
        p.push_back(P{c, g});
    }
    VSORT(p);
    ll res = 0;
    ll bit = (1 << k) - 1;
    for (;bit < (1LL<<n); bit = next_combination(bit)){
        vll s(n), gg(10);
        for(ll i = 0; i < n; i++){
            if((bit >> i) & 1){
                s[i] = 1;
                gg[p[i].second]++;
            }
        }
        ll tmp = 0;
        REP(i, n){
            if(s[i]){
                tmp += (p[i].first + gg[p[i].second] - 1);
            }
        }
        res = max(res, tmp);
    }
        
    cout << res << endl;
    return 0;
}
accuracy: 0.1
time_ms: 40
memory_kb: 3156
final_rank: 20

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 100;
const long long LLINF = 2e18 + 129;
const long long MOD = 1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

template<typename T>
bool chmax(T &a, T b){
    if(a < b){
        a = b;
        return true;
    }else{
        return false;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    vector< vector<int> > v(10);
    rep(i, n){
        int c, g;
        cin >> c >> g;
        --g;
        v[g].push_back(c);
    }
    rep(i, 10){
        SORT_INV(v[i]);
        v[i].insert(v[i].begin(), 0);
        REP(j, 1, v[i].size()){
            v[i][j] += v[i][j - 1];
            v[i][j] += (j * (j - 1)) - ((j - 1) * (j - 2));
        }
        while(v[i].size() < k){
            v[i].push_back(v[i].back());
        }
    }

    int dp[11][2001] = {0};
    rep(i, 10){
        rep(j, k + 1){
            rep(l, j + 1){
                chmax(dp[i + 1][j], dp[i][l] + v[i][j - l]);
            }
        }
    }

    cout << dp[10][k] << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3312
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int f(int i) {
    return i * 2;
}

void solve() {
    int N, K;
    cin >> N >> K;
    vector<pii> B(N);
    for (int i = 0; i < N; i++) {
        ll s, k;
        cin >> s >> k;
        k--;
        B[i] = {k, s};
    }
    sort(all(B), [](pii a, pii b){
        if (a.first == b.first) return a.second > b.second;
        return a.first < b.first;
    });

    vector<int> offset(10, -1);
    for (int i = 0; i < N; i++) {
        if (offset[B[i].first] == -1) offset[B[i].first] = i;
    }

    vector<vector<int>> dp(K + 1, vector<int>(2, -1));

    dp[0][0] = 0;
    for(int i = 0; i < N; i++) {
        vector<vector<int>> ndp(K + 1, vector<int>(2, -1));
        for(int j = 0; j <= K; j++) {
            if (j + 1 <= K && dp[j][0] != -1) {
                chmax(ndp[j + 1][0], dp[j][0] + B[i].second + f(i - offset[B[i].first]));
            }
            // 切り替わるタイミングで0, そうでないとき1
            int nextf = 1 - ((i == N - 1) || ((i < N - 1) && B[i].first != B[i + 1].first));
            if (dp[j][0] != -1) chmax(ndp[j][nextf], dp[j][0]);
            if (dp[j][1] != -1) chmax(ndp[j][nextf], dp[j][1]);
        }
        dp = ndp;
    }

    cout << max(dp[K][0], dp[K][1]) << endl;
}

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3396
final_rank: 17

code_snippet: #include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

int main() {
    cin.tie(0);
	ios::sync_with_stdio(false);

	int N, K;
	cin >> N >> K;

	vector<vector<int>> v(10);
	FOR(i, 0, N) {
	    int C, G;
	    cin >> C >> G;
	    G--;
	    v[G].emplace_back(C);
	}

	map<int, vector<vector<int>>> memo;
	FOR(kind, 0, 10) {
        vector<vector<int>> dp(v[kind].size() + 1, vector<int>(N + 1, -INF));
	    dp[0][0] = 0;
        FOR(i, 0, v[kind].size()) {
            FOR(j, 0, N) {
                int price = v[kind][i];
                if (dp[i][j] == -INF) {
                    continue;
                }

                // not use
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

                // use
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + price);
            }
        }
        memo[kind] = dp;
    }

    vector<vector<int>> dp2(11, vector<int>(N + 1, -INF));
	dp2[0][0] = 0;

	FOR(kind, 0, 10) {
        FOR(i, 0, dp2[0].size()) {
            if (dp2[kind][i] == -INF) {
                continue;
            }
            int num = v[kind].size();
            FOR(j, 0, num + 1) {
                int price = memo[kind][num][j] + max(0, j - 1) * j;
                // use
                dp2[kind + 1][i + j] = max(dp2[kind + 1][i + j], dp2[kind][i] + price);
            }
        }
    }

    int ans = 0;
    FOR(i, 0, K + 1) {
        ans = max(ans, dp2[10][i]);
    }
    print(ans);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 32996
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	int n, k;
	cin >> n>>k;
	vector<vector<int> > pr(10, vector<int>(n+1,0));
	vector<int> ko(10, 0);
	for (int i = 0; i < n; i++) {
		int c, g; cin >> c >> g;
		g--;
		pr[g][ko[g]] = c;
		ko[g]++;
	}
	for (int i = 0; i < 10; i++) {
		sort(pr[i].begin(), pr[i].end());
		reverse(pr[i].begin(), pr[i].end());
	}
	vector<vector<int> > sum(10,vector<int> (n+1,0));
	for (int i = 0; i <10; i++) {
		int su = 0;
		for (int j = 1; j <= ko[i]; j++) {
			su += pr[i][j-1];
			sum[i][j] = su + j * (j - 1);
		}
	}
	vector<vector<int> > dp(10, vector<int>(n + 1, 0));
	dp[0] = sum[0];
	for (int i = 1; i < 10; i++) {
		for (int j = 0; j <= k; j++) {
			for (int kk = j; kk <= k; kk++) {
				dp[i][kk] = max(dp[i][kk], dp[i - 1][j] + sum[i][kk - j]);
			}
		}
	}
	cout << dp[9][k] << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3296
final_rank: 6

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

typedef long long ll;
typedef pair<int, int> P;

int MOD=1e9+7;
ll INF=1e18;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int main(){
    int n, k; cin>>n>>k;
    vector<int> c(n+10), g(n+10);
    vector<vector<int>> books(20);
    for(int i=0; i<n; i++) {
        cin>>c[i]>>g[i];
        books[g[i]].emplace_back(c[i]);
    }

    for(int i=0; i<=10; i++){
        sort(books[i].begin(), books[i].end(), greater<int>());
    }
    
    vector<vector<int>> sum(20, vector<int>(2010, 0));
    for(int i=1; i<=10; i++){
        for(int j=0; j<books[i].size(); j++){
            sum[i][j+1] = sum[i][j] + books[i][j] + j*2;
        }
    }

    vector<vector<int>> dp(20, vector<int>(k+10, 0));
    for(int i=1; i<=10; i++){
        for(int j=0; j<=k; j++){
            for(int l=0; j+l<=k; l++){
                dp[i][j+l]=max(dp[i][j+l], dp[i-1][j]+sum[i][l]);
            }
        }
    }
    cout << dp[10][k] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3300
final_rank: 7

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define int long long
signed main(){
  int N, K;
  cin >> N >> K;
  vector< vector<int> > B(10);
  for(int i = 0; i < N; ++i){
    int c, g;
    cin >> c >> g;
    B[g-1].push_back(c);
  }
  
  vector< vector<int> > DP(10, vector<int>(K+1, 0));
  for(int i = 0; i < 10; ++i){
    if(B[i].empty()) continue;
    sort(B[i].begin(), B[i].end(), greater<>());
    DP[i][1] = B[i][0];
    for(size_t j = 1; j < min(B[i].size(), (size_t)K); ++j){
      DP[i][j+1] = DP[i][j] + B[i][j] + 2*j;
    }
  }
  vector<int> dp(K+1,0);
  for(int i = 0; i < 10; ++i){
    vector<int> dp_ = dp;
    for(int j = 0; j <= K; ++j){
      for(int k = 0; k <= K; ++k){
        if(j+k > K) break;
        dp_[j+k] = max(dp_[j+k], dp[j] + DP[i][k]);
      }
    }
    dp = dp_;
  }
  cout << dp[K] << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3280
final_rank: 3