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

Bài tập thuộc lộ trình

2 lộ trình

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 nn positive integers and qq inclusive range-sum queries [lj,rj][l_j,r_j]. Before answering all queries, you may permute the array elements arbitrarily once. Find the maximum possible sum of the qq query answers.

Input

The first line contains n,qn,q. The second has nn array values. Each following query line contains lj,rjl_j,r_j.

Output

Print the maximum possible sum of all range queries.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤201\le n,q\le 20; 1≤ai≤2⋅1051\le a_i\le2\cdot10^5; 1≤lj≤rj≤n1\le l_j\le r_j\le n.
  • Subtask 2 (30 points): 1≤n,q≤20001\le n,q\le2000; 1≤ai≤2⋅1051\le a_i\le2\cdot10^5; 1≤lj≤rj≤n1\le l_j\le r_j\le n.
  • Subtask 3 (50 points): 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 1≤ai≤2⋅1051\le a_i\le2\cdot10^5; 1≤lj≤rj≤n1\le l_j\le r_j\le n.

Examples

Input

3 3
5 3 2
1 2
2 3
1 3

Output

25

Explanation

The query frequencies are [2,3,2][2,3,2]. Put value 55 at the middle position and values 33 and 22 elsewhere. The total is 3⋅2+5⋅3+2⋅2=253\cdot2+5\cdot3+2\cdot2=25, which is maximum by pairing largest values with largest frequencies.