#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e5+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
bool cmp(pll A,pll B){
    if(A.F==B.F){
        R (k[A.S]<k[B.S]);
    }
    R A.F<B.F;
}
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        a[i].F=l[i]-k[i];
        a[i].S=i;
    }
    ll ans=0;
    sort(a,a+n,cmp);
    reverse(a,a+n);
    for(int i=0;i<n;i++){
        if(f<=0||k[a[i].S]==0||l[a[i].S]==0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ll x=k[a[i].S]*2;
        ll y=l[a[i].S];
        ans+=(min(x,y));
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e6+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
bool cmp(pll A,pll B){
    if(A.F==B.F){
        R (k[A.S]<k[B.S]);
    }
    if(k[A.S]==1)R 1;
    if(k[B.S]==1)R 0;
    R A.F<B.F;
}
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        a[i].F=l[i]-k[i];
        a[i].S=i;
    }
    ll ans=0;
    sort(a,a+n,cmp);
    reverse(a,a+n);
    for(int i=0;i<n;i++){
        if(f<=0||k[a[i].S]==0||l[a[i].S]==0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ll x=k[a[i].S]*2;
        ll y=l[a[i].S];
        ans+=(min(x,y));
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e5+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
bool cmp(pll A,pll B){
    if(A.F==B.F){
        R (k[A.S]<k[B.S]);
    }
    if(k[A.S]==1)R 1;
    if(k[B.S]==1)R 0;
    R A.F<B.F;
}
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        a[i].F=l[i]-k[i];
        a[i].S=i;

    }
    ll ans=0;
    sort(a,a+n,cmp);
    for(int i=0;i<n/2;i++)swap(a[i],a[n-i-1]);
    for(int i=0;i<n;i++){
        if(f<=0||k[a[i].S]==0||l[a[i].S]==0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ll x=k[a[i].S]*2;
        ll y=l[a[i].S];
        ans+=(min(x,y));
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e6+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX],a,b,ans;
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>a>>b;
        k[i]=min(2*a,b)-min(a,b);
        ans+=min(a,b);
    }
    sort(k,k+n);
    reverse(k,k+n);
    for(int i=0;i<f;i++)ans+=k[i];
    cout<<ans<<endl;
}


#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
struct tt{
    long long clients, products, sau, hieu;
} a[100001], b[100001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
bool dau[100001];

int main()
{
    int n,f;
    int num = 0;
    memset(dau, false, 100001);
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        b[i].clients = a[i].clients;
        b[i].products = a[i].products;
        a[i].hieu = a[i].clients - a[i].products;
    }
    long long sum2 = 0;
    if (f == 0){
        for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum2 = sum2 + b[i].products;
                }
                else {
                    sum2 = sum2 + b[i].clients;
                }
        }
        }
        cout<<sum2;
    }
    if ( f!= 0){
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                num++;
            }
            if (a[i].clients < 2*a[i].products){
                a[num].sau = a[i].clients - a[i].products;
                num++;
            }
        }
    }
    sort(a, a+num, cmp1);
    if (f >= num ) f = num;
    long long sum1 = 0;
    for (int i = 0; i < f; i++){
        sum1 = sum1 +a[i].sau;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum = sum + b[i].products;
                }
                else {
                    sum = sum + b[i].clients;
                }
        }
    }

    cout<<sum + sum1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;
struct day {
    ll k, l, s;
    bool operator<(const day &b) const {return s > b.s;}
} a[100005];
int n, f = 0;
ll r = 0;
queue<int>s;
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> f;
    int i = 0;
    for (i = 0; i < n; i++) {
        cin >> a[i].k >> a[i].l;
        a[i].s = min(a[i].l, a[i].k<<1) - min(a[i].l, a[i].k);
    }
    sort(a, a + n);
    for (i = 0; i < f; i++) r+=min(a[i].l, a[i].k<<1);
    for (; i < n; i++) r+=min(a[i].l, a[i].k);
    cout << r;
    return 0;

}


#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
struct tt{
    long long clients, products, sau, hieu;
}   a[200001], b[200001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
int main()
{
    int n,f;
    int num = 0;
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        b[i].clients = a[i].clients;
        b[i].products = a[i].products;
        a[i].hieu = a[i].clients - a[i].products;
    }
    long long sum2 = 0;
    if (f == 0){
        for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum2 = sum2 + b[i].products;
                }
                else {
                    sum2 = sum2 + b[i].clients;
                }
        }
        }
        cout<<sum2;
    }
    if ( f!= 0){
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                num++;
            }
            if (a[i].clients < 2*a[i].products){
                a[num].sau = a[i].clients - a[i].products;
                num++;
            }
        }
    }
    sort(a, a + num, cmp1);
    if (f >= num ) f = num;
    long long sum1 = 0;
    for (int i = 0; i < f; i++){
        sum1 = sum1 +a[i].sau;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum = sum + b[i].products;
                }
                else {
                    sum = sum + b[i].clients;
                }
        }
    }
    cout<<sum + sum1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
struct tt{
    long long clients, products, sau, hieu;
} a[100001], b[100001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
bool dau[100001];

int main()
{
    int n,f;
    int num = 0;
    memset(dau, false, 100001);
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        b[i].clients = a[i].clients;
        b[i].products = a[i].products;
        a[i].hieu = a[i].clients - a[i].products;
    }
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                num++;
            }
            if (a[i].clients < 2*a[i].products){
                a[num].sau = a[i].clients - a[i].products;
                num++;
            }
        }
    }
    sort(a, a+num, cmp1);
    if (f >= num ) f = num;
    long long sum1 = 0;
    for (int i = 0; i < f; i++){
        sum1 = sum1 +a[i].sau;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum = sum + b[i].products;
                }
                else {
                    sum = sum + b[i].clients;
                }
        }
    }
    cout<<sum + sum1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
struct tt{
    long long clients, products, sau, hieu;
}   a[500001], b[500001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
int main()
{
    int n,f;
    int num = 0;
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        b[i].clients = a[i].clients;
        b[i].products = a[i].products;
        a[i].hieu = a[i].clients - a[i].products;
    }
    long long sum2 = 0;
    if (f == 0){
        for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum2 = sum2 + b[i].products;
                }
                else {
                    sum2 = sum2 + b[i].clients;
                }
        }
        }
        cout<<sum2;
    }
    if ( f!= 0){
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                num++;
            }
            if (a[i].clients < 2*a[i].products){
                a[num].sau = a[i].clients - a[i].products;
                num++;
            }
        }
    }
    sort(a, a + num, cmp1);
    if (f >= num ) f = num;
    long long sum1 = 0;
    for (int i = 0; i < f; i++){
        sum1 = sum1 +a[i].sau;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum = sum + b[i].products;
                }
                else {
                    sum = sum + b[i].clients;
                }
        }
    }
    cout<<sum + sum1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
struct tt{
    long long clients, products, sau, hieu;
}   a[500001], b[500001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
int main()
{
    int n,f;
    int num = 0;
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        b[i].clients = a[i].clients;
        b[i].products = a[i].products;
        a[i].hieu = a[i].clients - a[i].products;
    }
    long long sum2 = 0;
    if (f == 0){
        for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum2 = sum2 + b[i].products;
                }
                else {
                    sum2 = sum2 + b[i].clients;
                }
        }
        }
        cout<<sum2;
        return 0;
    }
    if ( f!= 0){
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                num++;
            }
            if (a[i].clients < 2*a[i].products){
                a[num].sau = a[i].clients - a[i].products;
                num++;
            }
        }
    }
    sort(a, a + num, cmp1);
    if (f >= num ) f = num;
    long long sum1 = 0;
    for (int i = 0; i < f; i++){
        sum1 = sum1 +a[i].sau;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum = sum + b[i].products;
                }
                else {
                    sum = sum + b[i].clients;
                }
        }
    }
    cout<<sum + sum1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
struct tt{
    long long clients, products, sau, hieu, pos, posau;
} a[100001], b[100001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
bool dau[100001];

int main()
{
    int n,f;
    int num = 0;
    memset(dau, false, 100001);
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        b[i].clients = a[i].clients;
        b[i].products = a[i].products;
        a[i].hieu = a[i].clients - a[i].products;
        a[i].pos = i;
    }
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                a[num].posau = a[i].pos;
                num++;
            }
            if (a[i].clients < 2*a[i].products){
                a[num].sau = a[i].clients - a[i].products;
                a[num].posau = a[i].pos;
                num++;
            }
        }
    }
    sort(a, a+num, cmp1);
    if (f >= num ) f = num;
    long long sum1 = 0;
    for (int i = 0; i < f; i++){
        sum1 = sum1 +a[i].sau;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++){
        if  ((b[i].clients != 0) || (b[i].products != 0)){
                if (b[i].products <= b[i].clients){
                    sum = sum + b[i].products;
                }
                else {
                    sum = sum + b[i].clients;
                }
        }
    }
    cout<<sum + sum1;
    return 0;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
