#C7258. Array Transformation: Sum of Consecutive Elements

    ID: 51109 Type: Default 1000ms 256MiB

Array Transformation: Sum of Consecutive Elements

Array Transformation: Sum of Consecutive Elements

You are given an array of integers \(a[0], a[1], \ldots, a[n-1]\) and a positive integer \(k\). Your task is to transform the array into a new array \(b\) such that for each index \(i\) (0-based index),

[ b[i] = \sum_{j=i}^{\min(n, i+k)-1} a[j] ]

In other words, for each \(i\), you need to compute the sum of the subarray starting at index \(i\) and spanning the next \(k\) elements (or fewer if there are not enough elements). The transformed value for the last few elements might thus involve fewer than \(k\) numbers.

Example:

  • If \(a = [1, 2, 3, 4, 5]\) and \(k = 3\), then the output is \([6, 9, 12, 9, 5]\) because:
    • \(6 = 1+2+3\)
    • \(9 = 2+3+4\)
    • \(12 = 3+4+5\)
    • \(9 = 4+5\) (only 2 elements remaining)
    • \(5 = 5\) (only 1 element remaining)

inputFormat

The input is given from the standard input (stdin) in the following format:

 n k
 a[0] a[1] ... a[n-1]

where:

  • \(n\) is the number of elements in the array.
  • \(k\) is the number of consecutive elements to sum.
  • The second line contains \(n\) integers representing the elements of the array.

outputFormat

Output the transformed array as a single line with each element separated by a space. The output should be written to the standard output (stdout).

## sample
5 3
1 2 3 4 5
6 9 12 9 5