#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
using ull=unsigned long long;
using pii=pair<int,int>;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &OOO) {
copy_n(istream_iterator<T>(is), OOO.size(), OOO.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int inf = (1LL<<60);
const int maxn = 2e5+100;
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m,k;
cin>>n>>m>>k;
vector<int> aa(n);
cin>>aa;
vector<int> bb(m);
cin>>bb;
multiset<int> a,b;
for(auto x:aa) a.insert(x);
for(auto x : bb){
b.insert(x);
}
while(true){
if(a.empty()){
break;
}
if(b.size()<k){
cout<<"Impossible"<<endl;
return 0;
}
int x = *a.begin();
a.erase(a.lower_bound(x));
if(x<k){
cout<<"Impossible"<<endl;
return 0;
}
x-=k;
vector<int> v;
while(v.size()<k){
int y = *b.rbegin();
v.pb(y-1);
b.erase(b.lower_bound(y));
}
sort(all(v));
for(int j = v.size()-1;j>=0;j--){
if(x==0) break;
if(x>=v[j]){
x-=v[j];
v[j] = 0;
}else{
v[j]-=x;
x = 0;
}
}
while(true){
if(x==0) break;
if(b.empty()){
cout<<"Impossible"<<endl;
return 0;
}
int y = *b.rbegin();
b.erase(b.find(y));
if(x>=y){
x-=y;
continue;
}else{
y-=x;
x = 0;
if(y!=0){
b.insert(y);
}
}
}
for(auto x:v){
if(x!=0) b.insert(x);
}
}
int ans = 0;
for(auto x:b){
ans+=x;
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |