-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
141 lines (116 loc) · 3.04 KB
/
E.cpp
File metadata and controls
141 lines (116 loc) · 3.04 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool uin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool uax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll inf = 1e18;
const int N = 1 << 18;
struct node {
ll sum; int cnt;
node() : sum{0}, cnt{0} { }
node(const node &a, const node &b) : sum{a.sum + b.sum}, cnt{a.cnt + b.cnt} { }
};
node t[N << 1];
vector<ll> cmp;
int n;
void build(const vector<int> &a) {
cmp = {all(a)};
sort(all(cmp)); cmp.erase(unique(all(cmp)), end(cmp));
n = sz(cmp);
}
ll get(int x) {
if (x < 0 || t[1].cnt < x)
return inf;
auto rec = [&](auto &&self, int v, int l, int r) -> ll {
if (x == 0) return 0;
if (t[v].cnt <= x)
return x -= t[v].cnt, t[v].sum;
if (r - l == 1) {
auto res = x * cmp[l];
x = 0;
return res;
}
int m = (l + r) / 2;
ll res = self(self, v << 1, l, m);
res += self(self, v << 1|1, m, r);
return res;
};
auto res = rec(rec, 1, 0, n);
assert(x == 0);
return res;
}
void update(int x, int del) {
int p = lower_bound(all(cmp), x) - begin(cmp);
auto rec = [&](auto &&self, int v, int l, int r) -> void {
if (r - l == 1) {
t[v].cnt += del;
t[v].sum = t[v].cnt * cmp[l];
return;
}
int m = (l + r) / 2;
if (p < m) self(self, v << 1, l, m);
else self(self, v << 1|1, m, r);
t[v] = node(t[v << 1], t[v << 1|1]);
};
rec(rec, 1, 0, n);
}
void remove(int x) { update(x, -1); }
void add(int x) { update(x, +1); }
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k; cin >> n >> m >> k;
vector<int> c(n);
for (auto &x : c) cin >> x;
build(c);
vector<int> type(n, 0);
int a[2];
for (auto b : {0, 1}) {
cin >> a[b];
rep(i, 0, a[b]) {
int x; cin >> x; --x;
type[x] |= 1 << b;
}
}
array<vector<int>, 4> grp;
rep(i, 0, n) grp[type[i]].push_back(c[i]);
for (auto &v : grp)
sort(all(v));
ll cur = 0;
int x = min(k, sz(grp[3]));
for (auto x : grp[0])
add(x);
for (auto b : {1, 2}) {
if (k - x > sz(grp[b]))
return cout << "-1\n", 0;
rep(j, 0, k - x)
cur += grp[b][j];
rep(j, k - x, sz(grp[b]))
add(grp[b][j]);
}
rep(i, 0, x) cur += grp[3][i];
rep(i, x, sz(grp[3]))
add(grp[3][i]);
ll ans = get(m - 2 * k + x) + cur;
for (int y = x - 1, i = k - x + 1;
y >= 0 && y + 2 * i <= m && i <= sz(grp[1]) && i <= sz(grp[2]);
--y, ++i) {
cur -= grp[3][y];
add(grp[3][y]);
for (auto b : {1, 2}) {
cur += grp[b][i - 1];
remove(grp[b][i - 1]);
}
uin(ans, get(m - y - 2 * i) + cur);
}
cout << (ans < inf ? ans : -1) << '\n';
}