Pagini recente » Cod sursa (job #1438535) | Cod sursa (job #2891120) | Cod sursa (job #509620) | Cod sursa (job #2317698) | Cod sursa (job #586057)
Cod sursa(job #586057)
#include <cstdio>
#include <climits>
#include <set>
#include <algorithm>
using namespace std;
#define N 100010
#define ui unsigned int
#define pii pair< ui,ui >
#define fs first
#define sc second
#define mp make_pair
int n,nra,nrb;
ui ca[N],cb[N];
int h[N],nh;
ui v[N],t[N];
multiset< pii > s1,s2;
const ui zero = 0;
inline void citire() {
scanf("%d%d%d",&n,&nra,&nrb);
for(int i=1; i<=nra; ++i) {
scanf("%u",&ca[i]);
t[i] = ca[i];
h[i] = i;
}
for(int i=1; i<=nrb; ++i)
scanf("%u",&cb[i]);
}
inline int left_son(int x) {
return (x<<1);
}
inline int right_son(int x) {
return ((x<<1)+1);
}
inline int father(int x) {
return (x>>1);
}
inline void upheap(int k) {
int key = h[k];
while(k>1 && t[key]<t[h[father(k)]]) {
h[k] = h[father(k)];
k = father(k);
}
h[k] = key;
}
inline void downheap(int k) {
int son;
do {
son = 0;
if(left_son(k)<=nh) {
son = left_son(k);
if(right_son(k)<=nh && t[h[right_son(k)]]<t[h[son]])
son = right_son(k);
if(t[h[son]]>=t[h[k]])
son = 0;
}
if(son) {
swap(h[k],h[son]);
k = son;
}
}while(son);
}
inline void rezolvaa() {
nh = nra;
for(int i=(nra/2)+(nra&1); i>0; --i)
downheap(i);
for(int i=1; i<=n; ++i) {
v[i] = t[h[1]];
//printf("%d\n",h[1]);
t[h[1]] += ca[h[1]];
downheap(1);
}
}
inline bool vezi(ui m) {
s1.clear();
s2.clear();
for(int i=1; i<=nrb; ++i)
s1.insert(mp(cb[i],cb[i]));
pii aux;
multiset< pii >::iterator it;
for(int i=n; i>0; --i) {
while(!s2.empty()) {
it = s2.end();
--it;
aux = (*it);
if(aux.fs>=v[i]) {
s2.erase(it);
swap(aux.fs,aux.sc);
s1.insert(aux);
} else
break;
}
it = s1.lower_bound(mp((ui)(m-v[i]),zero));
if(it==s1.end()) {
if(s1.empty())
return false;
--it;
aux = (*it);
s1.erase(it);
} else {
aux = (*it);
if(v[i]+aux.fs!=m) {
if(it==s1.begin())
return false;
--it;
}
aux = (*it);
s1.erase(it);
}
if(v[i]>aux.fs)
aux.sc = v[i]-aux.fs;
else
aux.sc = zero;
swap(aux.fs,aux.sc);
s2.insert(aux);
}
return true;
}
inline void rezolvab() {
ui p=v[nra]+1,u=UINT_MAX,pok=u,m;
while(p<=u) {
m = p + ((u-p)>>1);
if(vezi(m)) {
pok = m;
u = m-1;
} else
p = m+1;
}
printf("%u %u\n",v[n],pok);
}
int main() {
freopen("fabrica.in","r",stdin);
freopen("fabrica.out","w",stdout);
citire();
rezolvaa();
printf("%u 1\n",v[n]);
return 0;
rezolvab();
return 0;
}