PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000088. Cô bé và tổng lớn nhất (Little Girl and Maximum Sum)
HỌC THEO LỘ TRÌNH
2 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Cô bé và tổng lớn nhất (Little Girl and Maximum Sum)
Little Girl and Maximum Sum
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers and inclusive range-sum queries . Before answering all queries, you may permute the array elements arbitrarily once. Find the maximum possible sum of the query answers.
Input
The first line contains . The second has array values. Each following query line contains .
Output
Print the maximum possible sum of all range queries.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
3 3
5 3 2
1 2
2 3
1 3
Output
25
Explanation
The query frequencies are . Put value at the middle position and values and elsewhere. The total is , which is maximum by pairing largest values with largest frequencies.