struct tt{
    long long clients, products, sau, hieu;
}   a[100001], b[100001];
bool cmp1(tt const i, tt const j){
    return i.sau >= j.sau;
}
int main()
{
    int n,f, c = 0;
    int num = 0;
    long long sum = 0;
    cin>>n>>f;
    for (int i = 0; i < n; i++) {
        cin>>a[i].products>>a[i].clients;
        a[i].hieu = a[i].clients - a[i].products;
    }
    if (f == 0){
        for (int i = 0; i < n; i++) sum+=min(a[i].clients, a[i].products);
        cout<<sum;
        return 0;
    }
    if ( f!= 0){
    for (int i = 0; i < n; i++){
        if ((a[i].hieu > 0) && (a[i].products > 0) ){
            if (a[i].clients >= (2*a[i].products)){
                a[num].sau = a[i].products;
                a[num].products = a[i].products;
            }
            else{
                a[num].sau = a[i].clients - a[i].products;
                a[num].products = a[i].products;
            } num++;
        } else b[c++] = a[i];
    }
    sort(a, a + num, cmp1);
    if (f >= num ) f = num;
    for (int i = 0; i < f; i++){
        sum+=a[i].products + a[i].sau;
    }
    for (int i = f; i < num; i++) sum+=min(a[i].clients, a[i].products);
    for (int i = 0; i < c; i++) sum+=min(b[i].clients, b[i].products);
    cout<<sum;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long v[100005];
int main()
{
    int n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else if(x<y)
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    if(k>0)
    {sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];}
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long v[100005];
int main()
{
    long long n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else if(x<y)
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    if(k>0)
    {sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];}
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int v[100005];
int main()
{
    int n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else if(x<y)
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    if(f>0)
    {sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];}
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int v[100005];
int main()
{
    int n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int v[100005];
int main()
{
    int n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long v[100005];
int main()
{
    long long n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else if(x<y)
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    if(k>0)
    {sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];}
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int v[100005];
int main()
{
    int n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else if(x<y&&k>0)
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

long long v[100005];

int main()
{
    long long n,k,i,x,y,s=0;
    cin>>n>>k;
    for(i=0; i<n; i++)
    {
        cin>>x>>y;
        s+=min(x,y);
        v[i]= min (2*x,y)-min(x,y);
    }
    sort(v,v+n);
    for(i=n-k;i<n;i++)
        s+=v[i];
    cout<<s;
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int v[100005];
int main()
{
    int n,k,i,x,y,s=0,u=1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
       cin>>x>>y;
       if(x>=y)
          s+=y;
       else if(x<y)
       {
          if(2*x>=y)
          {
             v[u]=y-x;
             u++;
             s+=x;
          }
          else if(2*x<y)
          {
             v[u]=2*x-x;
             u++;
             s+=x;
          }
       }
    }
    u--;
    if(k>0)
    {sort(v+1,v+u+1);
    for(i=u;i>=u-k+1;i--)
        s+=v[i];}
    cout<<s;
    return 0;
}


 #include <bits/stdc++.h>

 using namespace std;

 int main()
{
    int n,f;
    cin >> n >> f;
    unsigned long long outp(0);
    vector<pair<long long,int>  > v(0);
    for(int i=0;i<n;i++)
    {
        long long k,l;
        cin >> k >> l;
        if(k>=l) outp+=l;
        else
        {
            v.push_back(make_pair(min(2*k,l),k));
        }
    }
    sort(v.rbegin(),v.rend());
    for(int i=0;i<f && i<v.size();i++) outp+=v[i].first;
    for(int i=f;i<v.size();i++) outp+=v[i].second;
    cout << outp << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,f;
    cin >> n >> f;
    vector<pair<int,int> > v(0);
    long long outp(0);
    int l[n],k[n];
    for(int i=0;i<n;i++)
    {
        cin >> k[i] >> l[i];
        if(k[i]<l[i]) v.push_back(make_pair(min(2*k[i],l[i]),i));
    }
    sort(v.rbegin(),v.rend());
    int d[n]={0};
    for(int i=0;i<f && i<v.size();i++)
    {
        outp+=v[i].first;
        d[v[i].second]=1;
    }
    for(int i=0;i<n;i++)
    {
        if(!d[i]) outp+=min(l[i],k[i]);
    }
    cout << outp << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,f;
    cin >> n >> f;
    unsigned long long outp(0);
    vector<long long> v(0);
    for(int i=0;i<n;i++)
    {
        long long k,l;
        cin >> k >> l;
        if(k>l) outp+=l;
        else
        {
            v.push_back(min(2*k,l));
        }
    }
    sort(v.rbegin(),v.rend());
    for(int i=0;i<f;i++) outp+=v[i];
    for(int i=f;i<v.size();i++) outp+=v[i]/2;
    cout << outp << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,f;
    cin >> n >> f;
    unsigned long long outp(0);
    vector<pair<long long,int> > v(0);
    long long k[n],l[n];
    for(int i=0;i<n;i++)
    {
        cin >> k[i] >> l[i];
        if(k[i]>l[i]) outp+=l[i];
        else
        {
            v.push_back(make_pair(min(2*k[i],l[i]),i));
        }
    }
    sort(v.rbegin(),v.rend());
    for(int i=0;i<f;i++) outp+=v[i].first;
    for(int i=f;i<v.size();i++) outp+=min(k[v[i].second],l[v[i].second]);
    cout << outp << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,f;
    cin >> n >> f;
    vector<pair<int,int> > v(0);
    unsigned long long outp(0);
    int l[n],k[n];
    for(int i=0;i<n;i++)
    {
        cin >> k[i] >> l[i];
        if(k[i]<l[i]) v.push_back(make_pair(min(2*k[i],l[i]),i));
    }
    sort(v.rbegin(),v.rend());
    int d[n]={0};
    for(int i=0;i<f && i<v.size();i++)
    {
        outp+=v[i].first;
        d[v[i].second]=1;
    }
    for(int i=0;i<n;i++)
    {
        if(!d[i]) outp+=min(l[i],k[i]);
    }
    cout << outp << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    long long n,f;
    cin >> n >> f;
    vector<pair<long long,long long> > v(0);
    unsigned long long outp(0);
    long long l[n],k[n];
    for(int i=0;i<n;i++)
    {
        cin >> k[i] >> l[i];
        if(k[i]<l[i]) v.push_back(make_pair(min(2*k[i],l[i]),i));
    }
    sort(v.rbegin(),v.rend());
    int d[n]={0};
    for(int i=0;i<f && i<v.size();i++)
    {
        outp+=v[i].first;
        d[v[i].second]=1;
    }
    for(int i=0;i<n;i++)
    {
        if(!d[i]) outp+=min(l[i],k[i]);
    }
    cout << outp << endl;
    return 0;
}


 #include <bits/stdc++.h>

 using namespace std;

 int main()
{
    int n,f;
    cin >> n >> f;
    unsigned long long outp(0);
    vector<pair<long long,int>  > v(0);
    for(int i=0;i<n;i++)
    {
        long long k,l;
        cin >> k >> l;
        if(k>=l) outp+=l;
        else
        {
            v.push_back(make_pair(min(2*k,l),k));
        }
    }
    sort(v.rbegin(),v.rend());
    for(int i=0;i<f;i++) outp+=v[i].first;
    for(int i=f;i<v.size();i++) outp+=v[i].second;
    cout << outp << endl;
}


 #include <bits/stdc++.h>

 using namespace std;

 int main()
{
    int n,f;
    cin >> n >> f;
    unsigned long long outp(0);
    vector<pair<long long,int>  > v(0);

    for(int i=0;i<n;i++)
    {
        long long k,l;
        cin >> k >> l;
        if(k>=l) outp+=l;
        else
        {
            v.push_back(make_pair(min(2*k,l)-k,k));
        }
    }
    sort(v.rbegin(),v.rend());
    for(int i=0;i<f && i<v.size();i++) outp+=v[i].first+v[i].second;
    for(int i=f;i<v.size();i++) outp+=v[i].second;
    cout << outp << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
int a[maxn],k[maxn],l[maxn],b[maxn];
int cmp(int a,int b)
{
    return a > b;
}
int main()
{
    int n,f;
    scanf("%d%d",&n,&f);
    long long sum = 0;
    for(int i = 0;i < n;i++){
        scanf("%d%d",&k[i],&l[i]);
        a[i] = min(k[i],l[i]);
        sum += a[i];
        b[i] = min(2 * k[i],l[i]) - a[i];
    }
    sort(b,b + n,cmp);
    for(int i = 0;i < f;i++){
        sum += b[i];
    }
    printf("%d\n",sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
    int k,l;
    int re;
}str[maxn];
int cmp(node a,node b)
{
    return a.l > b.l;
}
int cmp1(node a,node b)
{
    return a.k > b.k;
}
int cmp2(node a,node b)
{
    return a.re > b.re;
}
int main()
{
    int n,f;
    int k[maxn];
    int k2[maxn];
    int l[maxn];
    scanf("%d%d",&n,&f);
    for(int i = 0;i < n;i++){
        scanf("%d%d",&str[i].k,&str[i].l);
        str[i].k *= 2;
        str[i].re = min(str[i].k,str[i].l);
    }
    sort(str,str + n,cmp2);
    int cnt = 0;
    for(int i = 0;i < n;i++){
        str[i].k /= 2;
        if(cnt < f){
            str[i].k *= 2;
        }

        cnt ++;
    }
    //for(int i = 0;i < n;i++)
       // cout << str[i].k << ' ' << str[i].l << endl;
   /* int cnt = 0;
    for(int i = 0;i < n;i++){
        if(cnt == f)
            break;
        if(str[i].k < str[i].l && str[i].k){
            cnt ++;
            str[i].k *= 2;
       // cout << str[i].k << endl;
        }
    }*/
    sort(str,str + n,cmp1);
    long long  sum = 0;
    for(int i = 0;i < n;i++)
        sum += min(str[i].k,str[i].l);
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
    int k,l;
    int re;
}str[maxn];
int cmp(node a,node b)
{
    return a.l > b.l;
}
int cmp1(node a,node b)
{
    return a.k > b.k;
}
int cmp2(node a,node b)
{
    return a.re > b.re;
}
int main()
{
    int n,f;
    int k[maxn];
    int k2[maxn];
    int l[maxn];
    scanf("%d%d",&n,&f);
    for(int i = 0;i < n;i++){
        scanf("%d%d",&str[i].k,&str[i].l);
        str[i].k *= 2;
        str[i].re = min(str[i].k,str[i].l);
    }
    sort(str,str + n,cmp2);
    int cnt = 0;
    for(int i = 0;i < n;i++){
        str[i].k /= 2;
        if(cnt < f){
            str[i].k *= 2;
        }

        cnt ++;
    }
    //for(int i = 0;i < n;i++)
       // cout << str[i].k << ' ' << str[i].l << endl;
   /* int cnt = 0;
    for(int i = 0;i < n;i++){
        if(cnt == f)
            break;
        if(str[i].k < str[i].l && str[i].k){
            cnt ++;
            str[i].k *= 2;
       // cout << str[i].k << endl;
        }
    }*/
    sort(str,str + n,cmp1);
    int sum = 0;
    for(int i = 0;i < n;i++)
        sum += min(str[i].k,str[i].l);
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
    int k,l;
}str[maxn];
int cmp(node a,node b)
{
    return a.l > b.l;
}
int cmp1(node a,node b)
{
    return a.k > b.k;
}
int main()
{
    int n,f;
    int k[maxn];
    int k2[maxn];
    int l[maxn];
    scanf("%d%d",&n,&f);
    for(int i = 0;i < n;i++){
        scanf("%d%d",&str[i].k,&str[i].l);
    }
    sort(str,str + n,cmp);
    //for(int i = 0;i < n;i++)
        //cout << str[i].k << ' ' << str[i].l << endl;
    int cnt = 0;
    for(int i = 0;i < n;i++){
        if(cnt == f)
            break;
        if(str[i].k < str[i].l && str[i].k){
            cnt ++;
            str[i].k *= 2;
       // cout << str[i].k << endl;
        }

    }
    sort(str,str + n,cmp1);
    int sum = 0;
    for(int i = 0;i < n;i++)
        sum += min(str[i].k,str[i].l);
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
int a[maxn],k[maxn],l[maxn],b[maxn];
int cmp(int a,int b)
{
    return a > b;
}
int main()
{
    int n,f;
    scanf("%d%d",&n,&f);
    long long sum = 0;
    for(int i = 0;i < n;i++){
        scanf("%d%d",&k[i],&l[i]);
        a[i] = min(k[i],l[i]);
        sum += a[i];
        b[i] = min(2 * k[i],l[i]) - a[i];
    }
    sort(b,b + n,cmp);
    for(int i = 0;i < f;i++){
        sum += b[i];
    }
    printf("%lf\n",sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
int a[maxn],k[maxn],l[maxn],b[maxn];
int cmp(int a,int b)
{
    return a > b;
}
int main()
{
    int n,f;
    scanf("%d%d",&n,&f);
    long long sum = 0;
    for(int i = 0;i < n;i++){
        scanf("%d%d",&k[i],&l[i]);
        a[i] = min(k[i],l[i]);
        sum += a[i];
        b[i] = min(2 * k[i],l[i]) - a[i];
    }
    sort(b,b + n,cmp);
    for(int i = 0;i < f;i++){
        sum += b[i];
    }
    printf("%lld\n",sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
    int k,l;
}str[maxn];
int cmp(node a,node b)
{
    return a.l > b.l;
}
int cmp1(node a,node b)
{
    return a.k > b.k;
}
int main()
{
    int n,f;
    int k[maxn];
    int k2[maxn];
    int l[maxn];
    scanf("%d%d",&n,&f);
    for(int i = 0;i < n;i++){
        scanf("%d%d",&str[i].k,&str[i].l);
    }
    sort(str,str + n,cmp);
    //for(int i = 0;i < n;i++)
        //cout << str[i].k << ' ' << str[i].l << endl;
    int cnt = 0;
    for(int i = 0;i < n;i++){
        if(str[i].k < str[i].l && str[i].k){
            cnt ++;
            str[i].k *= 2;
          //  cout << str[i].k << endl;
        }
        if(cnt == f)
            break;
    }
    sort(str,str + n,cmp1);
    int sum = 0;
    for(int i = 0;i < n;i++)
        sum += min(str[i].k,str[i].l);
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
    int k,l;
    int re;
}str[maxn];
int cmp(node a,node b)
{
    return a.l > b.l;
}
int cmp1(node a,node b)
{
    return a.k > b.k;
}
int cmp2(node a,node b)
{
    return a.re > b.re;
}
int main()
{
    int n,f;
    int k[maxn];
    int k2[maxn];
    int l[maxn];
    scanf("%d%d",&n,&f);
    for(int i = 0;i < n;i++){
        scanf("%d%d",&str[i].k,&str[i].l);
        str[i].k *= 2;
        str[i].re = min(str[i].k,str[i].l);
    }

    sort(str,str + n,cmp2);
    int cnt = 0;
    for(int i = 0;i < n;i++){
        str[i].k /= 2;
        if(cnt < f && str[i].k < str[i].l){
            str[i].k *= 2;
            cnt ++;
        }

    }
    //for(int i = 0;i < n;i++)
       // cout << str[i].k << ' ' << str[i].l << endl;
   /* int cnt = 0;
    for(int i = 0;i < n;i++){
        if(cnt == f)
            break;
        if(str[i].k < str[i].l && str[i].k){
            cnt ++;
            str[i].k *= 2;
       // cout << str[i].k << endl;
        }
    }*/
    sort(str,str + n,cmp1);
    long long  sum = 0;
    for(int i = 0;i < n;i++)
        sum += min(str[i].k,str[i].l);
    cout << sum << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
    int k,l;
}str[maxn];
int cmp(node a,node b)
{
    return a.l > b.l;
}
int main()
{
    int n,f;
    int k[maxn];
    int k2[maxn];
    int l[maxn];
    scanf("%d%d",&n,&f);
    for(int i = 0;i < n;i++){
        scanf("%d%d",&str[i].k,&str[i].l);
    }
    sort(str,str + n,cmp);
    //for(int i = 0;i < n;i++)
        //cout << str[i].k << ' ' << str[i].l << endl;
    int cnt = 0;
    for(int i = 0;i < n;i++){
        if(str[i].k < str[i].l && str[i].k){
            cnt ++;
            str[i].k *= 2;
          //  cout << str[i].k << endl;
        }
        if(cnt == f)
            break;
    }
    int sum = 0;
    for(int i = 0;i < n;i++)
        sum += min(str[i].k,str[i].l);
    cout << sum << endl;
    return 0;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0

using namespace std;

const ll INF = 1000000000;
int N, f, ans;
vector <int> k, l;
vector <pair <int, int> > d;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> f;
	k.resize(N);
	l.resize(N);
	d.resize(N);
	for (int i = 0; i < N; i ++)
	{
		cin >> k[i] >> l[i];
		d[i].fi = min(k[i] * 2, l[i]);
		d[i].se = i;
	}
	sort(d.begin(), d.end());
	for (int i = 0; i < f && !d.empty(); i ++)
	{
		int v = d[d.size() - 1].se;
		k[v] *= 2;
		d.pop_back();
	}
//	for (int i = 0; i < N; i ++)
//		cout << k[i] << " " << l[i] << '\n';
	for (int i = 0; i < N; i ++)
		ans += min(k[i], l[i]);
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0

using namespace std;

const ll INF = 1000000000;
int N, f;
long long ans;
vector <int> k, l;
vector <pair <int, int> > d;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> f;
	k.resize(N);
	l.resize(N);
	d.resize(N);
	for (int i = 0; i < N; i ++)
	{
		cin >> k[i] >> l[i];
		d[i].fi = min(k[i] * 2, l[i]);
		d[i].se = i;
	}
	sort(d.begin(), d.end());
	for (int i = N - f; i < N; i ++)
	{
		int v = d[i].se;
		k[v] *= 2;
	}
//	for (int i = 0; i < N; i ++)
//		cout << k[i] << " " << l[i] << '\n';
	for (int i = 0; i < N; i ++)
		ans += min(k[i], l[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0

using namespace std;

const ll INF = 1000000000;
int N, f;
long long ans;
vector <int> k, l;
vector <pair <int, int> > d;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> f;
	k.resize(N);
	l.resize(N);
	d.resize(N);
	for (int i = 0; i < N; i ++)
	{
		cin >> k[i] >> l[i];
		d[i].fi = k[i] * 2 - l[i];
		d[i].se = i;
	}
	sort(d.begin(), d.end());
	for (int i = N - f; i < N; i ++)
	{
		int v = d[i].se;
		k[v] *= 2;
	}
//	for (int i = 0; i < N; i ++)
//		cout << k[i] << " " << l[i] << '\n';
	for (int i = 0; i < N; i ++)
		ans += min(k[i], l[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0
#define pb push_back
#define ull unsigned long long

using namespace std;

const ll INF = 1000000000;
int N, F;
ll ans;
vector <int> l, k;
vector <pair <ll, int> > a;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> F;
	l.resize(N);
	k.resize(N);
	for (int i = 0; i < N; i ++)
		cin >> k[i] >> l[i];
	for (int i = 0; i < N; i ++)
		if (l[i] != 0 && k[i] != 0)
			a.pb(mp(min(l[i], k[i]) - min(l[i], k[i] * 2), i));
	sort(a.begin(), a.end());
/*	for (int i = 0; i < a.size(); i ++)
		cout << a[i].fi << '\t' << a[i].se << '\n';
	cout << '\n';*/
	for (int i = 0; i < F; i ++)
		k[a[i].se] *= 2;
	for (int i = 0; i < N; i ++)
		ans += min(l[i], k[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0
#define pb push_back
#define ull unsigned long long

using namespace std;

const ll INF = 1000000000;
int N, F;
ll ans;
vector <int> l, k;
vector <pair <int, int> > a;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> F;
	l.resize(N);
	k.resize(N);
	for (int i = 0; i < N; i ++)
		cin >> k[i] >> l[i];
	for (int i = 0; i < N; i ++)
		if (l[i] != 0 && k[i] != 0)
			a.pb(mp(min(l[i], k[i]) - min(l[i], k[i] * 2), i));
	sort(a.begin(), a.end());
/*	for (int i = 0; i < a.size(); i ++)
		cout << a[i].fi << '\t' << a[i].se << '\n';
	cout << '\n';*/
	for (int i = 0; i < F; i ++)
		k[a[i].se] *= 2;
	for (int i = 0; i < N; i ++)
		ans += min(l[i], k[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0

using namespace std;

const ll INF = 1000000000;
int N, f;
long long ans;
vector <int> k, l;
vector <pair <int, int> > d;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> f;
	k.resize(N);
	l.resize(N);
	d.resize(N);
	for (int i = 0; i < N; i ++)
	{
		cin >> k[i] >> l[i];
		d[i].fi = l[i] - k[i];
		d[i].se = i;
	}
	sort(d.begin(), d.end());
	for (int i = 0; i < f && !d.empty(); i ++)
	{
		int v = d[d.size() - 1].se;
		if (k[v] != 0)
		{
			k[v] *= 2;
			i ++;
		}
		d.pop_back();
	}
//	for (int i = 0; i < N; i ++)
//		cout << k[i] << " " << l[i] << '\n';
	for (int i = 0; i < N; i ++)
		ans += min(k[i], l[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0

using namespace std;

const ll INF = 1000000000;
int N, f;
long long ans;
vector <int> k, l;
vector <pair <int, int> > d;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> f;
	k.resize(N);
	l.resize(N);
	d.resize(N);
	for (int i = 0; i < N; i ++)
	{
		cin >> k[i] >> l[i];
		d[i].fi = l[i] - k[i];
		d[i].se = i;
	}
	sort(d.begin(), d.end());
	for (int i = 0; i < f && !d.empty(); i ++)
	{
		int v = d[d.size() - 1].se;
		if (k[v] != 0)
		{
			k[v] *= 2;
			i ++;
		}
		d.pop_back();
	}
	for (int i = 0; i < N; i ++)
		cout << k[i] << " " << l[i] << '\n';
	for (int i = 0; i < N; i ++)
		ans += min(k[i], l[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0
#define pb push_back
#define ull unsigned long long

using namespace std;

const ll INF = 1000000000;
int N, F;
ll ans;
vector <int> l, k;
vector <pair <ll, int> > a;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> F;
	l.resize(N);
	k.resize(N);
	for (int i = 0; i < N; i ++)
		cin >> k[i] >> l[i];
	for (int i = 0; i < N; i ++)
		if (l[i] != 0 && k[i] != 0 && l[i] > k[i])
			a.pb(mp(min(l[i], k[i]) - min(l[i], k[i] * 2), i));
	sort(a.begin(), a.end());
/*	for (int i = 0; i < a.size(); i ++)
		cout << a[i].fi << '\t' << a[i].se << '\n';
	cout << '\n';*/
	for (int i = 0; i < F; i ++)
		k[a[i].se] *= 2;
	for (int i = 0; i < N; i ++)
		ans += min(l[i], k[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0

using namespace std;

const ll INF = 1000000000;
int N, f;
long long ans;
vector <int> k, l;
vector <pair <int, int> > d;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> f;
	k.resize(N);
	l.resize(N);
	d.resize(N);
	for (int i = 0; i < N; i ++)
	{
		cin >> k[i] >> l[i];
		d[i].fi = min(k[i] * 2, l[i]);
		d[i].se = i;
	}
	sort(d.begin(), d.end());
	for (int i = 0; i < f; i ++)
	{
		int v = d[d.size() - 1].se;
		k[v] *= 2;
		d.pop_back();
	}
//	for (int i = 0; i < N; i ++)
//		cout << k[i] << " " << l[i] << '\n';
	for (int i = 0; i < N; i ++)
		ans += min(k[i], l[i]) * 1LL;
	cout << ans;
}


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define fi first
#define se second
#define mp make_pair
#define ll long long
#define al printf("!");
#define r0 return 0
#define pb push_back
#define ull unsigned long long

using namespace std;

const ll INF = 1000000000;
int N, F;
ll ans;
vector <int> l, k;
vector <pair <int, int> > a;

int main()
{
//	freopen("a.in", "r", stdin);
//	freopen("a.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N >> F;
	l.resize(N);
	k.resize(N);
	for (int i = 0; i < N; i ++)
		cin >> k[i] >> l[i];
	for (int i = 0; i < N; i ++)
		a.pb(mp(min(l[i], k[i]) - min(l[i], k[i] * 2), i));
	sort(a.begin(), a.end());
/*	for (int i = 0; i < a.size(); i ++)
		cout << a[i].fi << '\t' << a[i].se << '\n';
	cout << '\n';*/
	for (int i = 0; i < F; i ++)
		k[a[i].se] *= 2;
	for (int i = 0; i < N; i ++)
		ans += min(l[i], k[i]) * 1LL;
	cout << ans;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int X, int Y)
{
	return X > Y; // descending order
}

long long n,f,sells;
int profit[100000]
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int i=0;i<n;++i)
    {

     if (2*k[i]<=l[i])
        profit[i]=k[i];
        else if (k[i]<=l[i])
        profit[i]=l[i]-k[i];
    }
    sort(profit + 0, profit + n, cmp);
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    if (i<f)
        sells+=profit[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n],maxi;
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    maxi=k[0]+l[0];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]+l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]+l[i];
        j=i;
    }

        }
    if (2*k[j]<=l[j])
    sells+=(2*k[j]);
    else
    sells+=(2*k[j])-((2*k[j])-l[j]);
    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
    sells+=(k[i])-((k[i])-l[i]);
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

int n,f,j,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    int k[n],l[n],maxi;
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    maxi=k[0]+l[0];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]+l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]+l[i];
        j=i;
    }

        }
    if (2*k[j]<=l[j])
    sells+=(2*k[j]);
    else
    sells+=(2*k[j])-((2*k[j])-l[j]);
    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
    sells+=(k[i])-((k[i])-l[i]);
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int X, int Y)
{
	return X > Y; // descending order
}

long long n,f,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n],profit[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int i=0;i<n;++i)
    {

     if (2*k[i]<=l[i])
        profit[i]=k[i];
        else if (k[i]<=l[i])
        profit[i]=l[i]-k[i];
        else
        profit[i]=l[i];
    }
    sort(profit + 0, profit + n, cmp);
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    if (i<f && k[i]<=l[i])
        sells+=profit[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells,maxi;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    int k[20],l[20];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]*l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]*l[i];
        j=i;
    }

        }
    if ((2*k[j])<=l[j])
    sells+=(2*k[j]);
    else
    sells+=l[j];

    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells,maxi;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]*l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]*l[i];
        j=i;
    }

        }
    if ((2*k[j])<=l[j])
    sells+=(2*k[j]);
    else
    sells+=l[j];

    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int X, int Y)
{
	return X > Y; // descending order
}

long long n,f,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n],profit[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int i=0;i<n;++i)
    {

    if (2*k[i]<=l[i])
        profit[i]=k[i];
        else
        profit[i]=l[i]-k[i];
    }
    sort(profit + 0, profit + n, cmp);
    /*for (int i=0;i<n;++i)
    cout << profit[i] <<endl;*/
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    if (i<f)
        sells+=profit[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    int k[n],l[n],maxi=0;
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]+l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]+l[i];
        j=i;
    }

        }
    if ((2*k[j])<=l[j])
    sells+=(2*k[j]);
    else
    sells+=l[j];
    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int X, int Y)
{
	return X > Y; // descending order
}

long long n,f,sells;
int profit[100000];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int i=0;i<n;++i)
    {

     if (2*k[i]<=l[i])
        profit[i]=k[i];
        else if (k[i]<=l[i])
        profit[i]=l[i]-k[i];
    }
    sort(profit + 0, profit + n, cmp);
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    if (i<f)
        sells+=profit[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    int k[n],l[n],maxi;
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    maxi=k[0]+l[0];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]+l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]+l[i];
        j=i;
    }

        }
    if (2*k[j]<=l[j])
    sells+=(2*k[j]);
    else
    sells+=(2*k[j])-((2*k[j])-l[j]);
    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
    sells+=(k[i])-((k[i])-l[i]);
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells,maxi;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    int k[n],l[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]*l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]*l[i];
        j=i;
    }

        }
    if ((2*k[j])<=l[j])
    sells+=(2*k[j]);
    else
    sells+=l[j];

    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>

