水贪心
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define MAXN 500005
#define mod (ll)(1e9+7)
int a[MAXN];
multiset<int> b;
int main(){int n;priority_queue<pii> que;while(~scanf("%d",&n)){while(!que.empty())que.pop();for(int i=1;i<=n;i++){scanf("%d",&a[i]);que.push(make_pair(a[i]-i,i));}for(int i=1;i<=n;i++){int x;scanf("%d",&x);b.insert(x);}ll ans=0;set<int>::iterator it;for(int i=1;i<=n;i++){pii x;do{x=que.top(); que.pop();it=b.upper_bound(x.second);}while(it==b.begin());it--;que.push(x);//cout<<pos<<endl;ans=(ans+x.first)%mod;b.erase(it);que.push(make_pair(x.first-i-n,n+i));//cout<<ans<<endl;}cout<<ans<<endl;}return 0;
}