PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000121. Tổng các đoạn có độ dài lẻ (Sum of All Odd Length Subarrays)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng các đoạn có độ dài lẻ (Sum of All Odd Length Subarrays)
Sum of All Odd Length Subarrays
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers , consider every interval with and odd length . Add the sum of each such contiguous interval to a single total. Intervals with different endpoints count separately even if their values coincide.
Input
The first line contains . The second line contains the integers .
Output
Print the required integer total.
Subtasks
- Subtask 1 (20 points): ; for .
- Subtask 2 (30 points): ; for .
- Subtask 3 (50 points): ; for .
Example 1
Input
5
1 4 2 5 3
Output
58
Explanation
Length-one intervals contribute 15; the three length-three sums are 7, 11, 10; the length-five sum is 15. The total is 58.
Example 2
Input
2
1 2
Output
3
Explanation
Only the two singleton intervals qualify, giving 1 + 2 = 3.
Example 3
Input
3
10 11 12
Output
66
Explanation
Singletons contribute 33 and the length-three interval contributes 33, giving 66.