CF BUDDY
← Problems·

1809E · Two Tanks

2400 · binary search, dp, implementation

Problem: There are two water tanks, the first one fits aa liters of water, the second one fits bb liters of water. The first tank has cc (0ca0 \le c \le a) liters of water initially, the second tank has dd (0db0 \le d \le b) liters of water initially.

You want to perform nn operations on them. The ii-th operation is specified by a single non-zero integer viv_i. If vi>0v_i > 0, then you try to pour viv_i liters of water from the first tank into the second one. If vi<0v_i < 0, you try to pour vi-v_i liters of water from the second tank to the first one.

When you try to pour xx liters of water from the tank that has yy liters currently available to the tank that can fit zz more liters of water, the operation only moves min(x,y,z)\min(x, y, z) liters of water.

For all pairs of the initial volumes of water (c,d)(c, d) such that 0ca0 \le c \le a and 0db0 \le d \le b, calculate the volume of water in the first tank after all operations are performed.

Input Format: The first line contains three integers n,an, a and bb (1n1041 \le n \le 10^4; 1a,b10001 \le a, b \le 1000) — the number of operations and the capacities of the tanks, respectively.

The second line contains nn integers v1,v2,,vnv_1, v_2, \dots, v_n (1000vi1000-1000 \le v_i \le 1000; vi0v_i \neq 0) — the volume of water you try to pour in each operation.

Output Format: For all pairs of the initial volumes of water (c,d)(c, d) such that 0ca0 \le c \le a and 0db0 \le d \le b, calculate the volume of water in the first tank after all operations are performed.

Print a+1a + 1 lines, each line should contain b+1b + 1 integers. The jj-th value in the ii-th line should be equal to the answer for c=i1c = i - 1 and d=j1d = j - 1.

Note: Consider c=3c = 3 and d=2d = 2 from the first example:

  • The first operation tries to move 22 liters of water from the second tank to the first one, the second tank has 22 liters available, the first tank can fit 11 more liter. Thus, min(2,2,1)=1\min(2, 2, 1) = 1 liter is moved, the first tank now contains 44 liters, the second tank now contains 11 liter.
  • The second operation tries to move 11 liter of water from the first tank to the second one. min(1,4,3)=1\min(1, 4, 3) = 1 liter is moved, the first tank now contains 33 liters, the second tank now contains 22 liter.
  • The third operation tries to move 22 liter of water from the first tank to the second one. min(2,3,2)=2\min(2, 3, 2) = 2 liters are moved, the first tank now contains 11 liter, the second tank now contains 44 liters.

There's 11 liter of water in the first tank at the end. Thus, the third value in the fourth row is 11.

Sample Cases

Case 1

Input

3 4 4
-2 1 2

Output

0 0 0 0 0 
0 0 0 0 1 
0 0 1 1 2 
0 1 1 2 3 
1 1 2 3 4

Case 2

Input

3 9 5
1 -2 2

Output

0 0 0 0 0 0 
0 0 0 0 0 1 
0 1 1 1 1 2 
1 2 2 2 2 3 
2 3 3 3 3 4 
3 4 4 4 4 5 
4 5 5 5 5 6 
5 6 6 6 6 7 
6 7 7 7 7 8 
7 7 7 7 8 9

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback