Heidi and Doctor Who hopped out of the TARDIS and found themselves at EPFL in 2018. They were surrounded by stormtroopers and Darth Vader was approaching. Miraculously, they managed to escape to a nearby rebel base but the Doctor was very confused. Heidi reminded him that last year's HC2 theme was Star Wars. Now he understood, and he's ready to face the evils of the Empire!
The rebels have s spaceships, each with a certain attacking power a.
They want to send their spaceships to destroy the empire bases and steal enough gold and supplies in order to keep the rebellion alive.
The empire has b bases, each with a certain defensive power d, and a certain amount of gold g.
A spaceship can attack all the bases which have a defensive power less than or equal to its attacking power.
If a spaceship attacks a base, it steals all the gold in that base.
The rebels are still undecided which spaceship to send out first, so they asked for the Doctor's help. They would like to know, for each spaceship, the maximum amount of gold it can steal.
Input
The first line contains integers s and b (1 ≤ s, b ≤ 10^5), the number of spaceships and the number of bases, respectively.
The second line contains s integers a (0 ≤ a ≤ 10^9), the attacking power of each spaceship.
The next b lines contain integers d, g (0 ≤ d ≤ 10^9, 0 ≤ g ≤ 10^4), the defensive power and the gold of each base, respectively.
Output
Print s integers, the maximum amount of gold each spaceship can steal, in the same order as the spaceships are given in the input.
Example
Input
5 4
1 3 5 2 4
0 1
4 2
2 8
9 4
Output
1 9 11 9 11
Note
The first spaceship can only attack the first base.
The second spaceship can attack the first and third bases.
The third spaceship can attack the first, second and third bases.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given s spaceships with attacking power a_i. There are b bases each with defensive power d_j and gold g_j. A spaceship can attack all bases with d <= a_i (i.e., defensive power less than or equal to its attacking power). It steals all gold from those bases. So for each spaceship, the total gold = sum_{j: d_j <= a_i} g_j. That's the maximum (since it can attack all eligible bases). So it's just a prefix sum over gold sorted by defensive power.
We need to compute for each a_i, the total gold sum over bases with d <= a_i. The bases are not necessarily sorted; we can sort them by d, compute prefix sums of g, then for each spaceship, binary search the largest index with d <= a_i, and get prefix sum.
Constraints: s,b up to 1e5, a,d up to 1e9, g up to 1e4 (but sum may be up to 1e9? Actually b*g_max = 1e5 * 1e4 = 1e9, fits in 64-bit). So we can do O((b log b) + (s log b)). That's fine.
Edge cases: a_i could be less than all d's -> sum = 0; a_i greater than all -> sum = total gold.
We need output in order of spaceships as given.
Thus solution:
Read s, b.
Read list a of length s.
Create array of bases: (d, g) for b lines.
Sort bases by d.
Compute prefix sums: let pref[0]=0; for i from 1 to b: pref[i] = pref[i-1] + g_i (where g_i corresponds to sorted base i-1? careful indexing). Let sorted_d = sorted list of d.
Then for each spaceship, binary search in sorted_d for the rightmost index where d <= a_i (e.g., bisect_right(sorted_d, a_i) - 1). Use bisect_right returns index of first element > a_i, so count = bisect_right. Then sum = pref[count] (if we set pref[i] = sum of first i bases). So: let pos = bisect_right(sorted_d, a_i); total = pref[pos].
Print totals.
Complexities: O((b+s) log b).
Implementation details: Use Python's built-in bisect.
But note: There may be multiple bases with same d, all should be included if a_i >= d. Sorting and using bisect_right works.
Now the input example: s=5, b=4, a = [1,3,5,2,4]; bases: (0,1),(4,2),(2,8),(9,4). Sorted by d: (0,1), (2,8), (4,2), (9,4). Pref: [0,1,9,11,15]? Wait: pref[0]=0; after first:1; after second:1+8=9; after third:9+2=11; after fourth:11+4=15. Then for a_i: 1 -> bisect_right([0,2,4,9],1) returns index 1 (since first >1 is 2), so pref[1]=1; 3 -> bisect_right gives index 2 (since 2<=3, but 4>3 => index 2), pref[2]=9; 5 -> index 3 (since 4<=5,9>5 => index3), pref[3]=11; 2 -> index 2 (since 2<=2 but 4>2 => index2), pref[2]=9; 4 -> index 3 (since 4<=4, 9>4 => index3), pref[3]=11. Output: 1 9 11 9 11. matches.
So solution is straightforward.
Now ensure we handle large inputs efficiently: use sys.stdin.read() and parse ints.
Implementation plan:
import sys, bisect.
data = list(map(int, sys.stdin.read().split()))
parse: first two numbers: s, b.
next s numbers: a list.
then 2*b numbers: pairs d,g.
bases_d = []
bases_g = [] but we can create list of tuples.
for i in range(b): d = data[idx]; g = data[idx+1]; idx+=2; bases.append((d,g)).
sort bases by d.
sorted_d = [d for d,g in bases]
pref = [0]
for d,g in bases: pref.append(pref[-1]+g)
then for each a in a_list: pos = bisect.bisect_right(sorted_d, a); total = pref[pos]; append to result.
print result as space-separated.
Time complexity O((b+s) log b).
Edge Cases: None.
Now discuss: What about if there are bases with same d? sorted_d will have duplicates; bisect_right includes them all.
Thus solution done.
But the problem statement: "maximum amount of gold it can steal" is as described.
Thus answer.
We'll produce final solution code.
We need to provide an explanation.
Now maybe there is a twist: spaceship can attack all bases which have defensive power less than or equal to its attacking power, but "A spaceship can attack all the bases which have a defensive power less than or equal to its attacking power." It says "A spaceship can attack all the bases which have a defensive power less than or equal to its attacking power." It doesn't say that it can only attack a subset; it says it can attack all those bases. So it can steal all gold from all eligible bases. So it's indeed sum.
Thus correct.
Now we write final answer with explanation and code.
We'll also mention we used sorting and prefix sum.
Now produce final answer. ```python
import sys
import bisect
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
s = next(it)
b = next(it)
code
ships = [next(it) for _ in range(s)]
bases = []
for _ in range(b):
d = next(it)
g = next(it)
bases.append((d, g))
# sort bases by defensive power
bases.sort(key=lambda x: x[0])
sorted_d = [d for d, _ in bases]
pref = [0]
for _, g in bases:
pref.append(pref[-1] + g)
result = []
for a in ships:
pos = bisect.bisect_right(sorted_d, a) # number of bases with d <= a
result.append(str(pref[pos]))
sys.stdout.write(' '.join(result))