C2. Potions (Hard Version)

time limit per test

1 second

memory limit per test

256 megabytes

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

There are n potions in a line, with potion 1 on the far left and potion n on the far right. Each potion increases your health by ai when drunk. ai can be negative, meaning that the potion decreases your health.

You start with 0 health, and you will walk from left to right, from the 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 n (1≤n≤200000) --- the number of potions.

The next line contains n integers a1, a2, ... ,an (−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

Copy

复制代码

6

4 -4 1 -3 1 -3

Output

Copy

复制代码
5

Note

For the sample, you can drink 5 potions by taking potions 1, 3, 4, 5 and 6. It is not possible to drink all 6 potions because your health will go negative at some point

解题说明:此题是一道模拟题,可以采用贪心算法,从左到右遍历,先全部喝掉。如果血量变负,就丢掉之前喝过的负收益最大的那瓶(因为丢掉它能让血量增加最多)。这里需要用到小根堆,用来维护的是已喝的负值药水,堆顶就是最该丢掉的那个。

cpp 复制代码
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
typedef long long ll;
priority_queue<int, vector<int>, greater<int> >pq;
int main() 
{
	ll n, s = 0, c = 0;
	cin >> n;
	while (n--)
	{
		int x;
		cin >> x;
		pq.push(x); 
		c += x;
		s++;
		if (c < 0)
		{ 
			c -= pq.top();
			pq.pop(); 
			s--; 
		}
	}
	cout << s<<endl;
	return 0;
}
相关推荐
weixin_307779137 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
AI wins7 小时前
c++新增頭文件
c++
Shadow(⊙o⊙)7 小时前
仿muduo实现高并发服务器——事件就绪管理Channel.hpp
服务器·c++
by209997 小时前
哈希表——快速找到数据:冲突、删除与扩容的实现
数据结构·c++·哈希算法·散列表
音草8 小时前
C++ 继承详解:从概念到菱形继承
c++
weixin_307779138 小时前
C++代码实现MATLAB中的spectralcluster函数功能
开发语言·c++·算法·matlab
xiaoye-duck9 小时前
《C++ 项目:手写大模型接入 SDK》Trae IDE 开发环境搭建与依赖管理
c++·ide·语言模型
沫璃染墨9 小时前
《Qt从零入门系列(十四):QThread多线程详解——从线程创建到客户端阻塞处理》
开发语言·c++·笔记·qt·ui·交互·线程
草莓熊Lotso9 小时前
【C++ 基于微服务的即时通讯系统】Elasticsearch 从安装到 C++ 客户端二次封装全落地
linux·网络·数据库·c++·elasticsearch·微服务
CHPCWWHSU9 小时前
FreeCAD 源码解读:从 Property 到 Command,看一套 CAD 内核的完整闭环
c++·cad