细心题
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define ms(arr,a) memset(arr,a,sizeof arr)
#define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
typedef long long ll;
const int maxn=1e5+5;
int n;
ll g,p;
int a[maxn];
ll dp[maxn],sum[maxn];
bool ok1(ll m)
{
if(m>=g)return true;
int tmp=min(ll(n),p);
for(int i=0;i<tmp;++i)
{
m=m+a[i]<=0?0:m+a[i];
if(m>=g)return true;
}
return false;
}
bool ok2(ll m)
{
for(int i=0;i<n;++i)
{
m=m+a[i]<=0?0:m+a[i];
if(m>=g)return true;
}
ll gain,gg=0;
if(sum[n-1]<=0)
{
int tmp=p<2*n?p-n:n;
gain=dp[tmp-1];
}
else
{
if(p<2*n)gain=dp[p-n-1];
else
{
int tmp=p%n?p%n+n:n;
if(((g-m)/sum[n-1])<=((p-n-tmp)/n))return true;
gain=(p-n-tmp)/n*sum[n-1];
gg=max(dp[tmp%n-1]+sum[n-1],dp[n-1]);
}
}
if(m+gain>=g-gg)return true;
return false;
}
int main()
{
//freopen("Input.txt","r",stdin);
//freopen("1.txt","w",stdout);
int T;scanf("%d",&T);
for(int Case=1;Case<=T;++Case)
{
scanf("%d%lld%lld",&n,&g,&p);
for(int i=0;i<n;++i)scanf("%d",a+i);
sum[0]=a[0];dp[0]=a[0]>0?a[0]:0;
for(int i=1;i<n;++i)
{
sum[i]=sum[i-1]+a[i];
dp[i]=sum[i]>dp[i-1]?sum[i]:dp[i-1];
}
if(p<=n)
{
ll l=0,m,r=g;
while(l<r)
{
m=(l+r)/2;
if(ok1(m))r=m;
else l=m+1;
}
printf("Case #%d: %lld
",Case,l);
}
else
{
ll l=0,m,r=g;
while(l<r)
{
m=(l+r)/2;
if(ok2(m))r=m;
else l=m+1;
}
printf("Case #%d: %lld
",Case,l);
}
}
//freopen("CON","w",stdout);
//system("start 1.txt");
}