// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long   signed int LL;

typedef long long unsigned int LU;



#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)

#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)

#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)

#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)

#define  inc(i, n) incID(i, 0, n)

#define inc1(i, n) incII(i, 1, n)

#define  dec(i, n) decID(i, 0, n)

#define dec1(i, n) decII(i, 1, n)



#define inII(v, l, r) ((l) <= (v) && (v) <= (r))

#define inID(v, l, r) ((l) <= (v) && (v) <  (r))



#define PB push_back

#define EB emplace_back

#define MP make_pair

#define FI first

#define SE second

#define PQ priority_queue



#define  ALL(v)  v.begin(),  v.end()

#define RALL(v) v.rbegin(), v.rend()

#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)

#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)



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

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

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

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

template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }

template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }



// ---- ----



const int M = 100000;

LL n, k;

vector<LL> v[M], A, B;



int main() {

	cin >> n >> k;

	inc(i, n) {

		LL t, d;

		cin >> t >> d;

		t--;

		v[t].PB(d);

	}

	

	inc(t, M) {

		sort(RALL(v[t]));

		inc(i, v[t].size()) { (i == 0 ? A : B).PB(v[t][i]); }

	}

	sort(RALL(A));

	sort(RALL(B));

	

	vector<LL> SA(A.size() + 1); inc(i, A.size()) { SA[i + 1] = SA[i] + A[i]; }

	vector<LL> SB(B.size() + 1); inc(i, B.size()) { SB[i + 1] = SB[i] + B[i]; }

	

	LL ans = 0;

	inc1(i, k) {

		if(i > A.size() || k - i > B.size()) { continue; }

		

		LL x = i;

		setmax(ans, SA[i] + SB[k - i] + x * x);

	}

	

	cout << ans << endl;

	

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef long long   signed int LL;

typedef long long unsigned int LU;



#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)

#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)

#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)

#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)

#define  inc(i, n) incID(i, 0, n)

#define inc1(i, n) incII(i, 1, n)

#define  dec(i, n) decID(i, 0, n)

#define dec1(i, n) decII(i, 1, n)



#define inII(v, l, r) ((l) <= (v) && (v) <= (r))

#define inID(v, l, r) ((l) <= (v) && (v) <  (r))



#define PB push_back

#define EB emplace_back

#define MP make_pair

#define FI first

#define SE second

#define PQ priority_queue



#define  ALL(v)  v.begin(),  v.end()

#define RALL(v) v.rbegin(), v.rend()

#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)

#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)



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

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

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

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

template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }

template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }



// ---- ----



int main() {

	LL n, k;

	cin >> n >> k;

	

	vector<pair<LL, LL>> v(n);

	inc(i, n) { cin >> v[i].FI >> v[i].SE; }

	sort(RALL(v));

	

	vector<LL> x, y;

	inc(i, v.size()) { (i == 0 || v[i].FI != v[i - 1].FI ? x : y).PB(v[i].SE); }

	sort(RALL(x));

	sort(RALL(y));

	

	vector<LL> sx(n + 1, -1e18), sy(n + 1);

	sx[0] = 0;

	inc(i, x.size()) { sx[i + 1] = sx[i] + x[i]; }

	inc(i, y.size()) { sy[i + 1] = sy[i] + y[i]; }

	

	LL ans = 0;

	incII(i, 0, k) { setmax(ans, sx[i] + sy[k - i] + 1LL * i * i); }

	

	cout << ans << endl;

	

	return 0;

}


// Fastest known solution for this problem: