PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000127. Tổng kép theo hiệu dương (Double Sum)
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 kép theo hiệu dương (Double Sum)
Double Sum
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a sequence of nonnegative integers . For every pair of positions , contribute if it is positive, otherwise contribute zero. Compute
$$\displaystyle S=\sum_{i=1}^{n}\sum_{j=i+1}^{n}\max(A_j-A_i,0).$$Only the original order of positions matters. Equal values give zero contribution. The answer is guaranteed to fit in a signed 64-bit integer.
Input
- First line: .
- Second line: .
Output
Print as a signed 64-bit integer.
Subtasks
- Subtask 1 (20 points): ; ; the answer is guaranteed to satisfy .
- Subtask 2 (30 points): ; ; the answer is guaranteed to satisfy .
- Subtask 3 (50 points): ; ; the answer is guaranteed to satisfy .
Examples
Example 1
Input
3
2 5 3
Output
4
Explanation
There are exactly three position pairs: contributes ; contributes ; contributes . Total 4.
Example 2
Input
10
5 9 3 0 4 8 7 5 4 0
Output
58
Explanation
For each position , sum over earlier positions with . For instance, position 2 contributes and position 5 contributes . Continuing this for all positions gives 58.