Polycarp has an array a consisting of n integers.
He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n−1 elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-… or odd-even-odd-even-…) of the removed elements. Polycarp stops if he can’t make a move.
Formally:
If it is the first move, he chooses any element and deletes it;
If it is the second or any next move:
if the last deleted element was odd, Polycarp chooses any even element and deletes it;
if the last deleted element was even, Polycarp chooses any odd element and deletes it.
If after some move Polycarp cannot make a move, the game ends.
Polycarp’s goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.
Help Polycarp find this value.
Input
The first line of the input contains one integer n (1≤n≤2000) — the number of elements of a.
The second line of the input contains n integers a1,a2,…,an (0≤ai≤106), where ai is the i-th element of a.
Output
Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.
Examples
input
5
1 5 7 8 2
output
0
input
6
5 1 2 4 6 3
output
0
input
2
1000000 1000000
output
1000000
题目大意: 给出一个n,下一行给出n个数,在对这个序列的处理过程中,可以选择删除奇数偶数奇数偶数…(直至奇数的个数或偶数的个数为0)或偶数奇数偶数奇数…(直至奇数的个数或偶数的个数为0)的方式尽可能多的删除元素,求未删除的剩下数值。
思路: 在输入序列时统计好奇数的个数和偶数的个数,如果奇数的个数和偶数的个数相等或者两者之间相差为1,直接输出0,否则再对数组进行排序处理,如果序列中偶数个数大于奇数个数,则剩下的数一定是偶数,其剩下数的个数为偶数个数减奇数个数减一;同理,如果序列中奇数个数大于偶数个数,则剩下的数一定是奇数数,其剩下数的个数为奇数个数减偶数个数减一。再用循环遍历找出相应的几个偶数和或奇数和即为答案。
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
int n, a[2010], jishu = 0, oushu = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if(a[i] % 2) jishu++;
else oushu++;
}
if(jishu == oushu || abs(jishu - oushu) == 1) printf("0"); // 个数相等或相差为1直接输出0
else {
sort(a, a + n);
int ans = 0;
if(oushu > jishu) { // 如果偶数个数大于奇数个数
for(int i = 0, j = 0; j < (oushu - jishu - 1 ) && i < n; i++) {
if(a[i] % 2 == 0) { // 找出剩余的偶数和
ans += a[i];
j++;
}
}
} else { // 如果奇数个数大于偶数个数
for(int i = 0, j = 0; j < (jishu - oushu - 1 ) && i < n; i++) {
if(a[i] % 2 == 1) { // 找出剩余的奇数和
ans += a[i];
j++;
}
}
}
printf("%d", ans);
}
return 0;
}