首先假设全睡觉,然后用费用流考虑平衡要求建立网络流
把1n的点看作是i-k+1k这一段的和,连接(i,i+k,1,e[i]-s[i]),表示把i改成吃饭,能对i~i+k-1这一段的点产生影响;然后连接(i,i+1,k-ms-me,0),这是除了限额外可以随便选的(i+k,i+1>n就连到t)
然后建立ss,向1~k点连(ss,i,inf,0),再连(s,ss,k-ms,0)限流
然后跑最大费用最大流即可
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=2005;
long long n,k,ma,mb,a[N],b[N],s,t,ss,h[N],cnt=1,dis[N],ans,p[N],fr[N];
bool v[N];
struct qwe
{
long long ne,no,to,va,c;
}e[N*50];
long long read()
{
long long 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;
}
void add(long long u,long long v,long long w,long long c)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].no=u;
e[cnt].to=v;
e[cnt].va=w;
e[cnt].c=c;
h[u]=cnt;
}
void ins(long long u,long long v,long long w,long long c)
{
add(u,v,w,c);
add(v,u,0,-c);
}
bool spfa()
{
for(long long i=s;i<=t;i++)
dis[i]=-1e9;
queue<long long>q;
q.push(s);
v[s]=1;
dis[s]=0;
while(!q.empty())
{
long long u=q.front();
q.pop();
v[u]=0;
for(long long i=h[u];i;i=e[i].ne)
if(e[i].va>0&&dis[e[i].to]<dis[u]+e[i].c)
{
dis[e[i].to]=dis[u]+e[i].c;
fr[e[i].to]=i;
if(!v[e[i].to])
{
v[e[i].to]=1;
q.push(e[i].to);
}
}
}
return dis[t]!=-1e9;
}
void mcf()
{
long long x=1e9;
for(long long i=fr[t];i;i=fr[e[i].no])
x=min(x,e[i].va);
for(long long i=fr[t];i;i=fr[e[i].no])
{
e[i].va-=x;
e[i^1].va+=x;
ans+=e[i].c*x;
}
}
int main()
{
n=read(),k=read(),ma=read(),mb=read();
s=0,ss=n+1,t=n+2;
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=n;i++)
b[i]=read();
for(long long i=1;i<=n;i++)
{
ans+=a[i];
if(i+1<=n)
ins(i,i+1,k-ma-mb,0);
else
ins(i,t,k-ma-mb,0);
if(i+k<=n)
ins(i,i+k,1,b[i]-a[i]);
else
ins(i,t,1,b[i]-a[i]);
p[i]=cnt;
if(i<=k)
ins(ss,i,1e9,0);
}
ins(s,ss,k-ma,0);
while(spfa())
mcf();
printf("%lld
",ans);
for(long long i=1;i<=n;i++)
printf(e[p[i]].va?"E":"S");
return 0;
}