-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.cpp
51 lines (51 loc) · 933 Bytes
/
hello.cpp
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
49
50
51
#include <iostream>
#include <cstring>
#include <algorithm>
const int N = 1e5 + 100;
long long a[N];
long long b[N];
long long sum[N];
bool cmp(int a, int b)
{
return a > b;
}
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
sort(a + 1, a + n + 1, cmp);
int m;
cin >> m;
long long cnt = 0;
while (m--)
{
int l, r;
cin >> l >> r;
cnt = cnt + (sum[r] - sum[l - 1]);
b[l]++;
b[r + 1]--;
}
for (int i = 1; i <= n; i++)
{
b[i] = b[i - 1] + b[i];
}
sort(b + 1, b + n + 1, cmp);
long long ans = 0;
for (int i = 1; i <= n; i++)
{
// cout << "b[i]:" << b[i] << endl;
if (b[i] == 0)
{
break;
}
ans = b[i] * a[i] + ans;
}
cout << ans - cnt << endl;
return 0;
}