#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 10000000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    for(i = 0; i < n; ++i)
    {
        buf = charge(a[i], b[i], INF);
        if(buf < 0.0)
        {
            energy -= buf;
        }
    }
    if(p*mid >= energy)
        printf("-1");
    while((R-L) > 0.00001)
    {
        double mid = (R+L)/2.0;
        energy = 0.0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
            {
                energy -= buf;
            }
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
        printf("%lf", (L + R)/2);
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 100000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    while((R-L) > 0.000001)
    {
        double mid = (R+L)/2;
        energy = 0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
            {
                energy -= buf;
            }
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
    if((INF - L) <= 0.00001)
        printf("-1");
    else
        printf("%lf", (L + R)/2);
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 100000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    for(i = 0; i < n; ++i)
    {
        buf = charge(a[i], b[i], INF);
        if(buf < 0.0)
            energy -= buf;
    }
    if(p*INF >= energy)
        printf("-1");
    while((R-L) > 0.000001)
    {
        double mid = (R+L)/2;
        energy = 0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
            {
                energy -= buf;
            }
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
    if((INF - L) <= 0.00001)
        printf("-1");
    else
        printf("%lf", (L + R)/2);
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 10000000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    while((R-L) > 0.00001)
    {
        double mid = (R+L)/2.0;
        energy = 0.0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
            {
                energy -= buf;
            }
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
    if((INF - L) <= 0.00001)
        printf("-1");
    else
        printf("%lf", (L + R)/2);
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 100000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    for(i = 0; i < n; ++i)
    {
        buf = charge(a[i], b[i], INF);
        if(buf < 0.0)
            energy -= buf;
    }
    if(p*INF >= energy)
    {
        printf("-1");
        return 0;
    }
    while((R-L) > 0.000001)
    {
        double mid = (R+L)/2;
        energy = 0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
            {
                energy -= buf;
            }
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
    if((INF - L) <= 0.00001)
        printf("-1");
    else
        printf("%lf", (L + R)/2);
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 100000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    energy = 0;
    for(i = 0; i < n; ++i)
    {
        buf = charge(a[i], b[i], INF);
        if(buf < 0.0)
            energy -= buf;
    }
    if(p*INF >= energy)
    {
        printf("-1");
        return 0;
    }
    while((R-L) > 0.000001)
    {
        double mid = (R+L)/2;
        energy = 0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
                energy -= buf;
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
    printf("%lf", (L + R)/2);
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>
#include <algorithm>

#define INF 1000000000000.0
using namespace std;

double charge(int a, int b, double t)
{
    return b - (double)a*t;
}

int main()
{
    int i, n, p;
    scanf("%d %d", &n, &p);
    int *a, *b;
    a = new int [n];
    b = new int [n];
    for(i = 0; i < n; ++i)
    {
        scanf("%d", &a[i]);
        scanf("%d", &b[i]);
    }
    double L = 0.0;
    double R = INF;
    double buf;
    double energy;
    while((R-L) > 0.000001)
    {
        double mid = (R+L)/2;
        energy = 0;
        for(i = 0; i < n; ++i)
        {
            buf = charge(a[i], b[i], mid);
            if(buf < 0.0)
            {
                energy -= buf;
            }
        }
        if(p*mid >= energy)
            L = mid;
        else
            R = mid;
    }
    if((INF - L) <= 0.00001)
        printf("-1");
    else
        printf("%lf", (L + R)/2);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=1000000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>1000000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=100005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>100000) puts("-1");
    else printf("%.8f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=100000000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>100000000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q), sum+=A[i].p;
    if(P>=sum){
        puts("-1");
        return 0;
    }
    s=0, e=5000000000005;
    while(abs(e-s)>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=m*A[i].p-(double)A[i].q;
            if(p>0) sum+=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    printf("%.8f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=10000000000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>10000000000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=10000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>10000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=5000000000005;
    while(abs(e-s)>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=m*A[i].p-A[i].q;
            if(p>0) sum+=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>5000000000000) puts("-1");
    else printf("%.8f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=500000000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>500000000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=1000000000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>1000000000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100000;
const double eps=1e-6;

int N;
long long P;

struct Phone{
    int p, q;
} A[MAX_N+5];

int main(){
    int i;
    double sum, p;
    double s, e, m;
    scanf("%d %I64d", &N, &P);
    for(i=1; i<=N; i++) scanf("%d %d", &A[i].p, &A[i].q);
    s=0, e=5000000000005;
    while(e-s>eps){
        m=(s+e)/2;
        sum=0;
        for(i=1; i<=N; i++){
            p=A[i].q-m*A[i].p;
            if(p<0) sum-=p;
        }
        if(m*P>=sum) s=m;
        else e=m;
    }
    if(m>5000000000000) puts("-1");
    else printf("%f\n", m);
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	FOR(i,1,n)cin>>d[i].a>>d[i].b;
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=p*m;
		FOR(i,1,n){
			double s=d[i].b-d[i].a*m;
			if(s<eps)tmp+=s;
			if(tmp<eps)break;
		}
		if(tmp>=eps){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-5;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n,p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	FOR(i,1,n)cin>>d[i].a>>d[i].b;
	double l=0,r=1e9,m,t=-1;
	while(abs(r-l)>eps){
		m=(l+r)/2.0;
		double tmp=0;
		FOR(i,1,n)
			tmp+=min(d[i].b-d[i].a*m,0.0);
		//cout<<l<<" "<<r<<" "<<tmp<<endl;
		if(tmp+p*m>=eps){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e9)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	FOR(i,1,n)cin>>d[i].a>>d[i].b;
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=0;
		FOR(i,1,n)
			tmp+=min(d[i].b-d[i].a*m,0.0);
		if(tmp+p*m>=0.0){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-5;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	FOR(i,1,n)cin>>d[i].a>>d[i].b;
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=0;
		FOR(i,1,n)
			tmp+=min(d[i].b-d[i].a*m,0.0);
		if(tmp+p*m>=eps){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-5;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	FOR(i,1,n)cin>>d[i].a>>d[i].b;
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=0;
		FOR(i,1,n)
			tmp+=min(d[i].b-d[i].a*m,0.0);
		if(tmp+p*m>=0.0){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	LL sm=0;
	FOR(i,1,n)cin>>d[i].a>>d[i].b,sm+=d[i].a;
	if(sm<=p){
		puts("-1");
		return 0;
	}
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=p*m;
		FOR(i,1,n){
			double s=d[i].b-d[i].a*m;
			if(s<eps)tmp+=s;
			if(tmp<eps)break;
		}
		if(tmp>=eps){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	LL sm=0;
	FOR(i,1,n)cin>>d[i].a>>d[i].b,sm+=d[i].a;
	if(sm<=p){
		puts("-1");
		return 0;
	}
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=p*m;
		FOR(i,1,n){
			double s=d[i].b-d[i].a*m;
			if(s<0)tmp+=s;
			if(tmp<0)break;
		}
		if(tmp>=0){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
int main(){Accel
	cin>>n>>p;
	LL sm=0;
	FOR(i,1,n)cin>>d[i].a>>d[i].b,sm+=d[i].a;
	if(sm>=p){
		puts("-1");
		return 0;
	}
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		double tmp=p*m;
		FOR(i,1,n){
			double s=d[i].b-d[i].a*m;
			if(s<eps)tmp+=s;
			if(tmp<eps)break;
		}
		if(tmp>=eps){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	if(abs(r-1e18)>eps)
		printf("%.4lf\n",t);
	else puts("-1");
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
bool judge(double m){
	double tmp=p*m;
	FOR(i,1,n){
		double s=d[i].b-d[i].a*m;
		if(s<0)tmp+=s;
		if(tmp<0)return 0;
	}
	return 1;
}
int main(){Accel
	cin>>n>>p;
	double sm=0;
	FOR(i,1,n)cin>>d[i].a>>d[i].b,sm+=d[i].a;
	if(sm<=p){
		puts("-1");
		return 0;
	}
	double l=0,r=1e18,m,t=0;
	while(r-l>=eps){
		m=(l+r)/2.0;
		if(judge(m)){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	printf("%.4lf\n",t);
}


#include <bits/stdc++.h>
#define LL long long
#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Re(a,b) memset(a,b,sizeof a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
#define REP(I, N) for (int I = 0; I < (N); I++)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define SZ(a) ((int)a.size())
//#pragma GCC optimize(2)
using namespace std;
const int N=1e6+1;
const int M=1e9+7;
const double eps=1e-4;
typedef complex<double>C;
const double PI(acos(-1.0));
//size() use int
//__gcd, atan2(y,x)=y/x , __int128
//c(n,k)=c(n-1,k-1)+c(n-1,k)
//c(i,r),i for r to n =c(n+1,r+1)
//void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
//int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}
//int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
//LL pw(LL a, LL b,LL M) { return b ? b & 1 ? a*pw(a, b - 1,M) % M : pw(a*a%M, b>>1,M) : 1; }
//log() = ln() , log(x)/log(y)=log(y)-base-x
//watch out LL
//INT_MAX 127,INT_MIN 128
int n;
double p;
struct U{
	double a,b;
}d[N];
bool judge(double m){
	double tmp=p*m;
	FOR(i,1,n){
		double s=d[i].b-d[i].a*m;
		if(s<0)tmp+=s;
		if(tmp<0)return 0;
	}
	return 1;
}
int main(){Accel
	cin>>n>>p;
	LL sm=0;
	FOR(i,1,n)cin>>d[i].a>>d[i].b,sm+=d[i].a;
	if(sm<=p){
		puts("-1");
		return 0;
	}
	double l=0,r=1e18,m,t=-1;
	while(r-l>=eps){
		m=(l+r)/2.0;
		if(judge(m)){
			l=t=m;
		}
		else {
			r=m;
		}
	}
	printf("%.4lf\n",t);
}


#include <bits/stdc++.h>
using namespace std;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	return o.time < w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	for(int i = 1; i <= n - 1; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	if(o.time < w.time) return o.time;
	else if(o.time == w.time) {
		if(o.dec < w.dec) return o.time;
	}
	return w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	return o.time < w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
long double rate, total;
struct NODE{
	int dec, ini;
	long double time;
}t[100005];
bool cmp(NODE o, NODE w){
	return o.time < w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	long double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	return o.time < w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) < t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	if(o.time < w.time) return true;
	else if(o.time == w.time) {
		if(o.dec < w.dec) return true;
	}
	return false;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
long double rate, total;
struct NODE{
	int dec, ini;
	long double time;
}t[100005];
bool cmp(NODE o, NODE w){
	return o.time < w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	long double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			long double ans =  1.0 * total / (krate - p) ;
			cout << ans << endl;
			return 0;
		}
	}
	cout << 1.0 * total / (krate - p) << endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	if(o.time < w.time) return o.time;
	else if(o.time == w.time) {
		if(o.dec < w.dec) return o.time;
	}
	return w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			return 0;
		}
	}
	printf("%.12lf\n", 1.0 * total / (krate - p));
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	if(o.time < w.time) return true;
	else if(o.time == w.time) {
		if(o.dec > w.dec) return true;
	}
	return false;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			break;
		}
	}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define mod 1000000007
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int>a(n);
vector<ll>t(n);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>t[i];
map<int,multiset<ll>>dp;
for(int i=0;i<n;i++){
    dp[a[i]].insert(t[i]);
}
ll is=0;
vector<int>z;
for(auto i:dp){
   z.pb(i.first);}
   z.pb(10000000000);
for(int i=0;i<z.size()-1;i++){
    while(dp[z[i]].size()>1){
        int o=dp[z[i]].size()-1;
        int c=min(z[i+1],z[i]+o);
        int it=*dp[z[i]].begin();
        is+=o*(it);
        dp[z[i]].erase(it);
        dp[c].insert(it);
    }
}
cout<<is<<'\n';
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define mod 1000000007
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
vector<ll>a(n);
vector<ll>t(n);
for(ll i=0;i<n;i++)cin>>a[i];
for(ll i=0;i<n;i++)cin>>t[i];
map<int,multiset<ll>>dp;
for(ll i=0;i<n;i++){
    dp[a[i]].insert(t[i]);
}
ll is=0;
vector<ll>z;
for(auto i:dp){
   z.pb(i.first);}
   z.pb(1000000000000);
multiset<ll>temp;
ll k=0;
for(ll i=0;i<z.size()-1;i++){
    while(dp[z[i]].size()>1){
        ll o=dp[z[i]].size()-1+temp.size();
        ll c=min(z[i+1],z[i]+o);
        auto it=dp[z[i]].begin();
        temp.insert(*it);
        k+=*it;
        dp[z[i]].erase(it);
        auto it1=temp.begin();
        if(c!=z[i+1]){
        is+=(c-z[i])*( *it1);}
        if(c==z[i+1]){temp.insert(*it1);k+=*it1;}
        k-=*it1;
        temp.erase(it1);
    }
    while(temp.size()){
        ll o=temp.size()+z[i];
        if(o<z[i+1]){
            auto it=temp.begin();
            is+=(o-z[i])*(*it);
            k-=*it;
            temp.erase(it);
        }
        else break;
    }
    is+=k*(z[i+1]-z[i]);
}
cout<<is<<'\n';
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define mod 1000000007
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
vector<ll>a(n);
vector<ll>t(n);
for(ll i=0;i<n;i++)cin>>a[i];
for(ll i=0;i<n;i++)cin>>t[i];
map<int,multiset<ll>>dp;
for(ll i=0;i<n;i++){
    dp[a[i]].insert(t[i]);
}
ll is=0;
vector<ll>z;
for(auto i:dp){
   z.pb(i.first);}
   z.pb(10000000000);
multiset<ll>temp;
ll k=0;
for(ll i=0;i<z.size()-1;i++){
    ll o=dp[z[i]].size()-1;
    ll v=z[i+1]-o-z[i]-1;
    while(v>0&&temp.size()){
        auto it=temp.end();
        it--;
        dp[z[i]].insert(*it);
        k-=*it;
        temp.erase(it);
        v--;}
    is+=k*(z[i+1]-z[i]);
    while(dp[z[i]].size()>1){
         o=dp[z[i]].size()-1;
        ll c=min(z[i+1],z[i]+o);
        auto it=dp[z[i]].begin();
        is+=(c-z[i])*(*it);
        dp[z[i]].erase(it);
        if(c==z[i+1]){temp.insert(*it);k+=*it;}
        else dp[c].insert(*it);
    }
}
cout<<is<<'\n';
return 0;}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define mod 1000000007
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
vector<ll>a(n);
vector<ll>t(n);
for(ll i=0;i<n;i++)cin>>a[i];
for(ll i=0;i<n;i++)cin>>t[i];
map<int,multiset<ll>>dp;
for(ll i=0;i<n;i++){
    dp[a[i]].insert(t[i]);
}
ll is=0;
vector<ll>z;
for(auto i:dp){
   z.pb(i.first);}
   z.pb(10000000000);
for(ll i=0;i<z.size()-1;i++){
    while(dp[z[i]].size()>1){
        ll o=dp[z[i]].size()-1;
        ll c=min(z[i+1],z[i]+o);
        auto it=dp[z[i]].begin();
        is+= (c-z[i])*(*it);
        dp[z[i]].erase(it);
        dp[c].insert(*it);
    }
}
cout<<is<<'\n';
return 0;}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    //map<int, multiset<int>> m;
    gp_hash_table<int, multiset<int>> m;
    for (int i = 0; i < n; ++i)
        m[a[i]].insert(t[i]);
    int cnt = 0;
    for (auto p : m)
        cnt += p.second.size() == 1;
    if (cnt == m.size()) {
        cout << "0\n";
        return;
    }
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() <= 1) {
            m.erase(m.begin());
            continue;
        }
        int cur = m.begin()->first, i = 0;
        for (auto it = m.begin()->second.begin(); i < m.begin()->second.size() - 1; ++it, ++i) {
            ans += *it;
            m[cur + 1].insert(*it);
        }
        m.erase(m.begin());
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, set<pii>> m;
    for (int i = 0; i < n; ++i)
        m[a[i]].insert({ t[i], i });
    vector<int> to_del;
    for (auto p : m)
        if (p.second.size() == 1)
            to_del.push_back(p.first);
    for (int i : to_del)
        m.erase(i);
    if (m.empty()) {
        cout << "0\n";
        return;
        /*int best = INF1, ans;
        set<int> s;
        for (int i : a)
            s.insert(i);
        vector<int> v;
        for (int i : s)
            v.push_back(i);
        map<int, int> m2;
        m2[v[0]] = 0;
        for (int i = 1; i < v.size(); ++i) {
            if (v[i] == v[i - 1] + 1) {
                m2[v[i]] = m2[v[i - 1]];
            } else {
                m2[v[i]] = m2[v[i - 1]] + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            int left = 0, right = v.size();
            while (left + 1 < right) {
                int mid = (left + right) / 2;
                if (m2[v[mid]] == m2[a[i]])
                    left = mid;
                else
                    right = mid;
            }
            int cur = v[left] - a[i] + 1;
            cur *= t[i];
            if (cur < best) {
                best = cur;
                ans = i;
            }
        }
        cout << best << '\n';
        return;*/
    }
    map<int, set<int>> m3;
    for (int i = 0; i < n; ++i) {
        m3[a[i]].insert(i);
    }
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() == 1) {
            m.erase(m.begin());
            continue;
        }
        ans += m.begin()->second.begin()->first;
        m[m.begin()->first + 1].insert(*m.begin()->second.begin());
        m.begin()->second.erase(m.begin()->second.begin());
        if (m[m.begin()->first + 1].size() == 1 &&
            m3.find(m.begin()->first + 1) != m3.end()) {
            for (int i : m3[m.begin()->first + 1])
                m[m.begin()->first + 1].insert({ t[i], i });
        }
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, set<pii>> m;
    unordered_map<int, set<int>> m3;
    m3.reserve(n);
    for (int i = 0; i < n; ++i) {
        m[a[i]].insert({ t[i], i });
        m3[a[i]].insert(i);
    }
    vector<int> to_del;
    for (auto p : m)
        if (p.second.size() == 1)
            to_del.push_back(p.first);
    for (int i : to_del)
        m.erase(i);
    if (m.empty()) {
        cout << "0\n";
        return;
        /*int best = INF1, ans;
        set<int> s;
        for (int i : a)
            s.insert(i);
        vector<int> v;
        for (int i : s)
            v.push_back(i);
        map<int, int> m2;
        m2[v[0]] = 0;
        for (int i = 1; i < v.size(); ++i) {
            if (v[i] == v[i - 1] + 1) {
                m2[v[i]] = m2[v[i - 1]];
            } else {
                m2[v[i]] = m2[v[i - 1]] + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            int left = 0, right = v.size();
            while (left + 1 < right) {
                int mid = (left + right) / 2;
                if (m2[v[mid]] == m2[a[i]])
                    left = mid;
                else
                    right = mid;
            }
            int cur = v[left] - a[i] + 1;
            cur *= t[i];
            if (cur < best) {
                best = cur;
                ans = i;
            }
        }
        cout << best << '\n';
        return;*/
    }
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() == 1) {
            m.erase(m.begin());
            continue;
        }
        ans += m.begin()->second.begin()->first;
        m[m.begin()->first + 1].insert(*m.begin()->second.begin());
        m.begin()->second.erase(m.begin()->second.begin());
        if (m[m.begin()->first + 1].size() == 1 &&
            m3.find(m.begin()->first + 1) != m3.end()) {
            for (int i : m3[m.begin()->first + 1])
                m[m.begin()->first + 1].insert({ t[i], i });
        }
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    map<int, vector<int>> ma;
    for (int i = 0; i < n; ++i) {
        cin >> t[i];
        ma[a[i]].push_back(t[i]);
    }
    auto it = ma.begin();
    multiset<int> s;
    ll ans = 0, sum = 0, cur;
    while (it != ma.end() || !s.empty()) {
        if (s.size() == 1 && it == ma.end()) {
            s.clear();
            continue;
        }
        if (s.empty()) {
            for (int i : it->second) {
                s.insert(i);
                sum += i;
            }
            cur = it->first;
            ++it;
        }
        auto last = s.end(); --last;
        sum -= *last;
        s.erase(last);
        ans += sum;
        ++cur;
        if (it != ma.end() && it->first == cur) {
            for (int i : it->second) {
                s.insert(i);
                sum += i;
            }
            ++it;
        }
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, set<pii>> m;
    for (int i = 0; i < n; ++i) {
        m[a[i]].insert({ t[i], i });
        //m3[a[i]].insert(i);
    }
    vector<int> to_del;
    for (auto p : m)
        if (p.second.size() == 1)
            to_del.push_back(p.first);
    for (int i : to_del)
        m.erase(i);
    if (m.empty()) {
        cout << "0\n";
        return;
    }
    map<int, set<int>> m3;
    //m3.reserve(n);
    for (int i = 0; i < n; ++i) {
        //m[a[i]].insert({ t[i], i });
        m3[a[i]].insert(i);
    }
    /*if (m.empty()) {
        cout << "0\n";
        return;
        int best = INF1, ans;
        set<int> s;
        for (int i : a)
            s.insert(i);
        vector<int> v;
        for (int i : s)
            v.push_back(i);
        map<int, int> m2;
        m2[v[0]] = 0;
        for (int i = 1; i < v.size(); ++i) {
            if (v[i] == v[i - 1] + 1) {
                m2[v[i]] = m2[v[i - 1]];
            } else {
                m2[v[i]] = m2[v[i - 1]] + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            int left = 0, right = v.size();
            while (left + 1 < right) {
                int mid = (left + right) / 2;
                if (m2[v[mid]] == m2[a[i]])
                    left = mid;
                else
                    right = mid;
            }
            int cur = v[left] - a[i] + 1;
            cur *= t[i];
            if (cur < best) {
                best = cur;
                ans = i;
            }
        }
        cout << best << '\n';
        return;
    }*/
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() <= 1)
            m.erase(m.begin());
        if (m.empty())
            break;
        if (m[m.begin()->first + 1].empty() &&
            m3.find(m.begin()->first + 1) != m3.end()) {
            for (int i : m3[m.begin()->first + 1])
                m[m.begin()->first + 1].insert({ t[i], i });
        }
        auto ite = m.begin()->second.end(); --ite;
        for (auto it = m.begin()->second.begin(); it != ite; ++it) {
            ans += it->first;
            m[m.begin()->first + 1].insert(*it);
        }
        m.erase(m.begin());
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0
#define int ll

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, set<pii>> m;
    for (int i = 0; i < n; ++i)
        m[a[i]].insert({ t[i], i });
    vector<int> to_del;
    for (auto p : m)
        if (p.second.size() == 1)
            to_del.push_back(p.first);
    for (int i : to_del)
        m.erase(i);
    if (m.empty()) {
        cout << "0\n";
        return;
        /*int best = INF1, ans;
        set<int> s;
        for (int i : a)
            s.insert(i);
        vector<int> v;
        for (int i : s)
            v.push_back(i);
        map<int, int> m2;
        m2[v[0]] = 0;
        for (int i = 1; i < v.size(); ++i) {
            if (v[i] == v[i - 1] + 1) {
                m2[v[i]] = m2[v[i - 1]];
            } else {
                m2[v[i]] = m2[v[i - 1]] + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            int left = 0, right = v.size();
            while (left + 1 < right) {
                int mid = (left + right) / 2;
                if (m2[v[mid]] == m2[a[i]])
                    left = mid;
                else
                    right = mid;
            }
            int cur = v[left] - a[i] + 1;
            cur *= t[i];
            if (cur < best) {
                best = cur;
                ans = i;
            }
        }
        cout << best << '\n';
        return;*/
    }
    map<int, set<int>> m3;
    for (int i = 0; i < n; ++i) {
        m3[a[i]].insert(i);
    }
    int ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() == 1) {
            m.erase(m.begin());
            continue;
        }
        ans += m.begin()->second.begin()->first;
        m[m.begin()->first + 1].insert(*m.begin()->second.begin());
        m.begin()->second.erase(m.begin()->second.begin());
        if (m[m.begin()->first + 1].size() == 1 &&
            m3.find(m.begin()->first + 1) != m3.end()) {
            for (int i : m3[m.begin()->first + 1])
                m[m.begin()->first + 1].insert({ t[i], i });
        }
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, multiset<int>> m;
    //gp_hash_table<int, multiset<int>> m;
    for (int i = 0; i < n; ++i)
        m[a[i]].insert(t[i]);
    int cnt = 0;
    for (auto p : m)
        cnt += p.second.size() == 1;
    if (cnt == m.size()) {
        cout << "0\n";
        return;
    }
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() <= 1) {
            m.erase(m.begin());
            continue;
        }
        int cur = m.begin()->first, i = 0;
        for (auto it = m.begin()->second.begin(); i < m.begin()->second.size() - 1; ++it, ++i) {
            ans += *it;
            m[cur + 1].insert(*it);
        }
        m.erase(m.begin());
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, set<pii>> m;
    for (int i = 0; i < n; ++i) {
        m[a[i]].insert({ t[i], i });
        //m3[a[i]].insert(i);
    }
    map<int, set<int>> m3;
    //m3.reserve(n);
    for (int i = 0; i < n; ++i) {
        //m[a[i]].insert({ t[i], i });
        m3[a[i]].insert(i);
    }
    /*if (m.empty()) {
        cout << "0\n";
        return;
        int best = INF1, ans;
        set<int> s;
        for (int i : a)
            s.insert(i);
        vector<int> v;
        for (int i : s)
            v.push_back(i);
        map<int, int> m2;
        m2[v[0]] = 0;
        for (int i = 1; i < v.size(); ++i) {
            if (v[i] == v[i - 1] + 1) {
                m2[v[i]] = m2[v[i - 1]];
            } else {
                m2[v[i]] = m2[v[i - 1]] + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            int left = 0, right = v.size();
            while (left + 1 < right) {
                int mid = (left + right) / 2;
                if (m2[v[mid]] == m2[a[i]])
                    left = mid;
                else
                    right = mid;
            }
            int cur = v[left] - a[i] + 1;
            cur *= t[i];
            if (cur < best) {
                best = cur;
                ans = i;
            }
        }
        cout << best << '\n';
        return;
    }*/
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() == 1)
            m.erase(m.begin());
        if (m.empty())
            break;
        if (m[m.begin()->first + 1].empty() &&
            m3.find(m.begin()->first + 1) != m3.end()) {
            for (int i : m3[m.begin()->first + 1])
                m[m.begin()->first + 1].insert({ t[i], i });
        }
        auto ite = m.begin()->second.end(); --ite;
        for (auto it = m.begin()->second.begin(); it != ite; ++it) {
            ans += it->first;
            m[m.begin()->first + 1].insert(*it);
        }
        m.erase(m.begin());
    }
    cout << ans << '\n';
}


#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define multi_test 0

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

const ll MAXN = 3e5 + 7;
const ll INF = 2e9 + 11;
const ll MOD = 1e9 + 7;
const ll INF1 = 9e18;
const ld EPS = 1e-8;

void solve();

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int q = 1;
    if (multi_test) cin >> q;
    while (q--) solve();
}

/*-------------------------------------------------------------------------------------------------------------*/

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    vector<int> t(n);
    for (int &i : t)
        cin >> i;
    map<int, multiset<int>> m;
    for (int i = 0; i < n; ++i)
        m[a[i]].insert(t[i]);
    int cnt = 0;
    for (auto p : m)
        cnt += p.second.size() == 1;
    if (cnt == m.size()) {
        cout << "0\n";
        return;
    }
    ll ans = 0;
    while (!m.empty()) {
        if (m.begin()->second.size() <= 1) {
            m.erase(m.begin());
            continue;
        }
        int cur = m.begin()->first, i = 0;
        for (auto it = m.begin()->second.begin(); i < m.begin()->second.size() - 1; ++it, ++i) {
            ans += *it;
            m[cur + 1].insert(*it);
        }
        m.erase(m.begin());
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<int> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vpi a(n);
	F0R(i, n) cin >> a[i].f;
	F0R(i, n) cin >> a[i].s;
	sort(all(a));
	pub x = {a[0].f, a[0].s};
	x.pq.push(a[0].s);
	int i = 1;
	ll c = 0;
	while(i < n) {
		while(i < n && a[i].f == x.cnt) {
			x.total_cost += a[i].s;
			x.pq.push(a[i].s);
			i++;
		}
		int ycnt = i < n ? min(a[i].f, x.cnt + sz(x.pq)) : x.cnt + sz(x.pq);
		//cout << x.cnt << " " << ycnt << "\n";
		while(x.cnt < ycnt) {
			x.total_cost -= x.pq.top();
			x.pq.pop();
			c += x.total_cost;
			x.cnt++;
		}
		//cout << a[i].f << " " << x.cnt << "\n";
		//cout << sz(x.pq) << "\n";
		if(sz(x.pq) == 0 && i < n) x.cnt = a[i].f;
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<int> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vpi a(n);
	F0R(i, n) cin >> a[i].f;
	F0R(i, n) cin >> a[i].s;
	sort(all(a));
	pub previous = {a[0].f, 0, priority_queue<int>()};
	vector<pub> p;
	F0R(i, n) {
		if(previous.cnt == a[i].f){
			previous.pq.push(a[i].s);
			previous.total_cost += a[i].s;
		} else {
			previous.cnt *= -1;
			p.pb(previous);
			priority_queue<int> tmp;
			tmp.push(a[i].s);
			previous = {a[i].f, a[i].s, tmp};
		}
	}
	previous.cnt *= -1;
	p.pb(previous);
	ll c = 0;
	F0R(i, sz(p)) {
		auto x = p[i];
		int ycnt = (i+1) < sz(p) ? max(p[i+1].cnt, x.cnt - sz(x.pq)) : x.cnt - sz(x.pq);
		//cout << x.cnt << " " << ycnt << "\n";
		while(x.cnt > ycnt) {
			x.total_cost -= x.pq.top();
			x.pq.pop();
			c += x.total_cost;
			x.cnt--;
		}
		//cout << x.cnt << "\n";
		if((i+1) < sz(p)){
			auto y = p[i+1];
			if(y.cnt == x.cnt) {
				if(sz(x.pq) < sz(y.pq)) swap(x, y);
				x.total_cost += y.total_cost;
				while(sz(y.pq)) {
					x.pq.push(y.pq.top());
					y.pq.pop();
				}
			}
		}
		if(sz(x.pq)) p[i+1] = x;
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<ll> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	map<int, pub> m;
	vi a(n), b(n);
	F0R(i, n) {
		cin >> a[i];
	}
	F0R(i, n) {
		cin >> b[i];
	}
	F0R(i, n) {
		auto it = m.find(a[i]);
		if(it == m.end()) {
			priority_queue<ll> tmp;
			tmp.push(b[i]);
			m[a[i]] = {a[i], b[i], tmp};
		} else {
			(it->second).total_cost+=b[i];
			(it->second).pq.push(b[i]);
		}
	}
	priority_queue<pub> p;
	trav(x, m) x.s.cnt *= -1, p.push(x.s);
	ll c = 0;
	while(sz(p)) {
		auto x = p.top();
		p.pop();
		// cout << "cnt="<< x.cnt << "\n";
		if(sz(x.pq) == 1) continue;
		// cout << x.cnt << " " << x.pq.top() << "\n";
		x.total_cost -= x.pq.top();
		x.pq.pop();
		c += x.total_cost;
		// cout << x.cnt << " " << x.total_cost << "\n";
		x.cnt--;
		if(sz(p)) {
			auto y = p.top();
			if(y.cnt == x.cnt) {
				p.pop();
				// cout << "hi\n";
				if(sz(x.pq) > sz(y.pq)) swap(x, y);
				y.total_cost += x.total_cost;
				while(sz(x.pq)) {
					// cout << "inside\n";
					y.pq.push(x.pq.top());
					// cout << "asd\n";
					x.pq.pop();
				}
				p.push(y);
				// cout << "sz=" << sz(p.top().pq) << "\n";
			}

		}
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<ll> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	map<int, pub> m;
	vi a(n), b(n);
	F0R(i, n) {
		cin >> a[i];
	}
	F0R(i, n) {
		cin >> b[i];
	}
	F0R(i, n) {
		auto it = m.find(a[i]);
		if(it == m.end()) {
			priority_queue<ll> tmp;
			tmp.push(b[i]);
			m[a[i]] = {a[i], b[i], tmp};
		} else {
			(it->second).total_cost+=b[i];
			(it->second).pq.push(b[i]);
		}
	}
	priority_queue<pub> p;
	trav(x, m) x.s.cnt *= -1, p.push(x.s);
	ll c = 0;
	while(sz(p)) {
		auto x = p.top();
		p.pop();
		// cout << "cnt="<< x.cnt << "\n";
		if(sz(x.pq) == 1) continue;
		// cout << x.cnt << " " << x.pq.top() << "\n";
		x.total_cost -= x.pq.top();
		x.pq.pop();
		c += x.total_cost;
		// cout << x.cnt << " " << x.total_cost << "\n";
		x.cnt--;
		if(sz(p)){
			auto y = p.top();
			if(y.cnt == x.cnt) {
				p.pop();
				// cout << "hi\n";
				if(sz(x.pq) < sz(y.pq)) swap(x, y);
				x.total_cost += y.total_cost;
				while(sz(y.pq)) {
					// cout << "inside\n";
					x.pq.push(y.pq.top());
					// cout << "asd\n";
					y.pq.pop();
				}
			}
		}
		p.push(x);
				// cout << "sz=" << sz(p.top().pq) << "\n";
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<ll> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vpi a(n);
	F0R(i, n) cin >> a[i].f;
	F0R(i, n) cin >> a[i].s;
	sort(all(a));
	pub previous = {a[0].f, 0, priority_queue<ll>()};
	priority_queue<pub> p;
	F0R(i, n) {
		if(previous.cnt == a[i].f){
			previous.pq.push(a[i].s);
			previous.total_cost += a[i].s;
		} else {
			previous.cnt *= -1;
			p.push(previous);
			priority_queue<ll> tmp;
			tmp.push(a[i].s);
			previous = {a[i].f, a[i].s, tmp};
		}
	}
	previous.cnt *= -1;
	p.push(previous);
	ll c = 0;
	while(sz(p)) {
		auto x = p.top();
		p.pop();
		// cout << "cnt="<< x.cnt << "\n";
		if(sz(x.pq) == 1) continue;
		// cout << x.cnt << " " << x.pq.top() << "\n";
		x.total_cost -= x.pq.top();
		x.pq.pop();
		c += x.total_cost;
		// cout << x.cnt << " " << x.total_cost << "\n";
		x.cnt--;
		if(sz(p)){
			auto y = p.top();
			if(y.cnt == x.cnt) {
				p.pop();
				// cout << "hi\n";
				if(sz(x.pq) < sz(y.pq)) swap(x, y);
				//cout << sz(x.pq) << " " << sz(y.pq) << "\n";
				x.total_cost += y.total_cost;
				while(sz(y.pq)) {
					// cout << "inside\n";
					x.pq.push(y.pq.top());
					// cout << "asd\n";
					y.pq.pop();
				}
			}
		}
		p.push(x);
				// cout << "sz=" << sz(p.top().pq) << "\n";
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<ll> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vpi a(n);
	F0R(i, n) cin >> a[i].f;
	F0R(i, n) cin >> a[i].s;
	sort(all(a));
	pub previous = {a[n-1].f, 0, priority_queue<ll>()};
	priority_queue<pub> p;
	R0F(i, n) {
		if(previous.cnt == a[i].f){
			previous.pq.push(a[i].s);
			previous.total_cost += a[i].s;
		} else {
			previous.cnt *= -1;
			p.push(previous);
			priority_queue<ll> tmp;
			tmp.push(a[i].s);
			previous = {a[i].f, a[i].s, tmp};
		}
	}
	previous.cnt *= -1;
	p.push(previous);
	ll c = 0;
	while(sz(p)) {
		auto x = p.top();
		p.pop();
		// cout << "cnt="<< x.cnt << "\n";
		if(sz(x.pq) == 1) continue;
		// cout << x.cnt << " " << x.pq.top() << "\n";
		x.total_cost -= x.pq.top();
		x.pq.pop();
		c += x.total_cost;
		// cout << x.cnt << " " << x.total_cost << "\n";
		x.cnt--;
		if(sz(p)){
			auto y = p.top();
			if(y.cnt == x.cnt) {
				p.pop();
				// cout << "hi\n";
				if(sz(x.pq) < sz(y.pq)) swap(x, y);
				//cout << sz(x.pq) << " " << sz(y.pq) << "\n";
				x.total_cost += y.total_cost;
				while(sz(y.pq)) {
					// cout << "inside\n";
					x.pq.push(y.pq.top());
					// cout << "asd\n";
					y.pq.pop();
				}
			}
		}
		p.push(x);
				// cout << "sz=" << sz(p.top().pq) << "\n";
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<ll> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vpi a(n);
	F0R(i, n) cin >> a[i].f;
	F0R(i, n) cin >> a[i].s;
	sort(all(a));
	pub previous = {a[n-1].f, 0, priority_queue<ll>()};
	priority_queue<pub> p;
	R0F(i, n) {
		if(previous.cnt == a[i].f){
			previous.pq.push(a[i].s);
			previous.total_cost += a[i].s;
		} else {
			previous.cnt *= -1;
			p.push(previous);
			priority_queue<ll> tmp;
			tmp.push(a[i].s);
			previous = {a[i].f, a[i].s, tmp};
		}
	}
	previous.cnt *= -1;
	p.push(previous);
	ll c = 0;
	while(sz(p)) {
		auto x = p.top();
		p.pop();
		int ycnt = sz(p) ? max(p.top().cnt, x.cnt - sz(x.pq)) : x.cnt - sz(x.pq);
		//cout << x.cnt << " " << ycnt << "\n";
		while(x.cnt > ycnt) {
			x.total_cost -= x.pq.top();
			x.pq.pop();
			c += x.total_cost;
			x.cnt--;
		}
		//cout << x.cnt << "\n";
		if(sz(p)){
			auto y = p.top();
			if(y.cnt == x.cnt) {
				p.pop();
				if(sz(x.pq) < sz(y.pq)) swap(x, y);
				x.total_cost += y.total_cost;
				while(sz(y.pq)) {
					x.pq.push(y.pq.top());
					y.pq.pop();
				}
			}
		}
		if(sz(x.pq)) p.push(x);
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld; //
typedef double db; //
typedef string str; //
//
typedef pair<int, int> pi;
typedef pair<ll,ll> pl; //
typedef pair<ld,ld> pd; //
//
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd; //
typedef vector<str> vs; //
typedef vector<pi> vpi;
typedef vector<pl> vpl; //

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) //
#define rsz resize
#define ins insert //

#define mp make_pair
#define pb push_back
#define eb emplace_back //
#define f first
#define s second
#define lb lower_bound //
#define ub upper_bound //

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18; //
const ld PI = 4*atan((ld)1); //

template<class T> bool ckmin(T& a, const T& b) {
	return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

struct pub {
	int cnt;
	ll total_cost;
	priority_queue<ll> pq;

	bool operator < (const pub rhs) const{
			return cnt < rhs.cnt;
	}

};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	vpi a(n);
	F0R(i, n) cin >> a[i].f;
	F0R(i, n) cin >> a[i].s;
	sort(all(a));
	pub previous = {a[0].f, 0, priority_queue<ll>()};
	vector<pub> p;
	F0R(i, n) {
		if(previous.cnt == a[i].f){
			previous.pq.push(a[i].s);
			previous.total_cost += a[i].s;
		} else {
			previous.cnt *= -1;
			p.pb(previous);
			priority_queue<ll> tmp;
			tmp.push(a[i].s);
			previous = {a[i].f, a[i].s, tmp};
		}
	}
	previous.cnt *= -1;
	p.pb(previous);
	ll c = 0;
	F0R(i, sz(p)) {
		auto x = p[i];
		int ycnt = (i+1) < sz(p) ? max(p[i+1].cnt, x.cnt - sz(x.pq)) : x.cnt - sz(x.pq);
		//cout << x.cnt << " " << ycnt << "\n";
		while(x.cnt > ycnt) {
			x.total_cost -= x.pq.top();
			x.pq.pop();
			c += x.total_cost;
			x.cnt--;
		}
		//cout << x.cnt << "\n";
		if((i+1) < sz(p)){
			auto y = p[i+1];
			if(y.cnt == x.cnt) {
				if(sz(x.pq) < sz(y.pq)) swap(x, y);
				x.total_cost += y.total_cost;
				while(sz(y.pq)) {
					x.pq.push(y.pq.top());
					y.pq.pop();
				}
			}
		}
		if(sz(x.pq)) p[i+1] = x;
	}
	cout << c << "\n";
}

/**
 * An element is a pair {cnt, cost};
 * We want to group all cnts that are the same.
 * Let's make a struct {cnt, total_cost, pq{cost}}
 *
 * Then we can increment all same cnt elements except 1 using
 * at a cost total_cost - pq.min();
 *
 * cnt++
 * total_cost-=pq.min
 *
 * We can merge 2 pq in n log n with small to large.
 */


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
pair<int, int>arr[N];
multiset<int>seet;
int main()
{
    int n;
    sci(n);
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].first);
    }
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].second);
    }
    sort(arr + 1, arr + n + 1);
    int cur = arr[1].first;
    ll ans = 0;
    int sum = 0;
    for (int i = 1; i <= n || !seet.empty(); ) {
        while (i <= n && arr[i].first == cur) {
            sum += arr[i].second;
            seet.insert(arr[i].second);
            ++i;
        }
        if (seet.size() == 1) {
            sum = 0;
            seet.erase(seet.begin());
            cur = arr[i].first;
        }
        else {
            sum -= *(--seet.end());
            seet.erase(--seet.end());
            ++cur;
            ans += sum;
        }

    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
map<ll, ll>mp;
map<ll, ll>maxtime;
ll arr[N];
ll t[N];
ll r[N];
int main()
{
    int n;
    sci(n);
    ll maxx = 0;
    for (int i = 1; i <= n; ++i) {
        scll(arr[i]);
        maxx = max(maxx, arr[i]);
        ++mp[arr[i]];
    }
    for (int i = 1; i <= n; ++i) {
        scll(t[i]);
        maxtime[arr[i]] = max(maxtime[arr[i]], t[i]);
    }
    vector <pair<ll, pair<ll, int>>>vec;
    vector < pair<ll, pair<ll, int>>>vec2;
    for (int i = 1; i <= n; ++i) {
        vec.push_back({ arr[i], {t[i], i} });
        vec2.push_back({ t[i], {arr[i], i} });
    }
    sort(vec.begin(), vec.end());
    sort(vec2.begin(), vec2.end());
    stack<pair<ll, ll>>st;
    for (int i = n - 1; i >= 0; --i)
    {
        while (!st.empty() && st.top().first >= vec[i].second.first)st.pop();
        if (st.empty())r[vec[i].second.second] = -1;
        else r[vec[i].second.second] = st.top().second;
        st.push({maxtime[vec[i].first], vec[i].first});
    }
    ll ans = 0;
    vector<pair<ll, ll>>temp;
    for (int i = 0; i < n; ++i) {
        if (mp[vec[i].first] > 1) {
            if (r[vec[i].second.second] == -1) {
                temp.push_back({ vec[i].second.first, vec[i].first });

            }
            else {
                ans += 1LL * (r[vec[i].second.second] - vec[i].first) * vec[i].second.first;
                ++mp[r[vec[i].second.second]];
            }
            --mp[vec[i].first];
        }
    }
    sort(temp.rbegin(), temp.rend());
    for (int i = 0; i < temp.size(); ++i) {
        ++maxx;
        ans += (maxx - temp[i].second) * temp[i].first;
    }
    pll(ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <cmath>
#include<cstring>
#include <cstdio>
#include <stack>
#include<iomanip>
#include<queue>
#include <unordered_set>
#include<functional>
#include<iterator>
#include<new>
#include<cstdlib>
#include<math.h>
#include<fstream>
using namespace std;
#define pi 3.141592653589793
#define ii  pair<int,int>
#define ll long long
#define p10(ans) printf("%0.10f\n", ans)
#define pr(ans) printf("%d\n", ans)
#define scc(x)  scanf("%c\n", &c)
#define pll(ans) printf("%lld\n", ans)
#define scll(x) scanf("%lld",&x)
#define scd(a)  scanf("%lf", &a)
#define sci(x)  scanf("%d",&x)
#define pc(ans)  printf("%c", ans)
#define pd(a)    printf("%lf\n", a)
#define scanfchararray(arr) scanf("%s", arr)
#define printfstring(s)    printf("%s\n", s.c_str())
#define all(v)             v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v)            ((int)((v).size()))
#define mem(arr,d) memset(arr,d,sizeof(arr))
#define inf 10000000000000000
#define ninf -10000000000000000
#define MOD 1000000007
#define pow2(x) ((x)*(x))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define eps 1e-3
#define numberofdigit(N) floor(log10(N)) + 1;
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define loop0(i,n) for(int i=0;i<n;++i)
#define loop1(i,n) for(int i=1;i<=n;++i)
#define watch(x) cout << (#x) << " is " << (x) << endl
ll fastPower(ll base, ll exp, ll mod) {
    base %= mod;
    ll result = 1;
    while (exp > 0) {
        if (exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}
long long gcd(long long a, long long b)
{
    if (b == 0)return a; return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return  a * b / gcd(a, b);
}
ll nCr(ll n, ll r, ll mod) {
    if (n < r)
        return 0;

    if (r == 0)
        return 1;

    return ((n % mod) * ((nCr(n - 1, r - 1, mod) / r) % mod)) % mod;
}
ll removeAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code - (val * fastPower(BASE, idx, mod)) % mod + mod) % mod;
}

ll addAt(ll code, int idx, int val, ll BASE, ll mod) {
    return (code + (val * fastPower(BASE, idx, mod)) % mod) % mod;
}

ll shiftLeft(ll code, ll BASE, ll mod) {
    return (code * BASE) % mod;
}

ll shiftRight(ll code, ll BASE, ll mod) {
    return (code * fastPower(BASE, mod - 2, mod) % mod) % mod;
}
const int N = 2e5 + 5;
pair<int, int>arr[N];
multiset<int>seet;
int main()
{
    int n;
    sci(n);
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].first);
    }
    for (int i = 1; i <= n; ++i) {
        sci(arr[i].second);
    }
    sort(arr + 1, arr + n + 1);
    int cur = arr[1].first;
    ll ans = 0;
    int sum = 0;
    for (int i = 1; i <= n; ) {
        while (i <= n && arr[i].first == cur) {
            sum += arr[i].second;
            seet.insert(arr[i].second);
            ++i;
        }
        if (seet.size() == 1) {
            sum -= *(seet.begin());
            seet.erase(seet.begin());
            cur = arr[i].first;
        }
        else {
            sum -= *(--seet.end());
            seet.erase(--seet.end());
            ++cur;
        }
        ans += sum;
    }
    pll(ans);
    return 0;
}