Codeforces Round #550 (Div. 3) B. Parity Alternated Deletions【贪心】

B. 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

Examples output

0

 

Examples input

6
5 1 2 4 6 3

Examples output

0

 

Examples input

2
1000000 1000000

Examples output

1000000

题意

给定有n个元素的数组,你可以按照奇数-偶数-奇数...或者偶数-奇数-偶数...的顺序删除数组中元素,直至不能再删,问最后留下的元素的和的最小值

思路

将奇数和偶数分别存放在两个向量中,然后对两个向量按从小到大排序,由于要求最小值,因此我们优先删除较大的奇数和偶数,奇数-偶数-奇数...和偶数-奇数-偶数...的顺序可以分别多删除一个奇数和偶数,因此求出两种顺序下的最小值,最后的结果就是两者中的较小者。

C++代码

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

vector<int>odd,even;

int main()
{
	int n,x;
	while(~scanf("%d",&n))
	{
		odd.clear();
		even.clear();
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			if(x&1)
			  odd.push_back(x);
			else
			  even.push_back(x);
		}
		sort(odd.begin(),odd.end());
		sort(even.begin(),even.end());
		int size=min(odd.size(),even.size());
		//先奇数(可多删除一个奇数)
		//注意对于向量x,x.size()返回的是无符号数,
		//如果它减去一个大于大的数就会溢出,因此要强制类型转换。
		int sum1=0;
		for(int j=0;j<(int)odd.size()-size-1;j++)
			sum1+=odd[j];
		for(int j=0;j<(int)even.size()-size;j++)
			sum1+=even[j];
		//先偶数(可多删除一个偶数) 
		int sum2=0;
		for(int j=0;j<(int)even.size()-size-1;j++) 
			sum2+=even[j];
		for(int j=0;j<(int)odd.size()-size;j++)
			sum2+=odd[j];
		printf("%d\n",min(sum1,sum2));
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值