CodeForces 1144 B 贪心 水题

45 篇文章 0 订阅

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值