#include <bits/stdc++.h>
#include <sys/resource.h>
#include <limits.h>
#pragma comment(linker, "/STACK: 2000000")
using namespace std;
struct SegmentTreeNode {
pair <int,int> max_min;
void assignLeaf(int value) {
max_min = make_pair(value, value);
}
void merge (SegmentTreeNode& left, SegmentTreeNode& right) {
max_min = make_pair(max(left.max_min.first, right.max_min.first), min(left.max_min.second, right.max_min.second));
}
pair <int,int> getValue() {
return max_min;
}
};
void increase_stack_depth() // works on codechef
{
rlimit R;
getrlimit(RLIMIT_STACK, &R);
R.rlim_cur = R.rlim_max;
setrlimit(RLIMIT_STACK, &R);
}
template<class T, class V>
class SegmentTree {
SegmentTreeNode* nodes;
int N;
public:
SegmentTree(T arr[], int N) {
this->N = N;
nodes = new SegmentTreeNode[getSegmentTreeSize(N)];
buildTree(arr, 1, 0, N-1);
}
~SegmentTree() {
delete[] nodes;
}
V getValue(int lo, int hi) {
SegmentTreeNode result = getValue(1, 0, N-1, lo, hi);
return result.getValue();
}
void update(int index, T value) {
update(1, 0, N-1, index, value);
}
private:
void buildTree(T arr[], int stIndex, int lo, int hi) {
if (lo == hi) {
nodes[stIndex].assignLeaf(arr[lo]);
return;
}
int left = 2 * stIndex, right = left + 1, mid = (lo + hi) / 2;
buildTree(arr, left, lo, mid);
buildTree(arr, right, mid + 1, hi);
nodes[stIndex].merge(nodes[left], nodes[right]);
}
SegmentTreeNode getValue(int stIndex, int left, int right, int lo, int hi) {
if (left == lo && right == hi)
return nodes[stIndex];
int mid = (left + right) / 2;
if (lo > mid)
return getValue(2*stIndex+1, mid+1, right, lo, hi);
if (hi <= mid)
return getValue(2*stIndex, left, mid, lo, hi);
SegmentTreeNode leftResult = getValue(2*stIndex, left, mid, lo, mid);
SegmentTreeNode rightResult = getValue(2*stIndex+1, mid+1, right, mid+1, hi);
SegmentTreeNode result;
result.merge(leftResult, rightResult);
return result;
}
int getSegmentTreeSize(int N) {
int size = 1;
for (; size < N; size <<= 1);
return size << 1;
}
void update(int stIndex, int lo, int hi, int index, T value) {
if (lo == hi) {
nodes[stIndex].assignLeaf(value);
return;
}
int left = 2 * stIndex, right = left + 1, mid = (lo + hi) / 2;
if (index <= mid)
update(left, lo, mid, index, value);
else
update(right, mid+1, hi, index, value);
nodes[stIndex].merge(nodes[left], nodes[right]);
}
};
int input[1000005];
int length[10000007];
long int dp[(1 << 22) + 1];
int n;
int p = 0;
SegmentTree <int , pair<int, int> > *ans;
long int func (int mask, int len) {
if (mask == (1 << p) - 1) {
return 0;
}
if(dp[ mask] != -1) {
return dp[mask];
}
long int poppy = 0;
pair <int, int> temp;
for (int i = 0 ; i < p; i++) {
if ((mask & (1 << i)) == 0) {
temp = (*ans).getValue(len, len + length[i] - 1);
poppy = max(poppy , (length[i] *(temp.first - temp.second)) + func ((mask | (1 << i)), 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", &input[i]);
}
ans = new SegmentTree < int, pair <int, int> > (input, 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("%ld\n", func(0,0));
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxzeXMvcmVzb3VyY2UuaD4KI2luY2x1ZGUgPGxpbWl0cy5oPgojcHJhZ21hIGNvbW1lbnQobGlua2VyLCAiL1NUQUNLOiAyMDAwMDAwIikKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IFNlZ21lbnRUcmVlTm9kZSB7CiAgICBwYWlyIDxpbnQsaW50PiBtYXhfbWluOwoKICAgIHZvaWQgYXNzaWduTGVhZihpbnQgdmFsdWUpIHsKICAgICAgICBtYXhfbWluID0gbWFrZV9wYWlyKHZhbHVlLCB2YWx1ZSk7CiAgICB9CiAgICAgdm9pZCBtZXJnZSAoU2VnbWVudFRyZ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4gMDsKICAgIH0KICAgIGlmKGRwWyBtYXNrXSAhPSAtMSkgewogICAgICAgIHJldHVybiBkcFttYXNrXTsKICAgIH0KICAgIGxvbmcgaW50IHBvcHB5ID0gMDsKICAgIHBhaXIgPGludCwgaW50PiB0ZW1wOwogICAgZm9yIChpbnQgaSA9IDAgOyBpIDwgcDsgaSsrKSB7CiAgICAgICAgaWYgKChtYXNrICYgKDEgPDwgaSkpID09IDApIHsKICAgICAgICAgICAgdGVtcCA9ICgqYW5zKS5nZXRWYWx1ZShsZW4sIGxlbiArIGxlbmd0aFtpXSAtIDEpOwogICAgICAgICAgICBwb3BweSA9IG1heChwb3BweSAsIChsZW5ndGhbaV0gKih0ZW1wLmZpcnN0IC0gdGVtcC5zZWNvbmQpKSArIGZ1bmMgKChtYXNrIHwgKDEgPDwgaSkpLCBsZW4gKyBsZW5ndGhbaV0pICk7CiAgICAgICAgfQogICAgfQogICAgZHBbbWFza10gPSBwb3BweTsKICAgIHJldHVybiBwb3BweTsKfQoKCmludCBtYWluKCkgCnsKICAgIAogICAgaW5jcmVhc2Vfc3RhY2tfZGVwdGgoKTsKICAgIGludCBxLGEsYiwgdiwgazsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGZvciAoaW50IGkgPSAwIDsgaSAgPCBuOyBpKyspIHsKICAgICAgICBzY2FuZigiJWQiLCAmaW5wdXRbaV0pOwogICAgfQogICAgYW5zID0gbmV3IFNlZ21lbnRUcmVlIDwgaW50LCBwYWlyIDxpbnQsIGludD4gPiAoaW5wdXQsIG4pOwogICAgbWVtc2V0KGRwICwgLTEsIHNpemVvZihkcCkpOwogICAgc2NhbmYoIiVkIiwgJmspOwogICAgaW50IHRlbXA7CiAgICBmb3IgKGludCBpID0gMCA7IGkgPCBrIDsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiVkIiwgJnRlbXApOwogICAgICAgIGlmKCB0ZW1wID4gMCkgewogICAgICAgICAgICBsZW5ndGhbcCsrXSA9IHRlbXA7CiAgICAgICAgfQogICAgfQogICAgcHJpbnRmKCIlbGRcbiIsIGZ1bmMoMCwwKSk7CiAgICByZXR1cm4gMDsKfQ==