the description of the problem is really hard to understand . It’s means we should find a start point k in array B, than go back to array A, find the maximum of Aj−j in range (K,i) .we can us priority queue to slove this.
#include <bits/stdc++.h>
#define INF 0x7f7f7f7f
#define ll long long
using namespace std;
const int maxn = 250005;
const int mod = 1e9+7;
int a[maxn<<1],b[maxn];
struct node
{
int id,x;
bool operator < (const node &b) const
{
if(x != b.x) return x < b.x;
else return id > b.id;
}
};
int n;
int bucket[maxn];
void bucketsort(int vv){
memset(bucket,0,sizeof(bucket));
for(int i=0;i<n;i++){
bucket[b[i]]++;
}
for(int i=0,j=0;i<=vv;i++){
while(bucket[i]){
b[j++]=i;
bucket[i]--;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
node temp;
priority_queue<node> q;
for(int i = 1;i<=n;i++)
{
scanf("%d",&a[i]);
temp.x = a[i]-i;
temp.id = i;
q.push(temp);
}
int vv = 0;
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
//vv = max(vv,b[i]);
}
sort(b,b+n);
//bucketsort(vv);
// for(int i=0;i<n;i++)
// {
// cout<<b[i]<<" ";
// }
// cout<<endl;
int ans = 0;
for(int i= n+1;i<=2*n;i++)
{
temp = q.top();
while(b[i-n-1] > temp.id)
{
q.pop();
temp = q.top();
}
a[i] = temp.x;
temp.id = i;
temp.x = a[i]-i;
q.push(temp);
//q.pop();
}
// for(int i=1;i<=2*n;i++)
// {
// cout<<a[i]<<" ";
// }
// cout<<endl;
for(int i=n+1;i<=n+n;i++)
{
ans = (ans + a[i]) %mod;
}
printf("%d\n",ans);
}
return 0;
}