1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <map> #include <set> #include <cmath> #include <queue> #include <bitset> #include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second inline int read() { int x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } #define CASET fo(___,1,read()) const int M=40010; const int N=10005; const ll inf=1e18; namespace MCMF{ int ver[M],val[M],ne[M],cost[M],head[N],tot=1; int s,t; ll d[N]; int mi[N],pre[N]; bool vis[N]; queue<int> q; inline void add(int x,int y,int v,int c) { ver[++tot]=y; val[tot]=v; cost[tot]=c; ne[tot]=head[x]; head[x]=tot; ver[++tot]=x; val[tot]=0; cost[tot]=-c;ne[tot]=head[y]; head[y]=tot; } inline bool spfa() { fo(i,0,t) d[i]=-inf,vis[i]=pre[i]=mi[i]=0; d[s]=0; pre[s]=0; mi[s]=2e9; q.push(s); vis[s]=1; for(int u;!q.empty();) { u=q.front(); q.pop(); vis[u]=0; for(int i=head[u],v;i;i=ne[i]) if(val[i]&&d[v=ver[i]]<d[u]+cost[i]) { d[v]=d[u]+cost[i]; pre[v]=i; mi[v]=min(mi[u],val[i]); if(!vis[v]) q.push(v),vis[v]=1; } } return mi[t]; } inline ll work() { ll ans=0; while(spfa()) { ans+=1ll*d[t]*mi[t]; for(int v=t;v;v=ver[pre[v]^1]) val[pre[v]]-=mi[t],val[pre[v]^1]+=mi[t]; } return ans; } } ll sum; int n,k,t1,t2,a[N],b[N],pos[N]; int main() { n=read(); k=read(); t1=read(); t2=read(); fo(i,1,n) a[i]=read(); fo(i,1,n) b[i]=read(),sum+=b[i]; MCMF::s=0; MCMF::t=n-k+3; MCMF::add(MCMF::s,1,k-t2,0); MCMF::add(n-k+2,MCMF::t,k-t2,0); fo(i,1,n) MCMF::add(max(1,i-k+1),min(i+1,n-k+2),1,a[i]-b[i]),pos[i]=MCMF::head[max(1,i-k+1)]; fo(i,1,n-k+1) MCMF::add(i,i+1,k-t1-t2,0); printf("%lld\n",sum+MCMF::work()); fo(i,1,n) putchar(MCMF::val[pos[i]]?'E':'S'); return 0; }
|