array partition

Array Partition

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

1
2
3
Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
print("input length:")
n = int(input())
if n%2==0:
n=n
else:
print("please input 2n")
exit()
s1 = []
print("input elements:")
for m in range(0,n):
date = int(input())
s1.append(date)
s2 = sorted(s1)
print(s2)
result = 0
for i in range(0,n):
if i%2==0:
result+=s2[i]
print(result)