分析
我们先来看看顺移,它可以使我们到达某一列的任意一个位置后,将这一列走完。
所以,我们就只需要考虑对于每一列选多少个。
由题目可以知道,凡是走过的列都至少选一个,
通过贪心的想法知道,对于值最大的那一列就应该全部选上的。
所以,我们先枚举一个i,表示我们走到的最右边的那一列。
那么除了每一列选的一个,还可以选k-i个,
也就是说,可以全选(k-i)/(n-1)下取整列,然后如果有剩余就再在没有被选中找最大的。
这些可以通过线段树来维护。
code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1000003
#define ll long long
using namespace std;
struct arr
{
ll x,s;
}tr[4*N];
int n,m,mx,tt;
int a[N];
ll num,s,ans,sum,t,k;
char ch;
void read(int &n)
{
n=0;
ch=getchar();
while(ch<'0' || ch>'9')ch=getchar();
while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=getchar();
}
void ins(int x,int l,int r,int y)
{
if(l==r)
{
tr[x].s+=y;
tr[x].x++;
return;
}
int m=(l+r)/2;
if(y<=m)ins(x+x,l,m,y);else ins(x+x+1,m+1,r,y);
tr[x].s=tr[x+x].s+tr[x+x+1].s;
tr[x].x=tr[x+x].x+tr[x+x+1].x;
}
ll find(int x,int l,int r,ll y)
{
if(tr[x].x==y)return tr[x].s;
if(l==r)return y*l;
int m=(l+r)/2;
if(tr[x+x+1].x>=y)return find(x+x+1,m+1,r,y);
else
{
return find(x+x,l,m,y-tr[x+x+1].x)+tr[x+x+1].s;
}
}
ll findk(int x,int l,int r,int y)
{
if(l==r)return l;
int m=(l+r)/2;
if(tr[x+x+1].x>=y)return findk(x+x+1,m+1,r,y);
else
{
return findk(x+x,l,m,y-tr[x+x+1].x);
}
}
int main()
{
read(n);read(m);scanf("%lld",&k);
for(int i=1;i<=m;i++)
read(a[i]),mx=max(a[i],mx);
if(n==1)
{
for(int i=1;i<=k;i++)
ans=ans+a[i];
printf("%lld",ans);
return 0;
}
if(m>k)tt=k;else tt=m;
for(int i=1;i<=tt;i++)
{
ins(1,1,mx,a[i]);
sum+=a[i];
num=floor((k-i)/(n-1));
if(num!=0 && num<=i)s=find(1,1,mx,num)*(n-1);else s=0;
t=num+1;
num=k-i-num*(n-1);
if(num>0)s+=findk(1,1,mx,t)*num;
ans=max(ans,sum+s);
}
printf("%lld",ans);
return 0;
}