#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int> > mati;
typedef vector<vector<double> > matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define rep(i,x,y) for(int i=(x); i<(y); ++i)

template <typename T>
void vec_print(vector<T> VEC){
  rep(i, 0, VEC.size()){
    cout << VEC[i] << " ";
  }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  rep(i, 0, MAT.size()){
    rep(j, 0, MAT[i].size()){
      cout << MAT[i][j] << " ";
    }
    cout << "\n";
  }
};

/*
template <typename CLASS1, typename CLASS2>
class HOGE{
  public:
    CLASS1 key;
    CLASS2 value;
    HOGE(ll key, ll value){
      this->key = index;
      this->value = value;
    };
    ~HOGE(void){
      return;
    };

    void print(void){
      cout << "key : " << key << ", value : " << value << "\n";
      return;
    };
    
    bool operator==(const HOGE &obj){
      return (this->value == obj.value);
    };
    bool operator<(const HOGE &obj){
      return (this->value < obj.value);
    };
    bool operator>(const HOGE &obj){
      return (this->value > obj.value);
    };
};
*/

template <typename CLASS1, typename CLASS2>
class HOGE{
  public:
    CLASS1 key;
    CLASS2 value;
    HOGE(void){
      this->key = 0;
      this->value = 0;
    };
    HOGE(ll key, ll value){
      this->key = index;
      this->value = value;
    };
    ~HOGE(void){
      return;
    };

    void print(void){
      cout << "key : " << key << ", value : " << value << "\n";
      return;
    };

    void operator=(const HOGE &obj){
      this->key = obj.key;
      this->value = obj.value;
      return;
    }
    
    bool operator==(const HOGE &obj){
      return (this->key == obj.key && this->value == obj.value);
    };
    bool operator<(const HOGE &obj){
      if(this->value == obj.value) return (this->key > obj.key);
      return (this->value > obj.value);
    };
    bool operator>(const HOGE &obj){
      if(this->value == obj.value) return (this->key < obj.key);
      return (this->value < obj.value);
    };
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N, K;
  cin >> N >> K;
  vll t(N), d(N);
  vector<HOGE<ll, ll>> hoge(N);
  rep(i, 0, N){
    cin >> t[i] >> d[i];
    hoge[i].key = t[i];
    hoge[i].value = d[i];
  }

  sort(hoge.begin(), hoge.end());

  unordered_set<ll> st;
  vector<HOGE<ll, ll>> cand(K);
  ll kind = 0;
  unordered_map<ll, int> mp;
  vector<ll> start(K, -1);

  ll cand_sum = 0;
  
  for(int i=0; i<K; ++i){
    cand[i].key = hoge[i].key;
    cand[i].value = hoge[i].value;
    cand_sum += cand[i].value;
    if(st.find(hoge[i].key)==st.end()){
      st.emplace(hoge[i].key);
      mp.emplace(hoge[i].key, kind);
      start[kind] = i;
      kind++;
    }
  }

  ll ans = cand_sum + kind*kind;

  int cand_end = K-1;
  while(start[mp[cand[cand_end].key]] == cand_end) cand_end--;
  for(int i=K; i<N && cand_end>=0 && kind<=K; ++i){
    if(st.find(hoge[i].key)==st.end()){
      cand_sum += hoge[i].value - cand[cand_end].value;
      cand[cand_end] = hoge[i];
      cand_end--;
      while(start[mp[cand[cand_end].key]] == cand_end) cand_end--;
      kind++;
      st.emplace(hoge[i].key);
      ans = max(ans, cand_sum + kind*kind);
    }
  }

  cout << ans << "\n";

  return 0;
}

Measured memory_bytes: 11416