forked from gitter-badger/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF557C.cpp
More file actions
42 lines (39 loc) · 985 Bytes
/
CF557C.cpp
File metadata and controls
42 lines (39 loc) · 985 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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 100010;
int n, tot;
pair<int, int> a[maxn];
multiset<int> st;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].first);
}
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].second);
tot += a[i].second;
}
sort(a, a + n);
int ans = ~0U >> 1;
for (int i = 0; i < n; i++) {
int j = i, sum = a[i].second;
while (j + 1 < n && a[j+1].first == a[i].first)
sum += a[++j].second;
int cnt = j - i + 1;
multiset<int>::reverse_iterator it = st.rbegin();
for (int k = 0; k < cnt - 1 && it != st.rend(); k++, it++) {
sum += *it;
}
ans = min(ans, tot - sum);
for (int k = i; k <= j; k++) {
st.insert(a[k].second);
}
i = j;
}
printf("%d\n", ans);
return 0;
}