Skip to content

Commit c4ed8f8

Browse files
X0rD3v1LMohamad655
authored andcommitted
Create ChefinaAndSwaps.py
1 parent 6b75736 commit c4ed8f8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

CodeChef_problems/ChefinaAndSwaps.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
Problem Link - https://www.codechef.com/JULY20B/problems/CHFNSWPS
3+
'''
4+
5+
from sys import *
6+
from math import *
7+
from collections import *
8+
9+
mod = 1000000007
10+
11+
def get_array(): return list(map(int, stdin.readline().split()))
12+
13+
def get_ints(): return map(int, stdin.readline().split())
14+
15+
def get_int(): return int(stdin.readline())
16+
17+
def get_input(): return stdin.readline().strip()
18+
19+
def main():
20+
tc = get_int()
21+
while(tc):
22+
N = get_int();arr = get_array();arr1 = get_array()
23+
mini = min(min(arr),min(arr1))
24+
counter = Counter(arr+arr1)
25+
set,dic,dic1 = 0,{},{}
26+
for i in counter:
27+
if counter[i]&1:set = 1;break
28+
else:
29+
dic[i] = counter[i]/2
30+
if set:stdout.write("-1\n");
31+
else:
32+
dic1,list1,list2 = dic,[],[]
33+
for i in range(N):
34+
if dic[arr[i]]:
35+
dic[arr[i]] -= 1
36+
else:
37+
list1.append(arr[i])
38+
for i in range(N):
39+
if dic1[arr1[i]]:
40+
dic1[arr1[i]] -= 1
41+
else:
42+
list2.append(arr1[i])
43+
list1.sort();list2.sort(reverse=True)
44+
length = len(list1)
45+
if length==0:stdout.write('0\n')
46+
else:
47+
ans = 0
48+
for i in range(length):
49+
ans += min(2*mini,min(list1[i],list2[i]))
50+
stdout.write(str(ans) + '\n')
51+
tc-=1
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)