A - Parity Alternated Deletions

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
此题意思就是奇偶数相互抵消,最后求出剩余数的最小和。所以为了得到最小和,那我们在奇偶数抵消的过程中就要找大的数相互抵消,一种情况是如果奇数的个数和偶数的个数相等或只相差一,那么最后所有数都会被抵消掉,那么和就为零了,就不需要考虑其中数大小的关系了;另一种情况就是看是奇数个数多还是偶数的个数多,为了得到最小的和,那就把个数多的那个从小到大排序,并将最后消除后会剩余的个数求出来,并相加那么多个,就可以啦。
#include"stdio.h"
#include"algorithm"
using namespace std;
int main()
{
int n;
int a[2005];

while(~scanf("%d",&n))
{
	int sum1=0;
	int sum2=0;
	for(int i=0;i < n;i ++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i < n;i ++)
	{
		if(a[i]%2==0)
		{
			sum1 ++;
		}
		else
		{
			sum2 ++;
		}
	}
	int m=sum1-sum2;
	int l=sum2-sum1;
	int ss=0;
	int c=0;
	if(sum1==sum2||m==1||l==1)
   	{
   		printf("0\n");
   	}
   	else
	{
         	sort(a,a+n);
   		if(m > 0)
   		{
   			m--;
    	         	for(int i = 0;i < n;i ++)
    	         	{
	   	        	if(a[i] % 2 == 0)
	   	        	{
	    		        	ss+=a[i];
	    		        	m--;
	    	         	}
		           	if(m==0)
		         	{
	    		        	break;
	    	         	}
	         	}
	         	printf("%d\n",ss);
   		}
   		else if(l > 0)
   		{
	   		l--;
	         	for(int i = 0;i < n;i ++)
                        {
	    		         if(a[i] % 2 != 0)
	    		         {
		   			c+=a[i];
		    	         	l--;
		         	}
		         	if(l==0)
		          	{
	    			        break;
	    		        } 
	                 }
	         	printf("%d\n",c);
	   	}
        }
}
return 0;

}

你可能感兴趣的:(A,-,Parity,Alternated,Deletions)