This is the easy version of the problem. The only difference is that in this version n≤2000. 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 will increase your health by ai when drunk. ai can be negative, meaning that potion will decrease will health.
You start with 0 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 n (1≤n≤2000) — 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
6
4 -4 1 -3 1 -3
Output
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
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
using namespace std;
typedef long long ll;
ll a[110111],k,n;
ll judge(ll x)
{
ll sum=0;
for(ll i=0; i<n; i++)
{
if(a[i]>x)
sum+=(a[i]-x+k-2)/(k-1);
if(sum>x) return 0;
}
return 1;
}
void solve()
{
ll l=1,r=a[n-1];
while(l<=r)
{
ll mid=(l+r)>>1;
if(judge(mid))
r=mid-1;
else
l=mid+1;
}
cout<<l<<endl;
}
int main()
{
ll i,j;
cin>>n;
//cin输入慢于scanf
for(i=0; i<n; i++)
scanf("%lld",&a[i]);
cin>>k;
sort(a,a+n);
if(k==1)
{
cout<<a[n-1]<<endl;
return 0;
}
solve();
return 0;
}