首先把两个过程分别贪心,然后对应匹配一下即可。
(O(l log n).)
code :
#include <bits/stdc++.h>
#define LL long long
using namespace std;
template <typename T> void read(T &x){
x = 0; char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0',ch = getchar();
}
const int N = 1000050;
int n,m,k; LL a[N],b[N],ta[N],tb[N];
struct Node{
int x; LL v;
Node(int xx=0,LL vv=0){ x=xx,v=vv; }
inline bool operator < (const Node w) const{ return v > w.v; }
};
priority_queue<Node>H;
inline void work(int n,LL *a,LL *t){
static LL tt[N];
int len = 0,i; Node tmp;
while (!H.empty()) H.pop();
for (i = 1; i <= n; ++i) H.push(Node(i,a[i])),tt[i] = a[i];
while (len < k){
tmp = H.top(); H.pop(); t[++len] = tmp.v;
i = tmp.x; tt[i] += a[i]; H.push(Node(i,tt[i]));
}
}
int main(){
int i;
read(k),read(n),read(m);
for (i = 1; i <= n; ++i) read(a[i]);
for (i = 1; i <= m; ++i) read(b[i]);
work(n,a,ta); work(m,b,tb);
LL ans = 0;
for (i = 1; i <= k; ++i) ans = max(ans,ta[i] + tb[k+1-i]);
cout << ans << '
';
return 0;
}