#include <bits/stdc++.h>
using namespace std;
ifstream in("input.txt");
ofstream out("output.txt");
vector<pair<int, int>> mobs;
int n, k;
void input() {
  cin >> n >> k;
  mobs.resize(n);
  for (pair<int, int>& x : mobs) cin >> x.first >> x.second;
  sort(mobs.begin(), mobs.end());
}
vector<vector<pair<int, int>>> anss;
int best = 0;
void solve(vector<pair<int, int>>& cur) {
  vector<int> table;
  for (pair<int, int> x : cur) {
    int p = table.size();
    if (table.size() == k) {
      sort(table.begin(), table.end());
      p = 0;
    } else
      table.push_back(0);
    for (int i = 0; i < table.size(); i++) table[i] += x.second;
    table[p] = x.first;
  }
  int sum = 0;
  for (int x : table) sum += x;
  if (sum > best) {
    anss.clear();
    anss.push_back(cur);
    best = sum;
  } else if (sum == best)
    anss.push_back(cur);
}
void calc() {
  for (int l = k; l <= n;) {
    vector<pair<int, int>> cur;
    for (int i = 0; i < l; i++) cur.push_back(mobs[i]);
    solve(cur);
    if (!next_permutation(mobs.begin(), mobs.end())) l++;
  }
}
int main() {
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  ;
  input();
  calc();
  cout << best;
  return 0;
  cout << "Best value = " << best << '\n';
  for (vector<pair<int, int>>& x : anss) {
    for (pair<int, int> c : x) cout << c.first << '|' << c.second << "  ";
    cout << '\n';
  }
}
