#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)


struct tower
{
    int a, b;
};

bool operator < (const tower &t, const tower &t1)
{
    return (min(t.a, t.b) > min(t1.a, t1.b));
}

const int LEN = 100100;
int n, s, res;

tower ar[LEN], t;
set <tower> q;

int main()
{
    freopen("castle.in", "r", stdin);
    freopen("castle.out", "w", stdout);

    cin >> n >> s;
    for (int i = 0; i < n; i++)
    {
        cin >> ar[i].a >> ar[i].b;
        q.insert(ar[i]);

        res += ar[i].a;
    }

    while (s > 0 && res > 0)
    {
        t = *q.begin();
        q.erase(q.begin());

        if (t.a >= t.b)
        {
            res -= t.b;
            t.a -= t.b;
        }
        else
        {
            res -= t.a;
            t.a = 0;
        }

        s--;

        q.insert(t);
    }


    cout << res;
    return 0;
}
