http://codeforces.com/problemset/problem/1144/B
Polycarp has an array aa consisting of nn 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−1n−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 nn (1≤n≤20001≤n≤2000) — the number of elements of aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1060≤ai≤106), where aiai is the ii-th element of aa.
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个数,第一次你可以消去任何一个数,但是若上一次删去的是奇数,则下一次必须是偶数, 反之亦然, 求操作到最后剩下的数字之和的最小值。
思路:排序+贪心,水题。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int a[2005];
int b[2005];
int len1=0,len2=0;
int main()
{
int n,temp;
scanf("%d",&n);
while(n--)
{
scanf("%d",&temp);
if(temp&1)
a[len1++]=temp;
else
b[len2++]=temp;
}
if(len1-len2==1||len2-len1==1||len1==len2)
printf("0\n");
else
{
sort(a,a+len1);
sort(b,b+len2);
int sum=0;
if(len1>len2)
{
int t=len1-len2-1;
for(int i=0;i<t;i++)
sum+=a[i];
}
else
{
int t=len2-len1-1;
for(int i=0;i<t;i++)
sum+=b[i];
}
printf("%d\n",sum);
}
return 0;
}