using namespace std;

long long n,f,j,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    int k[n],l[n],maxi;
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    maxi=k[0]+l[0];
    for (int v=0;v<f;++v)
    {
        for (int i=0;i<n;++i)
        {
    if (maxi<k[i]+l[i] && k[i]!=0 && l[i]!=0)
    {
        maxi=k[i]+l[i];
        j=i;
    }

        }
    if ((2*k[j])<=l[j])
    sells+=(2*k[j]);
    else
    sells+=l[j];
    k[j]=0;
    l[j]=0;
    maxi=0;
    }
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int X, int Y)
{
	return X > Y; // descending order
}

long long n,f,sells;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n],profit[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int i=0;i<n;++i)
    {

     if (2*k[i]<=l[i])
        profit[i]=k[i];
        else if (k[i]<=l[i])
        profit[i]=l[i]-k[i];
        else
        profit[i]=l[i];
    }
    sort(profit + 0, profit + n, cmp);
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    if (i<f)
        sells+=profit[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cmp(int X, int Y)
{
	return X > Y; // descending order
}

long long n,f,sells,profit[100000000];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> f;
    long long k[n],l[n];
    for (int i=0;i<n;++i)
        cin >> k[i] >> l[i];
    for (int i=0;i<n;++i)
    {

     if (2*k[i]<=l[i])
        profit[i]=k[i];
        else if (k[i]<=l[i])
        profit[i]=l[i]-k[i];
    }
    sort(profit + 0, profit + n, cmp);
    for (int i=0;i<n;++i)
    {
    if (k[i]<=l[i])
        sells+=k[i];
    else
        sells+=l[i];
    if (i<f)
        sells+=profit[i];
    }
   cout << sells <<endl;
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0,diff;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           diff=y-x;
           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       sll i=0,ans=0;
       while( i<n)
       {
           if(k>0 && v[i].second.first!=0) v[i].second.first*=2,k--;

           if(v[i].second.first> v[i].second.second) ans+=v[i].second.second;
           else ans+=v[i].second.first;

           i++;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    int n,k;
    while(cin>>n>>k)
    {
        ll x,y,ans=0;
        vector<ll> v;
        for(int i=0; i<n; i++)
        {
            cin>>x>>y;
            ans+=min(x,y);
            if(y>x)
              v.push_back(min(x,y-x));
            else
                v.push_back(0);
        }
        int j=0;
        sort(rall(v));
        while(k--) ans+=v[j],j++;
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    int n,k;
    while(cin>>n>>k)
    {
        ll x,y,ans=0;
        vector<ll> v;
        for(int i=0; i<n; i++)
        {
            cin>>x>>y;
            ans+=min(x,y);
            if(y>x)
              v.push_back(min(x,y-x));
        }
        int j=0;
        sort(rall(v));
        while(k--) ans+=v[j],j++;
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0,diff;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           diff=y-x;

           v.push_back(make_pair(diff,make_pair(y,x)));
       }
       sort(rall(v));
       ld ans=0;
       for(int i=0;i<n;i++)
       {
           if(k>0 && v[i].second.first!=0 &&  v[i].second.second!=0) v[i].second.second*=2,k--;
           ans+=min(v[i].second.first,v[i].second.second);
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           sll diff=y-x;

           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       sll i=0,ans=0;
       while( i<n)
       {
           if(k>0 && v[i].second.first!=0) v[i].second.first*=2,k--;
           ans+=min(v[i].second.first,v[i].second.second);
           i++;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           sll diff=y-x;

           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       sll i=0,ans=0;
       while( i<n)
       {
           if(k>0) v[i].second.first*=2,k--;
           ans+=min(v[i].second.first,v[i].second.second);
           i++;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           sll diff=y-x;
           if(diff<0) diff=0;
           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       sll i=0,ans=0;
       while(i!=n)
       {
           if(k>0 && v[i].second.first!=0) v[i].second.first*=2,k--;
           ans+=min(v[i].second.first,v[i].second.second);
           i++;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0,diff;
       vector< pair<sll,sll> >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           diff=y-x;

           v.push_back(make_pair(y,x));
       }
       sort(rall(v));
       ld ans=0;
       for(int i=0;i<n;i++)
       {
           if(k>0 && v[i].first!=0 &&  v[i].second!=0) v[i].second*=2,k--;
           ans+=min(v[i].first,v[i].second);
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0,diff;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           diff=y-x;
           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       sll ans=0;
       for(int i=0;i<n;i++)
       {
           if(k>0 && v[i].second.first!=0 &&  v[i].second.second!=0) v[i].second.first*=2,k--;

           if(v[i].second.first> v[i].second.seond) ans+=v[i].second.second;
           else ans+=v[i].second.first;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    ull n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       ull x,y;
       vector< pair<ull,pair<ull,ull> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           ull diff=y-x;
           if(diff<0) diff=0;
           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       ull i=0,ans=0;
       while(i!=n)
       {
           if(k>0 && v[i].second.first!=0) v[i].second.first*=2,k--;
           ans+=min(v[i].second.first,v[i].second.second);
           i++;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    sll n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       sll x,y,c=0,diff;
       vector< pair<sll,pair<sll,sll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           diff=y-x;
           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       sll ans=0;
       for(int i=0;i<n;i++)
       {
           if(k>0 && v[i].second.first!=0 &&  v[i].second.second!=0) v[i].second.first*=2,k--;

           if(v[i].second.first> v[i].second.second) ans+=v[i].second.second;
           else ans+=v[i].second.first;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef signed long long sll;
typedef unsigned long long ull;
#define all(s) ((s).begin()),((s).end())
#define rall(s) ((s).rbegin()),((s).rend())
#define Pi 3.141592653589793238462643383279502884197169399375106
const int  OO=INT_MAX;

using namespace std;

void Yuko_Kurama()
{
    //freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);
    cin.tie(0);
    ios::sync_with_stdio(false);
}
string n2s(ll n)
{
    ostringstream con;
    con<<n;
    return con.str();

}
int main()
{
    Yuko_Kurama();
    double n,k,sum=0,c=0;
    while(cin>>n>>k)
    {
       ll x,y;
       vector< pair<ll,pair<ll,ll> > >  v;
       for(int i=0;i<n;i++)
       {
           cin>>x>>y;
           ll diff=y-x;
           if(diff<0) diff=0;
           v.push_back(make_pair(diff,make_pair(x,y)));
       }
       sort(rall(v));
       ll i=0,ans=0;
       while(i!=n)
       {
           if(k>0 && v[i].second.first!=0) v[i].second.first*=2,k--;
           ans+=min(v[i].second.first,v[i].second.second);
           i++;
       }
       cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#define maxn 100005
#define ll long long
using namespace std;
struct node
{
    ll wu,buy;
    ll big,sm;
}a[maxn];
bool cmp1(node a,node b)
{
    return a.big>b.big;
}
bool cmp2(node a,node b)
{
    return a.sm>b.sm;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,f;
    scanf("%d%d",&n,&f);
    for(int i=1;i<=n;i++)
        {
            cin>>a[i].wu>>a[i].buy;
            a[i].big=min(a[i].wu*2,a[i].buy);
            a[i].sm=min(a[i].wu,a[i].buy);
        }
    sort(a+1,a+n+1,cmp1);
    ll re=0;
    for(int i=1;i<=f;i++)
        re+=a[i].big;
    //sort(a+1+f,a+n+1,cmp2);
    for(int i=f+1;i<=n;i++)
        re+=a[i].sm;
    cout<<re<<endl;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#define maxn 100005
#define ll long long
using namespace std;
struct node
{
    ll wu,buy;
    ll big,sm;
}a[maxn];
bool cmp(node a,node b)
{
    return a.big>b.big;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,f;
    scanf("%d%d",&n,&f);
    for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld",&a[i].wu,&a[i].buy);
            a[i].big=min(a[i].wu*2,a[i].buy);
            a[i].sm=min(a[i].wu,a[i].buy);
        }
    sort(a+1,a+n+1,cmp);
    int re=0;
    for(int i=1;i<=f;i++)
        re+=a[i].big;
    for(int i=f+1;i<=n;i++)
        re+=a[i].sm;
    printf("%d\n",re);
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#define maxn 100005
#define ll long long
using namespace std;
struct node
{
    ll wu,buy;
    ll big,sm;
}a[maxn];
bool cmp1(node a,node b)
{
    return a.big>b.big;
}
bool cmp2(node a,node b)
{
    return a.sm>b.sm;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,f;
    scanf("%d%d",&n,&f);
    for(int i=1;i<=n;i++)
        {
            cin>>a[i].wu>>a[i].buy;
            a[i].big=min(a[i].wu*2,a[i].buy);
            a[i].sm=min(a[i].wu,a[i].buy);
        }
    sort(a+1,a+n+1,cmp1);
    int re=0;
    for(int i=1;i<=f;i++)
        re+=a[i].big;
    sort(a+1+f,a+n+1,cmp2);
    for(int i=f+1;i<=n;i++)
        re+=a[i].sm;
    printf("%d\n",re);
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#define maxn 100005
#define ll long long
using namespace std;
struct node
{
    ll wu,buy;
    ll big,sm;
}a[maxn];
bool cmp(node a,node b)
{
    return a.big>b.big;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,f;
    scanf("%d%d",&n,&f);
    for(int i=1;i<=n;i++)
        {
            cin>>a[i].wu>>a[i].buy;
            a[i].big=min(a[i].wu*2,a[i].buy);
            a[i].sm=min(a[i].wu,a[i].buy);
        }
    sort(a+1,a+n+1,cmp);
    int re=0;
    for(int i=1;i<=f;i++)
        re+=a[i].big;
    for(int i=f+1;i<=n;i++)
        re+=a[i].sm;
    printf("%d\n",re);
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#define maxn 100005
#define ll long long
using namespace std;
struct node
{
    ll wu,buy;
    ll big,sm;
}a[maxn];
bool cmp1(node a,node b)
{
    return a.big>b.big;
}
bool cmp2(node a,node b)
{
    return a.sm>b.sm;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,f;
    scanf("%d%d",&n,&f);
    for(int i=1;i<=n;i++)
        {
            cin>>a[i].wu>>a[i].buy;
            a[i].big=min(a[i].wu*2,a[i].buy);
            a[i].sm=min(a[i].wu,a[i].buy);
        }
    sort(a+1,a+n+1,cmp1);
    int re=0;
    for(int i=1;i<=f;i++)
        re+=a[i].big;
    sort(a+1+f+1,a+n+1,cmp2);
    for(int i=f+1;i<=n;i++)
        re+=a[i].sm;
    printf("%d\n",re);
}


//   __________________
//  | ________________ |
//  ||          ____  ||
//  ||   /\    |      ||
//  ||  /__\   |      ||
//  || /    \  |____  ||
//  ||________________||
//  |__________________|
//  \\#################\\
//   \\#################\\
//    \        ____       \
//     \_______\___\_______\
// An AC a day keeps the doctor away.

#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) 0//(cerr<<#x<<" = "<<(x)<<'\n')
#define all(v) begin(v),end(v)
#define siz(v) (ll(v.size()))
#define get_pos(v,x) (lower_bound(all(v),x)-begin(v))
#define pb emplace_back
#define ff first
#define ss second
#define mid (l+(r-l>>1))

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
template<typename T> using rkt = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
constexpr ld PI = acos(-1),eps = 1e-8;
constexpr ll N = 12125, INF = 1e18, MOD = 1000000007, K = 6025;

rkt<pll> BIT[N];
void ins(int x, int y) {
    for(int p = x; p < N; p += p&-p) BIT[p].insert({y,BIT[p].size()});
}
void ins(pll R) {ins(R.ff,R.ss);}
ll qry(int x, int y1, int y2) {
    ll res = 0;
    for(int p = x; p; p -= p&-p) res += BIT[p].order_of_key({y2+1,0}) - BIT[p].order_of_key({y1,0});
    return res;
}
ll C2(ll x){return x*(x-1)/2;}
ll n;
vector<pair<pll,pll> > H,V;
signed main(){
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++) {
        pll l,r;
        cin >> l.ff >> l.ss >> r.ff >> r.ss;
        l.ff += K, l.ss += K, r.ff += K, r.ss += K;
        if(l > r) swap(l,r);
        if(l.ff == r.ff) {
            V.pb(l,r);
        }else if(l.ss == r.ss) {
            H.pb(l,r);
        }
    }
    sort(all(H)), sort(all(V));
    int p = 0;
    ll res = 0;
    for(int i = 0; i < siz(V); i++) {
        ll x1 = V[i].ff.ff;
        while(p < H.size() && H[p].ff.ff <= x1) ins(H[p++].ss);
        for(int j = i+1; j < siz(V); j++) {
            ll x2 = V[j].ff.ff;
            ll y1 = max(V[i].ff.ss, V[j].ff.ss);
            ll y2 = min(V[i].ss.ss, V[j].ss.ss);
            // P.x <= x1, x2 <= Q.x, y1 <= y <= y2
            if(y2 > y1) res += C2(qry(N-1,y1,y2) - qry(x2-1,y1,y2));
        }
    }
    cout << res << '\n';
}
/*
5
1 5 1 0
0 1 5 1
5 4 0 4
4 2 4 0
4 3 4 5

6
5 1 0 1
1 0 1 5
4 5 4 0
2 4 0 4
3 4 5 4
0 2 6 2
*/


//   __________________
//  | ________________ |
//  ||          ____  ||
//  ||   /\    |      ||
//  ||  /__\   |      ||
//  || /    \  |____  ||
//  ||________________||
//  |__________________|
//  \\#################\\
//   \\#################\\
//    \        ____       \
//     \_______\___\_______\
// An AC a day keeps the doctor away.

#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) 0//(cerr<<#x<<" = "<<(x)<<'\n')
#define all(v) begin(v),end(v)
#define siz(v) (ll(v.size()))
#define get_pos(v,x) (lower_bound(all(v),x)-begin(v))
#define pb emplace_back
#define ff first
#define ss second
#define mid (l+(r-l>>1))

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> rkt;
constexpr ld PI = acos(-1),eps = 1e-8;
constexpr ll N = 10125, INF = 1e18, MOD = 1000000007, K = 1e7;

rkt BIT[N];
void ins(int x, int y) {
    for(int p = x; p < N; p += p&-p) BIT[p].insert(y);
}
void ins(pll R) {ins(R.ff,R.ss);}
ll qry(int x, int y1, int y2) {
    ll res = 0;
    for(int p = x; p; p -= p&-p) res += BIT[p].order_of_key(y2+1) - BIT[p].order_of_key(y1);
    return res;
}
ll C2(ll x){return x*(x-1)/2;}
ll n;
vector<pair<pll,pll> > H,V;
signed main(){
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++) {
        pll l,r;
        cin >> l.ff >> l.ss >> r.ff >> r.ss;
        l.ff += 5001, l.ss += 5001, r.ff += 5001, r.ss += 5001;
        if(l > r) swap(l,r);
        if(l.ff == r.ff) {
            V.pb(l,r);
        }else if(l.ss == r.ss) {
            H.pb(l,r);
        }
    }
    sort(all(H)), sort(all(V));
    int p = 0;
    ll res = 0;
    for(int i = 0; i < siz(V); i++) {
        ll x1 = V[i].ff.ff;
        while(p < H.size() && H[p].ff.ff <= x1) ins(H[p++].ss);
        debug(x1-5001);
        for(int j = i+1; j < siz(V); j++) {
            //V[i], V[j];
            ll x2 = V[j].ff.ff;
            ll y1 = max(V[i].ff.ss, V[j].ff.ss);
            ll y2 = min(V[i].ss.ss, V[j].ss.ss);
            // P.x <= x1, x2 <= Q.x, y1 <= y <= y2
            debug(x2-5001);
            res += C2(qry(N-1,y1,y2) - qry(x2-1,y1,y2));
            debug(res);
        }
    }
    cout << res << '\n';
}


//   __________________
//  | ________________ |
//  ||          ____  ||
//  ||   /\    |      ||
//  ||  /__\   |      ||
//  || /    \  |____  ||
//  ||________________||
//  |__________________|
//  \\#################\\
//   \\#################\\
//    \        ____       \
//     \_______\___\_______\
// An AC a day keeps the doctor away.

#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) 0//(cerr<<#x<<" = "<<(x)<<'\n')
#define all(v) begin(v),end(v)
#define siz(v) (ll(v.size()))
#define get_pos(v,x) (lower_bound(all(v),x)-begin(v))
#define pb emplace_back
#define ff first
#define ss second
#define mid (l+(r-l>>1))

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> rkt;
constexpr ld PI = acos(-1),eps = 1e-8;
constexpr ll N = 12125, INF = 1e18, MOD = 1000000007, K = 6025;

rkt BIT[N];
void ins(int x, int y) {
    for(int p = x; p < N; p += p&-p) BIT[p].insert(y);
}
void ins(pll R) {ins(R.ff,R.ss);}
ll qry(int x, int y1, int y2) {
    ll res = 0;
    for(int p = x; p; p -= p&-p) res += BIT[p].order_of_key(y2+1) - BIT[p].order_of_key(y1);
    return res;
}
ll C2(ll x){return x*(x-1)/2;}
ll n;
vector<pair<pll,pll> > H,V;
signed main(){
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++) {
        pll l,r;
        cin >> l.ff >> l.ss >> r.ff >> r.ss;
        l.ff += K, l.ss += K, r.ff += K, r.ss += K;
        if(l > r) swap(l,r);
        if(l.ff == r.ff) {
            V.pb(l,r);
        }else if(l.ss == r.ss) {
            H.pb(l,r);
        }
    }
    sort(all(H)), sort(all(V));
    int p = 0;
    ll res = 0;
    for(int i = 0; i < siz(V); i++) {
        ll x1 = V[i].ff.ff;
        while(p < H.size() && H[p].ff.ff <= x1) ins(H[p++].ss);
        for(int j = i+1; j < siz(V); j++) {
            ll x2 = V[j].ff.ff;
            ll y1 = max(V[i].ff.ss, V[j].ff.ss);
            ll y2 = min(V[i].ss.ss, V[j].ss.ss);
            // P.x <= x1, x2 <= Q.x, y1 <= y <= y2
            if(y2 > y1) res += C2(qry(N-1,y1,y2) - qry(x2-1,y1,y2));
        }
    }
    cout << res << '\n';
}
/*
5
1 5 1 0
0 1 5 1
5 4 0 4
4 2 4 0
4 3 4 5

6
5 1 0 1
1 0 1 5
4 5 4 0
2 4 0 4
3 4 5 4
0 2 6 2
*/


//   __________________
//  | ________________ |
//  ||          ____  ||
//  ||   /\    |      ||
//  ||  /__\   |      ||
//  || /    \  |____  ||
//  ||________________||
//  |__________________|
//  \\#################\\
//   \\#################\\
//    \        ____       \
//     \_______\___\_______\
// An AC a day keeps the doctor away.

#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) 0//(cerr<<#x<<" = "<<(x)<<'\n')
#define all(v) begin(v),end(v)
#define siz(v) (ll(v.size()))
#define get_pos(v,x) (lower_bound(all(v),x)-begin(v))
#define pb emplace_back
#define ff first
#define ss second
#define mid (l+(r-l>>1))

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> rkt;
constexpr ld PI = acos(-1),eps = 1e-8;
constexpr ll N = 12125, INF = 1e18, MOD = 1000000007, K = 5025;

rkt BIT[N];
void ins(int x, int y) {
    for(int p = x; p < N; p += p&-p) BIT[p].insert(y);
}
void ins(pll R) {ins(R.ff,R.ss);}
ll qry(int x, int y1, int y2) {
    ll res = 0;
    for(int p = x; p; p -= p&-p) {
        res += distance(BIT[p].lower_bound(y1),BIT[p].upper_bound(y2));
        //res += BIT[p].order_of_key(y2+1) - BIT[p].order_of_key(y1);
    }
    return res;
}
ll C2(ll x){return x*(x-1)/2;}
ll n;
vector<pair<pll,pll> > H,V;
signed main(){
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++) {
        pll l,r;
        cin >> l.ff >> l.ss >> r.ff >> r.ss;
        l.ff += K, l.ss += K, r.ff += K, r.ss += K;
        if(l > r) swap(l,r);
        if(l.ff == r.ff) {
            V.pb(l,r);
        }else if(l.ss == r.ss) {
            H.pb(l,r);
        }
    }
    sort(all(H)), sort(all(V));
    int p = 0;
    ll res = 0;
    for(int i = 0; i < siz(V); i++) {
        ll x1 = V[i].ff.ff;
        while(p < H.size() && H[p].ff.ff <= x1) ins(H[p++].ss);
        for(int j = i+1; j < siz(V); j++    ) {
            ll x2 = V[j].ff.ff;
            ll y1 = max(V[i].ff.ss, V[j].ff.ss);
            ll y2 = min(V[i].ss.ss, V[j].ss.ss);
            // P.x <= x1, x2 <= Q.x, y1 <= y <= y2
            if(y2 >= y1) res += C2(qry(N-1,y1,y2) - qry(x2-1,y1,y2));
        }
    }
    cout << res << '\n';
}
/*
5
1 5 1 0
0 1 5 1
5 4 0 4
4 2 4 0
4 3 4 5

6
5 1 0 1
1 0 1 5
4 5 4 0
2 4 0 4
3 4 5 4
0 2 6 2
*/


// g++ -std=c++14

/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

#define int ll

const int N = 10004;

vector<pair<int, int> > hori[N], vert[N];

struct bit{		// use 1 based indices!!!
	int n; vector<int> tree;
	bit(int n) : n(n) { tree.assign(n + 1, 0); }
	int query(int l, int r) { return query(r) - query(l - 1); }
	int query(int r) {
		int s = 0;
		for(; r > 0; r -= (r & (-r))) s += tree[r];
		return s;
	}
	void update(int i, int v) {
		for(; i <= n; i += (i & (-i))) tree[i] += v;
	}
};
const int D = 5001;

pair<int, int> intersection(pair<int, int> a, pair<int, int> b){
    if(b.fi > a.se || a.fi > b.se){
        return mp(-1, -1);
    }
    return mp(max(a.fi, b.fi), min(a.se, b.se));
}

int32_t main(){ _
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a += D;
        b += D;
        c += D;
        d += D;
        if(b == d){ // horizontal
            hori[b].push_back({min(a, c), max(a, c)});
        }
        else{
            vert[a].push_back({min(b, d), max(b, d)});
        }
    }
    bit t(N);
    auto c2 = [](int c) -> int{
        return ((c * (c - 1)) / 2);
    };
    int ans = 0;
    for(int i = 1; i < N; i++){
        for(auto h: hori[i]){
            int l = h.fi;
            int r = h.se;
            map<int, vector<int> > fin;
            for(int j = l; j <= r; j++){
                for(auto v: vert[j]){
                    if(v.fi <= i && v.se > i){
                        fin[v.se + 1].pb(j);
                        t.update(j, 1);
                    }
                }
            }
            for(int j = i + 1; j < N; j++){
                for(auto &k: fin[j]){
                    t.update(k, -1);
                }
                for(auto h2: hori[j]){
                    auto ii = intersection(h, h2);
                    if(ii.fi != -1){
                        int c = t.query(ii.fi, ii.se);
                        ans += c2(c);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}


// g++ -std=c++14

/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

// #define int ll

const int N = 10004;

vector<pair<int, int> > hori[N], vert[N];

struct bit{		// use 1 based indices!!!
	int n; vector<int> tree;
	bit(int n) : n(n) { tree.assign(n + 1, 0); }
	int query(int l, int r) { return query(r) - query(l - 1); }
	int query(int r) {
		int s = 0;
		for(; r > 0; r -= (r & (-r))) s += tree[r];
		return s;
	}
	void update(int i, int v) {
		for(; i <= n; i += (i & (-i))) tree[i] += v;
	}
};
const int D = 5001;

pair<int, int> intersection(pair<int, int> a, pair<int, int> b){
    if(b.fi > a.se || a.fi > b.se){
        return mp(-1, -1);
    }
    return mp(max(a.fi, b.fi), min(a.se, b.se));
}

vector<int> fin[N];

int32_t main(){ _
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a += D;
        b += D;
        c += D;
        d += D;
        if(b == d){ // horizontal
            hori[b].push_back({min(a, c), max(a, c)});
        }
        else{
            vert[a].push_back({min(b, d), max(b, d)});
        }
    }
    bit t(N);
    auto c2 = [](ll c) -> ll{
        return ((c * (c - 1)) / 2);
    };
    ll ans = 0;
    for(int i = 1; i < N; i++){
        for(auto h: hori[i]){
            int l = h.fi;
            int r = h.se;
            vector<int> pre;
            for(int j = l; j <= r; j++){
                for(auto &v: vert[j]){
                    if(v.fi <= i && v.se > i){
                        fin[v.se + 1].pb(j);
                        pre.pb(v.se + 1);
                        t.update(j, 1);
                    }
                }
            }
            for(int j = i + 1; j < N; j++){
                for(auto &k: fin[j]){
                    t.update(k, -1);
                }
                for(auto &h2: hori[j]){
                    auto ii = intersection(h, h2);
                    if(ii.fi != -1){
                        int c = t.query(ii.fi, ii.se);
                        ans += c2(c);
                    }
                }
            }
            for(auto &j: pre){
                fin[j].clear();
            }
        }
    }
    cout << ans << endl;
}


// g++ -std=c++14

/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

// #define int ll

const int N = 10004;

vector<pair<int, int> > hori[N], vert[N];

struct bit{		// use 1 based indices!!!
	int n; vector<int> tree;
	bit(int n) : n(n) { tree.assign(n + 1, 0); }
	int query(int l, int r) { return query(r) - query(l - 1); }
	int query(int r) {
		int s = 0;
		for(; r > 0; r -= (r & (-r))) s += tree[r];
		return s;
	}
	void update(int i, int v) {
		for(; i <= n; i += (i & (-i))) tree[i] += v;
	}
};
const int D = 5001;

pair<int, int> intersection(pair<int, int> a, pair<int, int> b){
    if(b.fi > a.se || a.fi > b.se){
        return mp(-1, -1);
    }
    return mp(max(a.fi, b.fi), min(a.se, b.se));
}

int32_t main(){ _
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a += D;
        b += D;
        c += D;
        d += D;
        if(b == d){ // horizontal
            hori[b].push_back({min(a, c), max(a, c)});
        }
        else{
            vert[a].push_back({min(b, d), max(b, d)});
        }
    }
    bit t(N);
    auto c2 = [](ll c) -> ll{
        return ((c * (c - 1)) / 2);
    };
    ll ans = 0;
    for(int i = 1; i < N; i++){
        for(auto h: hori[i]){
            int l = h.fi;
            int r = h.se;
            map<int, vector<int> > fin;
            for(int j = l; j <= r; j++){
                for(auto v: vert[j]){
                    if(v.fi <= i && v.se > i){
                        fin[v.se + 1].pb(j);
                        t.update(j, 1);
                    }
                }
            }
            for(int j = i + 1; j < N; j++){
                for(auto &k: fin[j]){
                    t.update(k, -1);
                }
                for(auto h2: hori[j]){
                    auto ii = intersection(h, h2);
                    if(ii.fi != -1){
                        int c = t.query(ii.fi, ii.se);
                        ans += c2(c);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}


// g++ -std=c++14

/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

// #define int ll

const int N = 10004;

vector<pair<int, int> > hori[N], vert[N];

struct bit{		// use 1 based indices!!!
	int n; vector<int> tree;
	bit(int n) : n(n) { tree.assign(n + 1, 0); }
	int query(int l, int r) { return query(r) - query(l - 1); }
	int query(int r) {
		int s = 0;
		for(; r > 0; r -= (r & (-r))) s += tree[r];
		return s;
	}
	void update(int i, int v) {
		for(; i <= n; i += (i & (-i))) tree[i] += v;
	}
};
const int D = 5001;

pair<int, int> intersection(pair<int, int> a, pair<int, int> b){
    if(b.fi > a.se || a.fi > b.se){
        return mp(-1, -1);
    }
    return mp(max(a.fi, b.fi), min(a.se, b.se));
}

vector<int> fin[N];

int32_t main(){ _
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a += D;
        b += D;
        c += D;
        d += D;
        if(b == d){ // horizontal
            hori[b].push_back({min(a, c), max(a, c)});
        }
        else{
            vert[a].push_back({min(b, d), max(b, d)});
        }
    }
    bit t(N);
    auto c2 = [](ll c) -> ll{
        return ((c * (c - 1)) / 2);
    };
    ll ans = 0;
    for(int i = 1; i < N; i++){
        for(auto h: hori[i]){
            int l = h.fi;
            int r = h.se;
            vector<int> pre;
            for(int j = l; j <= r; j++){
                for(auto &v: vert[j]){
                    if(v.fi <= i && v.se > i){
                        fin[v.se + 1].pb(j);
                        pre.pb(v.se + 1);
                        t.update(j, 1);
                    }
                }
            }
            for(int j = i + 1; j < N; j++){
                for(auto &k: fin[j]){
                    t.update(k, -1);
                }
                for(auto &h2: hori[j]){
                    auto ii = intersection(h, h2);
                    if(ii.fi != -1){
                        int c = t.query(ii.fi, ii.se);
                        ans += c2(c);
                    }
                }
            }
            for(auto &j: pre){
                fin[j].clear();
            }
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <unordered_map>

#define LL long long
#define ll long long
#define ull unsigned long long
#define ls l, mid, rt << 1
#define rs mid + 1, r, rt << 1 | 1
using namespace std;
const int maxn=5005;
struct Edge
{
	int x1, y1, x2, y2;
	Edge(int _x1, int _y1, int _x2, int _y2)
	{
		x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2;
	}
};
vector<Edge>e1, e2, tmp;
bool cmp1(Edge a, Edge b)
{
	return a.x1 < b.x1;
}
bool cmp2(Edge a, Edge b)
{
	return a.y1 < b.y1;
}
bool cmp3(Edge a, Edge b)
{
	return a.y2 < b.y2;
}
int tr[10010];
int lowbit(int x)
{
	return x & (-x);
}
void update(int x)
{
	for(; x <= 10005; x += lowbit(x))
		tr[x] += 1;
}
int query(int x)
{
	int ans = 0;
	for(; x; x -= lowbit(x))
		ans += tr[x];
	return ans;
}
int main()
{
	int n;
	scanf("%d", &n);
	while(n --)
	{
		int x1, x2, y1, y2;
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		x1 += 5002, y1 += 5002, x2 += 5002, y2 += 5002;
		if(x1 == x2)
		{
			if(y1 < y2)
				swap(y1, y2);
			e2.push_back(Edge(x1, y1, x2, y2));
		}
		else
		{
			if(x1 > x2)
				swap(x1, x2);
			e1.push_back(Edge(x1, y1, x2, y2));
		}
	}
	//cout << e1.size() << " " << e2.size() << endl;
	/*
	for(int i = e2.size() - 1; i >= 0; i --)
		cout << e2[i].x1 << " " << e2[i].y1 << " " << e2[i].x2 << " " << e2[i].y2 << endl;
	*/
	sort(e1.begin(), e1.end(), cmp2);
	/*
	for(int i = 0; i < e1.size(); i ++)
		cout << e1[i].x1 << " " << e1[i].y1 << " " << e1[i].x2 << " " << e1[i].y2 << endl;
	 */
	LL ans = 0;
	sort(e2.begin(), e2.end(), cmp1);
	int siz1 = e1.size(), siz2 = e2.size();
	for(int i = 1; i < siz1; i ++)
	{
		//cout << e1[i].x1 << " " << e1[i].y1 << " " << e1[i].x2 << " " << e1[i].y2 << endl;
		for(int j = 0; j < siz2; j ++)
		{
			//cout << e2[j].x1 << " " << e2[j].y1 << " " << e2[j].x2 << " " << e2[j].y2 << endl;
			if(e2[j].x1 >= e1[i].x1 && e2[j].x2 <= e1[i].x2 && e2[j].y1 >= e1[i].y1 && e2[j].y2 <= e1[i].y2)
			{
				tmp.push_back(e2[j]);
			}
		}
		//cout << tmp.size() << endl;
		sort(tmp.begin(), tmp.end(), cmp3);

		//for(int i = 0; i < tmp.size(); i ++)
		//	cout << tmp[i].x1 << " " << tmp[i].y1 << " " << tmp[i].x2 << " " << tmp[i].y2 << endl;

		int sizz = tmp.size();
		int st = 0;
		for(int j = 0; j < i; j ++)
		{
			//cout << e1[j].x1 << " " << e1[j].y1 << " " << e1[j].x2 << " " << e1[j].y2 << endl;
			while(tmp[st].y2 <= e1[j].y1 && st < sizz)
			{
				//cout << tmp[st].x1 << endl;
				update(tmp[st].x1);
				st ++;
			}
			//cout << e1[j].y1 << " " << e1[j].y2 << endl;
			//cout << query(e1[j].y2) << " " << query(e1[j].y1 - 1) << endl;
			int temp = query(e1[j].x2) - query(e1[j].x1 - 1);
			ans += 1LL * temp * (temp - 1) / 2;
		}
		tmp.clear();
		memset(tr, 0, sizeof(tr));
		//cout << ans << endl;
	}
	cout << ans << endl;
	return 0;
}