-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathF.cpp
65 lines (65 loc) · 1.54 KB
/
F.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[500001], cost[500001], tot, num[500001], nowres[500001], sum, ans;
priority_queue<int> q;
int cmp(int a, int b)
{
return a > b;
}
signed main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
ans+=a[i];
}
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++)
{
if (a[i] != a[i - 1] || i == 1)
cost[++tot] = a[i], num[tot]++;
else
num[tot]++;
}
for (int i = 1; i <= tot; i++)
{
int sz=q.size();
int sy = min(sum - sz * 2, num[i]);
int now = 0;
int res = min(num[i], sum) - sy;
for (int j = 1; j <= sy; j++)
nowres[++now] = cost[i];
for (int j = 1; j <= res; j+=2)
{
int x = -q.top();
q.pop();
if (x < cost[i])
{
nowres[++now] = cost[i];
if (j < res)
nowres[++now] = cost[i];
}
else
{
nowres[++now] = x;
if (j < res)
nowres[++now] = 2 * cost[i] - x;
}
}
for (int j = 1; j <= now; j++)
{
if (nowres[j] >= 0)
q.push(-nowres[j]);
}
sum += num[i];
}
while (!q.empty())
{
ans += q.top();
q.pop();
}
printf("%lld\n", ans);
return 0;
}