#include #include #include #include #include #include #include #include #define lol long long //#define LIM 9999999 using namespace std; //const lol mod=1e9+7; lol x[200111]; lol y[200111]; lol x1[200111]; lol x2[200111]; lol z[200111]; int main() { for(lol i=0; i<200111; i++) z[i]=0; ios_base::sync_with_stdio(false); ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); lol n,m,a; lol tot=0; lol farthest=0; cin >> n; for(lol i=0; i> x[i]; tot+=x[i]; } for(lol i=0; i> y[i]; z[y[i]]=x[i]; farthest=max(farthest,y[i]); } lol add=0; cin >> m; for(lol i=0; i> x1[i]; for(lol i=0; i> x2[i]; for(lol j=0; j<=x2[i]; j++){ lol t1=x1[i]-j,t2=x1[i]+j; if(t1<0) t1=0; if(t2>farthest) t2=farthest; if(z[t1]!=0){ tot-=z[t1]; add=max(add,z[t1]); } if(z[t2]!=0 && t1!=t2){ tot-=z[t2]; add=max(add,z[t2]); } } } //for(lol i=0; i<=farthest; i++) cout << z[i] << " "; cout << tot+add; return 0; }