#PS0000105. Tổng của các tổng đoạn (Sum of Sums)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng của các tổng đoạn (Sum of Sums)
Sum of Sums
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers and an integer , there are exactly contiguous subarrays of length . Compute the sum of each such subarray, then add all these sums. An element contributes separately to each subarray containing it.
Input
The first line contains two integers .
The second line contains integers .
Output
Print one integer: the sum of the sums of all length- contiguous subarrays.
Subtasks
- Subtask 1 (50 points): ; ; for all .
- Subtask 2 (50 points): ; ; for all .
Examples
Input
5 3
1 2 4 8 16
Output
49
Explanation
There are five elements and the required length is three. The three subarray sums are , , and . Their sum is .
Example 2
Input
20 10
100000000 100000000 98667799 100000000 100000000 100000000 100000000 99986657 100000000 100000000 100000000 100000000 100000000 98995577 100000000 100000000 99999876 100000000 100000000 99999999
Output
10988865195
Explanation
There are 11 windows of exactly 10 elements. The sums, in order of starting positions 1 through 11, are 998654456, 998654456, 998654456, 999986657, 998982234, 998982234, 998982234, 998982110, 998995453, 998995453, 998995452. Their total is 10988865195, exceeding the signed 32-bit range.