#K2091. Maximum Subarray Score
Maximum Subarray Score
Maximum Subarray Score
You are given the number of players, the number of games each player has played, and each player's game scores. Your task is to determine the maximum total score achievable by each player by summing any contiguous non-empty subarray of their scores. If all possible subarrays result in a negative sum, the answer for that player should be 0.
The optimal approach is based on the following recurrence relation in Latex:
\(\text{max\_ending\_here} = \max(0, \text{max\_ending\_here} + x)\)
\(\text{max\_so\_far} = \max(\text{max\_so\_far}, \text{max\_ending\_here})\)
For example, if one player's scores are [1, 2, 3], then the maximum contiguous subarray sum is 6; if the scores are [-1, 2, 3, -2], then the maximum is 5; and if all scores are negative, the result should be 0.
inputFormat
The input is read from standard input (stdin) as follows:
- The first line contains an integer \(N\) representing the number of players.
- The second line contains \(N\) space-separated integers, where the i-th integer denotes the number of games played by the i-th player.
- This is followed by \(N\) lines. The i-th of these lines contains the scores of the games played by the i-th player (space-separated integers).
outputFormat
Output to standard output (stdout) a single line containing \(N\) space-separated integers, where the i-th integer is the maximum contiguous subarray sum for the i-th player.
## sample3
3 4 2
1 2 3
-1 2 3 -2
-1 -1
6 5 0