#include <bits/stdc++.h>
#define lli long long
#define pll pair<lli,lli>
using namespace std;
int k;
int n;
lli A[100005];
lli B[22];
bool vis[1<<20];
lli dp[1<<20];
pll F[100005][21];
// fast input
template<typename T>
inline void fi(T *a)
{
register char c=0;
while (c<33) c=getchar_unlocked();
*a=0;
int tmp = 0;
while (c>33)
{
if ( c == 45 ) tmp = 1;
else *a=*a*10+c-'0';
c=getchar_unlocked();
}
if ( tmp == 1 ) *a = 0-(*a);
}
void pre()
{
for ( int i = 0; i < n; i++ ) {
F[i][0].first = F[i][0].second = A[i];
}
int kk = (int)log2(n);
for ( int i = 1; i <= kk; i++ ) {
for ( int j = 0; j + (1<<(i-1)) < n; j++ ) {
F[j][i].first = max(F[j][i-1].first, F[j+(1<<(i-1))][i-1].first);
F[j][i].second = min(F[j][i-1].second, F[j+(1<<(i-1))][i-1].second);
}
}
}
pll get(int a, int b)
{
int kk = (int)log2(b-a+1);
pll ans;
ans.first = max(F[a][kk].first, F[b+1-(1<<kk)][kk].first);
ans.second = min(F[a][kk].second, F[b+1-(1<<kk)][kk].second);
return ans;
}
lli f(int mask, int len)
{
if ( mask == ((1<<k)-1) ) return 0LL;
if ( vis[mask] ) return dp[mask];
vis[mask] = true;
lli ans = 0;
for ( int i = 0; i < k; i++ ) {
if ( mask&(1<<i) ) continue;
pll tt = get(len, len + B[i]-1);
ans = max(ans, (tt.first - tt.second)*B[i] + f(mask|(1<<i), len + B[i]));
}
dp[mask] = ans;
return ans;
}
int main()
{
fi(&n);
for ( int i = 0; i < n; i++ ) fi(&A[i]);
pre();
fi(&k);
for ( int i = 0; i < k; i++ ) fi(&B[i]);
lli ans = f(0,0);
printf("%lld\n", ans);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGxpIGxvbmcgbG9uZwojZGVmaW5lIHBsbCBwYWlyPGxsaSxsbGk+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGs7CmludCBuOwpsbGkgQVsx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