Atomic Energy
#include
using namespace std;
const int N = 21000;
typedef long long ll;
ll f[N], a[N];
int n, q;
int main()
{
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for(int i = n + 1; i <= n * n; i++) f[i] = 1e18;
for(int i = 1; i <= n; i++) f[i] = a[i];
for(int i = n + 1; i <= n * n; i++){
for(int j = 1; j < i; j++){
f[i] = min(f[i], f[j] + f[i - j]);
}
}
ll mina = a[1], minv = 1;
for(ll i = 1; i <= n; i++){
if(mina * i > minv * a[i]){
mina = a[i];
minv = i;
}
}
while(q--){
ll k;
scanf("%lld", &k);
if(k <= n * n){
printf("%lld\n", f[k]);
}
else{
ll cnt = (k - n * n) / minv - 1;
while(k - cnt * minv > n * n) cnt++;
printf("%lld\n", cnt * mina + f[k - cnt * minv]);
}
}
return 0;
}
#include
using namespace std;
const int N = 410;
int n, a[N], b[N], c[N], d[N];
int disab[N][N], disac[N][N], disbc[N][N];
bool check(int a[], int b[], int s1, int s2)
{
static int st1[N], ed1[N], st2[N], ed2[N];
memset(st1, 0, sizeof st1);
memset(ed1, 0, sizeof ed1);
memset(st2, 0, sizeof st2);
memset(ed2, 0, sizeof ed2);
st1[s1] = st2[s2] = 0, ed1[s1] = a[s1], ed2[s2] = b[s2];
for(int i = 1; i < n; i++){
st1[(s1 + i) % n] = ed1[(s1 + i - 1) % n] + d[(s1 + i - 1) % n];
ed1[(s1 + i) % n] = st1[(s1 + i) % n] + a[(s1 + i) % n];
st2[(s2 + i) % n] = ed2[(s2 + i - 1) % n] + d[(s2 + i - 1) % n];
ed2[(s2 + i) % n] = st2[(s2 + i) % n] + b[(s2 + i) % n];
}
for(int i = 0; i < n; i++){
if(st1[i] <= st2[i] && ed1[i] > st2[i]) return false;
if(st1[i] >= st2[i] && st1[i] < ed2[i]) return false;
}
return true;
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &d[i]);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
for(int i = 0; i < n; i++) scanf("%d", &b[i]);
for(int i = 0; i < n; i++) scanf("%d", &c[i]);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j) continue;
disab[i][j] = check(a, b, i, j);
disac[i][j] = check(a, c, i, j);
disbc[i][j] = check(b, c, i, j);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k = 0; k < n; k++){
if(disab[i][j] && disac[i][k] && disbc[j][k]){
printf("%d %d %d\n", i + 1, j + 1, k + 1);
return 0;
}
}
}
}
printf("impossible\n");
return 0;
}