forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
48 lines (40 loc) · 968 Bytes
/
C.cpp
File metadata and controls
48 lines (40 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <map>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <sstream>
#define FOR(i, a, b) for(int i=(a), _b=(b); i<=_b; i++)
#define DOW(i, a, b) for(int i=(a), _b=(b); i>=_b; i--)
#define REP(i, b) for(int i=0; i<b; i++)
#define PB push_back
#define CT(x) ((x) << 1)
#define CP(x) (CT(x) + 1)
#define MAXN 50111
using namespace std;
const int MN = 200111;
int n, q, a[MN], cnt[MN];
int main() {
ios::sync_with_stdio(0);
while (cin >> n >> q) {
FOR(i,1,n) cin >> a[i];
memset(cnt, 0, sizeof cnt);
FOR(i,1,q) {
int u, v; cin >> u >> v;
cnt[u]++;
cnt[v+1]--;
}
FOR(i,1,n) cnt[i] += cnt[i-1];
sort(a+1, a+n+1);
sort(cnt+1, cnt+n+1);
long long res = 0;
FOR(i,1,n) res += cnt[i] * (long long) a[i];
cout << res << endl;
}
return 0;
}