We need to answer queries: given an index of chocolate (global index over all boxes), determine which box it falls into. Boxes have counts Ci. This is a typical problem: precompute prefix sums of Ci, then for each query find the smallest box index such that prefix sum >= index.
Constraints: N, Q up to 1e5, total chocolates sum <= 1e6, and each Ci <=10.
We can do prefix sums array of length N, and then for each query binary search over prefix sums. Complexity O(Q log N) which is fine.
Alternatively, because sum <= 1e6 and Ci <=10, we could precompute an array mapping each global index to box number, but that would be O(total chocolates) which is 1e6, also fine. But simpler: binary search.
Edge cases: query index within total sum.
Sample: N=2, C=[2,3], prefixes=[2,5]. query 2: prefix[0]=2 >=2, so box 1. query 4: prefix[0]=2<4, prefix[1]=5>=4, so box 2.
Implementation: read N, read array C of N ints. compute prefix sums in place or separate array: prefix[i] = sum of C[0..i] inclusive. Then for each query I, find smallest i such that prefix[i] >= I. Usually using bisect_left. Print i+1 (1-indexed boxes).
Total chocolates sum can be up to 1e6, so prefix sums fit in int.
Time: O(N+Q log N). Memory: O(N).
Given constraints, also could be done by scanning for each query? Q up to 1e5, N up to 1e5, scanning each query O(N) would be 1e10, too slow. So binary search is needed.
But note: Ci <=10 and total sum <=1e6, could also use method: precompute an array box_of_index of size total sum + 1 (1-indexed) then answer in O(1). With total sum <=1e6, it's feasible: allocate list of size total_sum+1 (like 1e6+1) which is about 8 MB if using ints? Actually Python list of ints may be larger but still possible? Possibly memory okay for 1e6, but Python overhead per int is high (28 bytes) leading to ~28 MB, which might be borderline but maybe okay. However binary search is simpler and memory lighter.
Thus, binary search is fine.
We'll implement solve() reading from stdin, using sys.stdin.buffer.read() for speed. Parse ints.
Pseudo:
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
N = next(it)
C = [next(it) for _ in range(N)]
# compute prefix sums
prefix = [0]*N
s = 0
for i in range(N):
s += C[i]
prefix[i] = s
Q = next(it)
out_lines = []
for _ in range(Q):
I = next(it)
# binary search
lo, hi = 0, N-1
while lo < hi:
mid = (lo+hi)//2
if prefix[mid] >= I:
hi = mid
else:
lo = mid+1
out_lines.append(str(lo+1))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
Check: when prefix[0] maybe exactly equals I? Then hi=0 gives box1.
Edge: N may be 0? But constraints N>=1.
We must also ensure that sum of C maybe up to 1e6, but prefix values are fine.
That's it.
But given the problem statement from "Milly and Pranjul" typical from hackerearth? Possibly they want efficient queries. So binary search.
Alternatively, we could use bisect module: from bisect import bisect_left, but using custom binary search is okay.
We'll implement as above.
Double-check sample: N=2, C=[2,3], prefix=[2,5]. Q=2, queries 2 and 4.
First query: binary search: lo=0 hi=1 mid=0: prefix[0]=2>=2 => hi=0 => lo=0 break? Actually loop continues while lo<hi, after setting hi=0, lo=0, condition false, exit. So box=1. Second: lo=0 hi=1 mid=0: prefix[0]=2<4 => lo=1. Now lo=1 hi=1, exit, box=2. Good.
Now complexity: O(N+Q log N). With N up to 1e5 and Q up to 1e5, logN ~17, so fine.
But wait: total sum of Ci <= 1e6, and Ci <=10. Does that hint that we can answer each query in O(1) using some precomputed array of size total sum? That would be O(total sum) preprocessing, which is okay. But binary search is fine.
Now consider memory: prefix list of N ints, fine.
We'll produce final answer accordingly.
Potential pitfalls: Input might have extra spaces or newlines. Using read().split() handles that.
Edge cases: query index exactly equals total sum, then we want box N. Our binary search works: lo eventually becomes N-1 (since prefix[N-1] = total sum >= I, and prefix[N-2] maybe < I if appropriate). For example, N=1: prefix=[C1]. I= C1. binary: lo=0 hi=0, loop not entered, box=1. Good.
So solution ready.
Now produce final answer.
But the problem might be part of a coding challenge; it's straightforward.
Let's write the final code with proper comments.
We'll also consider using sys.stdout.write to output quickly.
Thus, final solution.
We'll mention the approach: prefix sums + binary search.
That's it. ```python
import sys
def solve() -> None:
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
n = next(it)
# read chocolate counts per box
C = [next(it) for _ in range(n)]
# build prefix sums: pref[i] = total chocolates in boxes 0..i
pref = [0] * n
s = 0
for i in range(n):
s += C[i]
pref[i] = s
q = next(it)
out = []
for _ in range(q):
idx = next(it)
# binary search for first box whose prefix sum >= idx
lo, hi = 0, n - 1
while lo < hi:
mid = (lo + hi) // 2
if pref[mid] >= idx:
hi = mid
else:
lo = mid + 1
# boxes are 1-indexed in output
out.append(str(lo + 1))
sys.stdout.write("\n".join(out))
if name == "main":
solve()