#include <bits/stdc++.h>
#include <sys/resource.h>
#include <limits.h>
#pragma comment(linker, "/STACK: 2000000")
using namespace std;
#define MAXN 100005
#define MAXLOGN 25
int M[MAXN][MAXLOGN];
int MA[MAXN][MAXLOGN];
int A[1000005];
void increase_stack_depth() // works on codechef
{
rlimit R;
getrlimit(RLIMIT_STACK, &R);
R.rlim_cur = R.rlim_max;
setrlimit(RLIMIT_STACK, &R);
}
void Compute_ST(int N){
int i,j;
for(i=0;i<N;i++) {
M[i][0]=i;
MA[i][0]=i;
}
for(j=1; (1<<j) <= N ;j++){
for(i=0;i+(1<<(j-1))<N;i++){
if(A[M[i][j-1]]<=A[M[i+(1<<(j-1))][j-1]])
M[i][j]=M[i][j-1];
else
M[i][j]=M[i+(1<<(j-1))][j-1];
if(A[MA[i][j-1]]>=A[MA[i+(1<<(j-1))][j-1]])
MA[i][j]=MA[i][j-1];
else
MA[i][j]=MA[i+(1<<(j-1))][j-1];
}
}
}
pair <int, int> RMQ(int s,int e){
int k=e-s;
pair <int, int> ret;
k=31-__builtin_clz(k+1); // k = log(e-s+1)
if(A[M[s][k]]<=A[M[e-(1<<k)+1][k]])
ret.second = A[M[s][k]];
else
ret.second = A[M[e-(1<<k)+1][k]];
if(A[MA[s][k]]>=A[MA[e-(1<<k)+1][k]])
ret.first = A[MA[s][k]];
else
ret.first = A[MA[e-(1<<k)+1][k]];
return ret;
}
int length[107];
long long int dp[(1 << 21) + 1];
int n;
int p = 0;
long long int func (int mask, int len) {
if (mask == ((1 << p) - 1)) {
return 0;
}
long long int poppy = 0;
pair <int, int> temp;
for (int i = 0 ; i < p; i++) {
int vv = (1 << i);
if ((mask & (vv)) == 0) {
temp = RMQ(len, len + length[i] - 1);
if (dp[1 << (mask | (vv))] != -1 ){
poppy = max(poppy, (length[i] *(temp.first - temp.second)) + dp[1 << (mask | (vv))]);
} else {
poppy = max(poppy , (length[i] *(temp.first - temp.second)) + func ((mask | (vv)), len + length[i]) );
}
}
}
dp[mask] = poppy;
return poppy;
}
int main()
{
increase_stack_depth();
int q,a,b, v, k;
scanf("%d", &n);
for (int i = 0 ; i < n; i++) {
scanf("%d", &A[i]);
}
Compute_ST(n);
memset(dp , -1, sizeof(dp));
scanf("%d", &k);
int temp;
for (int i = 0 ; i < k ; i++) {
scanf("%d", &temp);
if( temp > 0) {
length[p++] = temp;
}
}
printf("%lld\n", func(0,0));
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxzeXMvcmVzb3VyY2UuaD4KI2luY2x1ZGUgPGxpbWl0cy5oPgojcHJhZ21hIGNvbW1lbnQobGlua2VyLCAiL1NUQUNLOiAyMDAwMDAwIikKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBNQVhOIDEwMDAwNQojZGVmaW5lIE1BWExPR04gMjUKaW50IE1bTUFYTl1bTUFYTE9HTl07CmludCBNQVtNQVhOXVtNQVhMT0dOXTsKaW50IEFbMTAwMDAwNV07CnZvaWQgaW5jcmVhc2Vfc3RhY2tfZGVwdGgoKQkJCS8vIHdvcmtzIG9uIGNvZGVjaGVmCnsKCXJsaW1pdCBSOwoJZ2V0cmxpbWl0KFJMSU1JVF9TVEFDSywgJlIpOwoJUi5ybGltX2N1ciA9IFIucmxpbV9tYXg7CglzZXRybGltaXQoUkxJTUlUX1NUQUNLLCAmUik7Cn0Kdm9pZCBDb21wdXRlX1NUKGludCBOKXsKICAgIGludCBpLGo7CiAgICBmb3IoaT0wO2k8TjtpKyspIHsKICAgICAgICBNW2ldWzBdPWk7CiAgICAgICAgTUFbaV1bMF09aTsKICAgIH0KICAgIGZvcihqPTE7ICgxPDxqKSA8PSBOIDtqKyspewogICAgICAgIGZvcihpPTA7aSsoMTw8KGotMSkpPE47aSsrKXsKICAgICAgICAgICAgaWYoQVtNW2ldW2otMV1dPD1BW01baSsoMTw8KGotMSkpXVtqLTFdXSkKICAgICAgICAgICAgICAgIE1baV1bal09TVtpXVtqLTFdOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBNW2ldW2pdPU1baSsoMTw8KGotMSkpXVtqLTFdOwoKICAgICAgICAgICAgaWYoQVtNQVtpXVtqLTFdXT49QVtNQVtpKygxPDwoai0xKSldW2otMV1dKQogICAgICAgICAgICAgICAgTUFbaV1bal09TUFbaV1bai0xXTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgTUFbaV1bal09TUFbaSsoMTw8KGotMSkpXVtqLTFdOwogICAgICAgIH0KICAgIH0KfQpwYWlyIDxpbnQsIGludD4gUk1RKGludCBzLGludCBlKXsKICAgIGludCBrPWUtczsKICAgIHBhaXIgPGludCwgaW50PiByZXQ7CiAgICBrPTMxLV9fYnVpbHRpbl9jbHooaysxKTsgLy8gayA9IGxvZyhlLXMrMSkKICAgIGlmKEFbTVtzXVtrXV08PUFbTVtlLSgxPDxrKSsxXVtrXV0pCiAgICAgICAgcmV0LnNlY29uZCA9IEFbTVtzXVtrXV07CiAgICBlbHNlCiAgICAgICAgcmV0LnNlY29uZCA9IEFbTVtlLSgxPDxrKSsxXVtrXV07CgogICAgaWYoQVtNQVtzXVtrXV0+PUFbTUFbZS0oMTw8aykrMV1ba11dKQogICAgICAgIHJldC5maXJzdCA9IEFbTUFbc11ba11dOwogICAgZWxzZQogICAgICAgIHJldC5maXJzdCA9IEFbTUFbZS0oMTw8aykrMV1ba11dOwoKICAgIHJldHVybiByZXQ7Cn0KaW50IGxlbmd0aFsxMDddOwpsb25nIGxvbmcgaW50IGRwWygxIDw8IDIxKSArIDFdOwppbnQgbjsKaW50IHAgPSAwOwpsb25nIGxvbmcgaW50IGZ1bmMgKGludCBtYXNrLCBpbnQgbGVuKSB7CiAgIGlmIChtYXNrID09ICgoMSA8PCBwKSAtIDEpKSB7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9CiAgICBsb25nIGxvbmcgaW50IHBvcHB5ID0gMDsKICAgIHBhaXIgPGludCwgaW50PiB0ZW1wOwogICAgZm9yIChpbnQgaSA9IDAg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B0ZW1wOwogICAgICAgIH0KICAgIH0KICAgIHByaW50ZigiJWxsZFxuIiwgZnVuYygwLDApKTsKICAgIHJldHVybiAwOwp9Cg==