#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
#include <ctime>

using namespace std;

#define x first
#define y second
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define SZ(x) int(x.size())
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = b-1; i >= a; i--)
#define DBG(x) cerr << (#x) << "=" << x << "\n";
#define ll long long
#define inf 1000000007
#define mod 1000000007
#define N 200005

template<typename U, typename V> void Min(U &a, const V & b){if(a > b)a = b;}
template<typename U, typename V> void Max(U &a, const V & b){if(a < b)a = b;}
template<typename U, typename V> void add(U &a, const V & b){a = (a + b) % mod;}
template<typename U> U gcd(U a, U b){
    if(a == 0)return b;
    if(b == 0)return a;
    if(a >= b)return gcd(a % b, b);
    else return gcd(a, b % a);
}
int pow(int a, int b){
    int ans = 1;
    while(b){
        if(b & 1)ans = 1LL * ans * a % mod;
        a = 1LL * a * a % mod;
        b >>= 1;
    }
    return ans;
}
int pow(int a, ll b, int c){
    int ans = 1;
    while(b){
        if(b & 1)ans = 1LL * ans * a % c;
        a = 1LL * a * a % c;
        b >>= 1;
    }
    return ans;
}

int a[N], b[N];
int main(){
  int ca = 0, T, i, j, k, K, n, m;
  scanf("%d", &n);
  rep(i, 0, n) scanf("%d", &a[i]);
  reverse(a, a+n);
  rep(i, 0, n) scanf("%d", &b[i]);
  ll ans = 0, tot = 0;;
  set<pii> s;
  rep(i, 0, n){
    tot += a[i] + b[i];
    s.insert({a[i], i});
    s.insert({b[i], i+n});
    ans += s.begin()->x;
    s.erase(s.begin());
  }
  printf("%lld\n", tot - ans);
}

#include <bits/stdc++.h>

// #define ONLINE_JUDGE 1
using namespace std;
#define ll long long int 
#define endl '\n'
#define vi vector<int>
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define f(var,start,end) for(int var = start; var<=end; var++)
#define all(x) x.begin(),x.end()
#define v(x) vector<x>
#define rall(x) x.rbegin(),x.rend()
#define mod 1000000007LL
const ll INF = (ll)(1e9);

void __print(int x) {cerr << x;}void __print(long x) {cerr << x;}void __print(long long x) {cerr << x;}void __print(unsigned x) {cerr << x;}void __print(unsigned long x) {cerr << x;}void __print(unsigned long long x) {cerr << x;}void __print(float x) {cerr << x;}void __print(double x) {cerr << x;}void __print(long double x) {cerr << x;}void __print(char x) {cerr << '\'' << x << '\'';}void __print(const char *x) {cerr << '\"' << x << '\"';}void __print(const string &x) {cerr << '\"' << x << '\"';}void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifdef _Deep_M
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif

// fflush(stdout) or cout.flush() in C ++;
void solver();

int main()
{
#ifdef _Deep_M 
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	freopen("error.txt","w",stderr);
#else
	ios::sync_with_stdio(0);cin.tie(0);
#endif	
ll t;
t = 1;

// cin >> t;

for(int i = 1; i<=t; i++)
{
	// cout << "Case #"<<i<<": ";//google kickstart;
	solver();
}

return 0;
}

void solver()
{	
	ll deep;
	ll n;
	ll k;
	cin >> n>> k;
	map<ll,ll> arr;
	for(int i= 0; i<n; i++)
	{
		ll x,y;
		cin >> x>> y;
		arr[x]+=y;
	}

	ll ans = 0;

	for(auto i: arr)
	{
		dbg(i);
		if(i.first-ans<=k)
		{
			k-=i.first;
			k+=i.second;
			ans+=i.first;
		}
		else
		{
			break;
		}
	}
	ans+=k;
	cout << ans<< endl;
}








