forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHFNSWPS.cpp
62 lines (57 loc) · 1.26 KB
/
CHFNSWPS.cpp
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
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#define ll long long int
#define all(v) (v).begin(),(v).end()
#define Blegh ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ugh int z;cin>>z;while(z--){solve();}
////////////////////////////////////////////////////////////////////////////////
void solve()
{
ll n,cost=0,i,j;
cin>>n;
vector<ll> A(n),D(2*n),no;
for(i=0;i<n;++i){
cin>>A[i];
D[i]=A[i];
}
for(i=0;i<n;++i)
cin>>D[n+i];
sort(all(A));
sort(all(D));i=0;j=0;
while(i<2*n-1 && j<n){
if(D[i]!=D[i+1]){cout<<-1<<"\n";return; }
else if(D[i]!=A[j]){
if(D[i]<A[j])
{
no.push_back(D[i]);i=i+2;
}
else if(D[i]>A[j])
{
no.push_back(A[j]);++j;
}
}
else{
i+=2;++j;
}
}
while(j<n){
no.push_back(A[j]);++j;
}
while(i<2*n-1){
no.push_back(D[i]);i+=2;
}
auto it=no.begin();
i=no.size();i/=2;
j=2*D[0];int flag=0;
while(i--)
{ if(*it>j){flag=1;break;}
cost+=*it;++it;
}if(flag==1)cost+=(i+1)*j;
cout<<cost<<"\n";
}
int main() {
Blegh
ugh
return 0;
}