贪心(优先队列)喝饮料

Potions (Easy Version)

 CodeForces - 1526C1 

This is the easy version of the problem. The only difference is that in this version n≤2000n≤2000. You can make hacks only if both versions of the problem are solved.

There are nn potions in a line, with potion 11 on the far left and potion nn on the far right. Each potion will increase your health by aiai when drunk. aiai can be negative, meaning that potion will decrease will health.

You start with 00 health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.

What is the largest number of potions you can drink?

Input

The first line contains a single integer nn (1≤n≤20001≤n≤2000) — the number of potions.

The next line contains nn integers a1a1, a2a2, ... ,anan (−109≤ai≤109−109≤ai≤109) which represent the change in health after drinking that potion.

Output

Output a single integer, the maximum number of potions you can drink without your health becoming negative.

Example

Input

6
4 -4 1 -3 1 -3

Output

5

Note

For the sample, you can drink 55 potions by taking potions 11, 33, 44, 55 and 66. It is not possible to drink all 66 potions because your health will go negative at some point

思路:

见了饮料就喝等总值为负值时就把肚子里最小的 

#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long> > q;
int main()
{
	long long n,m,j,i,sum=0,w,z=0;
	scanf("%lld",&w);
	for(i=1; i<=w; i++)
	{
		scanf("%lld",&n) ;
		z++;
		sum+=n;
		q.push(n);
		if(sum<0)
		{
			sum-=q.top();
			
			z--;
			q.pop();
		}
	}
	printf("%lld",z);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值