#include<iostream> #include<cstring> #include<cstdio> #define maxn 100010 #define maxm 17 #define ll long long usingnamespacestd;
int n, m, a[maxn], b[maxm]; ll s[maxn];
int f[1 << maxm];
intcalc(int x, int v){ int l = x + 1, r = n, mid, ans = x; while (l <= r) { mid = l + r >> 1; if (v >= s[mid] - s[x]) ans = mid, l = mid + 1; else r = mid - 1; } return ans; }
intmain(){ cin >> m >> n; int M = (1 << m) - 1; for (int i = 1; i <= m; ++i) cin >> b[i]; for (int i = 1; i <= n; ++i) cin >> a[i], s[i] = s[i - 1] + a[i]; for (int S = 0; S < M; ++S) for (int i = 1; i <= m; ++i) { int T = S | 1 << i - 1; if (S == T) continue; f[T] = max(f[T], calc(f[S], b[i])); } int ans = -1; for (int i = 1; i <= M; ++i) { if (f[i] != n) continue; int res = 0; for (int j = 1; j <= m; ++j) if (!(i >> j - 1 & 1)) res += b[j]; ans = max(ans, res); } cout << ans << endl; return0; }