import std.stdio, std.string, std.conv, std.algorithm;
import std.typecons;
import std.array, std.range, std.container;
 
void main() {
	auto data = readln.split;
	auto N = data[0].to!int, K = data[1].to!int;
	Tuple!(int, int)[] sushi;
	foreach (i; 0 .. N) {
		data = readln.split;
		sushi ~= tuple(data[0].to!int, data[1].to!int);
	}
	sushi = sushi.sort!( (a,b) => a[1] > b[1] || (a[1]==b[1] && a[0] > b[0]) ).array;
	
	long result;
	foreach (number; 1 .. K+1) {
		long[] d_list = [];
		auto t_list = new RedBlackTree!(int);
		int count = 0;
		foreach (s; sushi) {
			if (d_list.length == K) break;
			if (count == number) { d_list ~= s[1]; }
			// new type of sushi
			else if (t_list.insert(s[0])) { count++; d_list ~= s[1]; }
			// old type of sushi
			else { continue; }
		}
		if (count < number) break;
		result = max(result, d_list.sum + number * number);
	}
	writeln(result);
}

