// WRONG #12 (LINEAR_REM_WALK): answers are correct, but each query walks the // remaining (K-mu)%lam steps one by one -> O(Q * cycle length) = TLE. // Input: T, then T cases of (N / A / Q / K_1..K_Q). // Needs: big cycle (~1e5) and Q ~ 1e5 with random K up to 1e12. #include using namespace std; typedef long long ll; static void solve() { int n; scanf("%d", &n); vector a(n); for (auto &x : a) scanf("%lld", &x); vector seen(n, -1), path; vector pre(1, 0); int r = 0, steps = 0; while (seen[r] == -1) { seen[r] = steps; path.push_back(r); pre.push_back(pre.back() + a[r]); steps++; r = pre[steps] % n; } int mu = seen[r], lam = steps - mu; ll S = pre[steps] - pre[mu]; int q; scanf("%d", &q); while (q--) { ll k; scanf("%lld", &k); if (k <= steps) { printf("%lld\n", pre[k]); continue; } ll t = k - mu, full = t / lam, rem = t % lam; ll ans = pre[mu] + full * S; for (ll i = 0; i < rem; i++) ans += a[path[mu + i]]; // BUG: O(rem) per query printf("%lld\n", ans); } } int main() { solve(); }