ll @n, @q, len = 0; ll @a[n]; for (ll i = 0; i < n; i++) { len += a[i]; } ll c_idx[n+1], c[n+1]; c_idx[0] = 0; c[0] = 0; rep (i,n) { c_idx[i + 1] = c_idx[i] + a[i]; c[i + 1] = c[i] + a[i] * (a[i] + 1) / 2; } rep (q) { ll @s; if (c[n] < s) { wt(-1); continue; } ll l = lower_bound(c, c + n + 1, s) - c; wt(c_idx[l - 1] + long(std::sqrt(2 * (s - c[l - 1]) + 0.5))); }