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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=1.5e3+10; const int Inf=0x3f3f3f3f; int N,D,a[MAXN],b[MAXN],Sum; int Dp[MAXN][MAXN]; struct Seg_tree { #define ls (p<<1) #define rs (p<<1|1) struct Segment { int l,r,val; }Tr[MAXN<<2]; inline void pushUp(int p) { Tr[p].val=std::min(Tr[ls].val,Tr[rs].val); } void build(int p,int l,int r) { Tr[p].l=l,Tr[p].r=r; if(l==r) return Tr[p].val=Inf,void(); int mid=(l+r)>>1; build(ls,l,mid),build(rs,mid+1,r); pushUp(p); } void modifyX(int p,int x,int v) { if(Tr[p].l==Tr[p].r) return checkMin(Tr[p].val,v),void(); int mid=(Tr[p].l+Tr[p].r)>>1; x<=mid?modifyX(ls,x,v):modifyX(rs,x,v); pushUp(p); } int queryMin(int p,int l,int r) { if(l<=Tr[p].l&&Tr[p].r<=r) return Tr[p].val; int mid=(Tr[p].l+Tr[p].r)>>1,res=Inf; if(l<=mid) checkMin(res,queryMin(ls,l,r)); if(mid<r) checkMin(res,queryMin(rs,l,r)); return res; } void modify(int x,int v){ modifyX(1,x,v); } int query(int l,int r){ return queryMin(1,l,r); } #undef ls #undef rs }Tra[MAXN],Trb[MAXN]; signed main() { read(N,D); for(int i=1;i<=N;++i) read(a[i]),Sum+=a[i]; for(int i=1;i<=N;++i) read(b[i]),Sum+=b[i]; std::memset(Dp,0x3f,sizeof(Dp)); Dp[0][0]=0; for(int i=0;i<=N;++i) Tra[i].build(1,0,N),Trb[i].build(1,0,N); Tra[0].modify(0,0),Trb[0].modify(0,0); for(int i=1;i<=N;++i) for(int j=std::max(0,i-D-1);j<=std::min(N,i+D);++j) { int l=std::max(0,i-D-1),r=std::min(N,i+D),k=j+(j+1==i); checkMin(Dp[k][r],Tra[j].query(l,r)+a[i]); Tra[k].modify(r,Dp[k][r]),Trb[r].modify(k,Dp[k][r]); checkMin(Dp[r][k],Trb[j].query(l,r)+b[i]); Trb[k].modify(r,Dp[r][k]),Tra[r].modify(k,Dp[r][k]); checkMin(Dp[r][r],Tra[j].query(l,r)+a[i]+b[i]), checkMin(Dp[r][r],Trb[j].query(l,r)+a[i]+b[i]); Tra[r].modify(r,Dp[r][r]),Trb[r].modify(r,Dp[r][r]); } write(Sum-Dp[N][N]); return 0; }
|