A chain of unmanned relay stations forwards a spacecraft's baseline telemetry signal down the line. Every station receives the current accumulated signal value from the station before it, combines that value with its own local reading using one fixed combination rule shared by the entire chain, and passes the new value on to the next station. The combination rule is one of three modes: SUM (add the reading to the accumulated value), DIFF (subtract the reading from the accumulated value), or PROD (multiply the accumulated value by the reading). Starting from a given baseline signal, process every station's reading in order along the chain and report the final accumulated signal value that emerges after the last station.
SUM, DIFF, or PROD.n and init, the number of relay stations and the baseline signal value.n space-separated integers, the local reading of each station in order along the chain.A single integer: the final accumulated signal value after folding in all n station readings, in order, under the given mode.
1 <= n <= 100-1000 <= init <= 1000-1000 <= reading_i <= 1000Example 1
Input
SUM 4 10 1 2 3 4
Expected
20
Explanation
Starting from init=10 in SUM mode, the accumulator is updated as 10+1=11, 11+2=13, 13+3=16, 16+4=20, so the final signal is 20.
Example 2
Input
PROD 3 2 3 -1 5
Expected
-30
Explanation
Starting from init=2 in PROD mode, the accumulator is updated as 2*3=6, 6*-1=-6, -6*5=-30, so the final signal is -30.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →