对于第一问二分然后贪心判断即可
对于第二问,设f[i][j]为已经到j为止砍了i段,转移的话从$$ f[i][j]=sigema f[k][j-1] (s[j]-s[k-1]<=ans)
这里用权和嘴和优化成nm的即可
#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005,mod=10007;
int n,m,a[N],f[N],la[N];
long long sm[N],s[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool ok(int w)
{
int sum=0,s=1;
for(int i=1;i<=n;i++)
{
if(sum+a[i]>w)
sum=a[i],s++;
else
sum+=a[i];
}
return s<=m;
}
int main()
{
n=read(),m=read()+1;
int l=0,r=0,ans,ans2=0;
for(int i=1;i<=n;i++)
a[i]=read(),l=max(l,a[i]),r+=a[i],sm[i]=sm[i-1]+a[i];
while(l<=r)
{
int mid=(l+r)>>1;
if(ok(mid))
r=mid-1,ans=mid;
else
l=mid+1;
}
int las=0;
for(int i=1;i<=n;i++)
{
while(sm[i]-sm[las]>ans)
las++;
la[i]=las;
}
for(int i=0;i<=n;i++)
s[i]=1;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
f[j]=(s[j-1]-s[la[j]-1])%mod;
s[0]=0;
for(int j=1;j<=n;j++)
s[j]=s[j-1]+f[j];
ans2=(ans2+f[n])%mod;
}
printf("%d %d
",ans,ans2);
return 0;
}