显然每个bi都要被选择一次,每次贪心的选出an+k=max{aj | bi<=j<n+k}
求出ai的后缀最大值a′i=max(a[j] | j>=i)
对bi从小到大排序,依次选取即可,顺便维护一个maxVal=max(a[i] | i>n)
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#include<memory.h>
#include<queue>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))
using namespace std;
//const int inf=0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 250000 + 5;
const int inf=MOD;
int a[N],b[N];
ll slove(int n){
ll maxVal=-inf;
ll ans=0;
for(int i=1;i<=n;++i){
int idx=b[i];
ll t=max(maxVal,(ll)a[idx]);
ans=(ans+t)%MOD;
maxVal=max(maxVal,(ll)a[idx]-(n+i));
}
return ans;
}
int main()
{
//freopen("/home/lu/code/r.txt","r",stdin);
//freopen("/home/lu/code/w.txt","w",stdout);
int n;
while(~scanf("%d",&n)){
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
a[i]-=i;
}
for(int i=n-1;i>=1;--i){
a[i]=max(a[i],a[i+1]);
}
for(int i=1;i<=n;++i){
scanf("%d",&b[i]);
}
sort(b+1,b+n+1);
printf("%lld\n",slove(n));
}
return 0;
}