-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathCHFNSWPS.cpp
86 lines (83 loc) · 1.32 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tc ll te; cin>>te; while(te--)
#define pa pair<ll,ll>
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0);
tc
{
ll n, mini = INT_MAX, s = 0, ans = 0;
vector <ll> v;
map<ll, ll> map1, map2;
cin >> n;
ll a[n], b[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
map1[a[i]]++;
}
for (int i = 0; i < n; i++)
mini = min(mini, a[i]);
for (int i = 0; i < n; i++)
{
cin >> b[i];
map2[b[i]]++;
}
for (int i = 0; i < n; i++)
mini = min(mini, b[i]);
for (int i = 0; i < n; i++)
{
if (map2[a[i]] > 0)
{
map2[a[i]]--;
map1[a[i]]--;
}
}
for (int i = 0; i < n; i++)
{
if (map1[a[i]] % 2)
{
s = 1;
break;
}
if (map2[b[i]] % 2 )
{
s = 1;
break;
}
}
for (int i = 0; i < n; i++)
{
if (map1[a[i]] > 0)
{
v.push_back(a[i]);
map1[a[i]]--;
}
if (map2[b[i]] > 0)
{
v.push_back(b[i]);
map2[b[i]]--;
}
}
sort(v.begin(), v.end());
if (s == 1)
cout << "-1" << endl;
else
{
for (int i = 0; i < v.size() / 2; i += 2)
{
if (v[i] <= mini * 2)
ans += v[i];
else
ans += (mini * 2);
}
cout << ans << endl;
}
}
}