import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

void main() {
    int N, K;
    scanf("%d %d\n", &N, &K);
    int[][] X = new int[][10];
    foreach (i; 0 .. N) {
        int c, g;
        scanf("%d %d\n", &c, &g);
        g--;
        X[g] ~= c;
    }
    foreach (i; 0 .. 10) {
        X[i] = X[i].sort!"a > b".array;
    }
    int[][] Y = new int[][10];
    foreach (i; 0 .. 10) {
        Y[i] = new int[X[i].length + 1];
        Y[i][0] = 0;
        foreach (int j; 1 .. cast(int)X[i].length + 1) {
            Y[i][j] = Y[i][j - 1] + X[i][j - 1] + (j - 1) * 2;
        }
    }
    // X.writeln;
    // Y.writeln;
    int cache[10][2000]; memset(cast(void*)cache, -1, cache.sizeof);
    int dfs(int index, int r) {
        //[index, r].writeln;
        if (index == 10) return 0;
        if (cache[index][r] >= 0) return cache[index][r];
        int ret = 0;
        foreach (i; 0 .. min(r, X[index].length) + 1) {
            ret = max(ret, dfs(index + 1, r - i) + Y[index][i]);
        }
        return cache[index][r] = ret;
    }
    dfs(0, K).writeln;
}