-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCC-CHFNSWPS.cpp
More file actions
80 lines (73 loc) · 1.68 KB
/
CC-CHFNSWPS.cpp
File metadata and controls
80 lines (73 loc) · 1.68 KB
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
//This code is written by Shammi Anand
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Max(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define nl "\n"
#define w(x) int x; cin>>x; while(x--)
void shammi() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main() {
shammi();
w(t) {
ll mn = mod;
ll n, ans = 0; cin >> n;
map <ll, pair<ll, ll> > freq;
for (int i = 0; i < n; i++) {
ll a; cin >> a;
mn = min(mn, a);
freq[a].f++;
}
for (int i = 0; i < n; i++) {
ll b; cin >> b;
freq[b].s++;
mn = min(mn, b);
}
vector < ll > A, B;
bool possible = true;
for (auto check : freq) {
if ((check.s.f + check.s.s) % 2 == 1) {
possible = false;
continue;
}
if (abs(check.s.f - check.s.s) > 0) {
if (check.s.f > check.s.s) {
int num = abs(check.s.f - check.s.s) / 2;
fill_n(back_inserter(A), num, check.f);
} else {
int num = abs(check.s.f - check.s.s) / 2;
fill_n(back_inserter(B), num, check.f);
}
}
}
if (!possible) {
cout << -1 << nl;
continue;
}
//debugging :
// cout << "A : ";
// for (auto x : A) cout << x << " ";
// cout << nl;
// cout << "B : ";
// for (auto x : B) cout << x << " ";
// cout << nl;
ll len = A.size();
for (int i = 0; i < len; i++) {
ans += min(mn * 2, min(A[i], B[len - i - 1]));
A[i] = 0;
B[len - i - 1] = 0;
}
cout << ans << nl;
}
return 0;
}