using static System.Math; using System.Linq; using System.Collections.Generic; using System; public class P { public int to { get; set; } public int d { get; set; } } public class Hello { static void Main() { var n = int.Parse(Console.ReadLine().Trim()); string[] line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, int.Parse); getAns(n, a); } static void getAns2(int g, long[] n3, List n4) { var n3sum = n3[n3.Length - 1]; var shuki = n4.Count(); var shukisum = n4[shuki - 1]; var q = int.Parse(Console.ReadLine().Trim()); for (int i = 0; i < q; i++) { var k = int.Parse(Console.ReadLine().Trim()); if (k <= g) Console.WriteLine(n3[k]); else { var sum = n3sum; k -= g; var tshuki = k / shuki; sum += tshuki * shukisum; var nk = k % shuki; if (nk != 0) { nk--; if (nk < 0) nk += shuki; sum += n4[nk]; } Console.WriteLine(sum); } } } static void getAns(int n, int[] a) { var aa = new P[n]; for (int i = 0; i < n; i++) aa[i] = new P(); var hs = new HashSet(); var g = -1; var gnode = -1; var p = 0; var c = 1; var n2 = new int[n]; hs.Add(0); while (true) { var np = (p + a[p]) % n; aa[p].to = np; aa[p].d = a[p]; if (!hs.Add(np)) { g = n2[np]; gnode = np; break; } n2[np] = c; p = np; c++; } var n3 = new long[g + 1]; var point = 0; var node = 0; for (int i = 1; i < g + 1; i++) { point += aa[node].d; n3[i] = point; node = aa[node].to; } var n4 = new List(); node = gnode; point = 0; while (true) { point += aa[node].d; n4.Add(point); node = aa[node].to; if (node == gnode) break; } getAns2(g, n3, n4); } }