HLD(vvi &childs, vi &p) : V(p.size()), T(0), p(p), childs(childs), pr(V,-1), size(V,-1), heavy(V,-1), t_in(V,-1), t_out(V,-1) { dfs(0); set_pr(0,0); }

SegmentTree(const vector<int> &_A){ A = _A; n = (int)A.size(); st.assign(4 * n, 0); build(1, 0, n-1); }

vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }

UnionFind(int n){ rank.resize(n,0); parent.resize(n); size.resize(n,1); for(int i=0;i<n;++i)parent[i]=i; }

SegmentTree(vi &_A){ size = (int)_A.size(); A = _A; tree.assign(size*4,0); build(1,0,size-1); }

rep(i,N){ int t; cin >> t; dist.pb(t); }

// print function // the number is stored in a vector with the 0th position being the least // significant 'bits' -> go from last to first and concatenate to print // the correct output void print(const vi &v) { for (vi::const_reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit) { (rit != v.rbegin()) ? printf("%04d", *rit) : printf("%d", *rit); } printf("\n"); }

SegmentTree(const vi &_A){ A = _A; n = (int) A.size(); st.assign(n * 4, 0); lazy.assign(n * 4, -1); build(1, 0, n - 1); }

bool contains(int N){ vi::iterator it = lower_bound(marks.begin(), marks.end(), N); if(*it == N)return true; return false; }

void update(int p, int val){ for(;p<ft.size();p += (p&(-p))){ ft[p] += val; } }

int main(){ FASTER; cin >> n >> q; vi W(3000000,0); ft.assign(6000000,0); for (int i = 0; i < n; ++i) { int t; cin >> t; v.push_back(t); W[t]++; if(query(t,t) == 0){ update(t,1); } } st1.assign(20000000,0); st2.assign(20000000,0); buildMax(1,0,v.size()-1); buildMin(1,0,v.size()-1); for (int i = 0; i < q; ++i) { char c; int x,y; cin >> c >>x >> y; if(c == 'Q'){ y--; int maxi = queryMax(1,0,v.size()-1, x,y); int mini = queryMin(1,0,v.size()-1, x,y); printf("range (%d,%d) = %lld %lld\n",mini,maxi, v[mini], v[maxi]); ll cnt = query(v[mini],v[maxi]); cout << cnt << endl; }else{ ll val = v[x]; if(W[val]){ printf("remove %lld\n", val); W[val]--; if(W[val] == 0 && query(val,val) == 1){ update(val,-1); } } W[y]++; if(query(y,y) == 0){ update(y,1); } updateMax(1,x,0,(int)v.size()-1,y); updateMin(1,x,0,(int)v.size()-1,y); } } return 0; }

void invf() { int i = 0, j = f.size()-1; for(; i<j; i++, j--) { swap(f[i], f[j]); } }

inline void make_set(vi& set,int size) { set.resize(size); rep(i,size)set[i]=i; }

void update(ll i){ for(;i<ft.size();i += (i &(-i))){ ft[i] += 1LL; } }

using vi = vector<int>; using vvi = vector<vi>; vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }

int main(){ scanf("%d %d %d %d",&n, &l, &x, &y); for (int i = 0,t; i < n; ++i) { scanf("%d", &t); marks.push_back(t); } // sort(mea.begin(), mea.end()); bool containsX = false,containsY = false; if(containsCut(x)){ containsX = true; } if(containsCut(y)){ containsY = true; } vi new_mark; // Solve both if(!containsX && !containsY){ for (int i = 0; i < n; ++i) { int u = marks[i]; // printf("u + x + y = %d\n",u + x + y); if(u + x + y <= l && contains(u + x + y)){ new_mark.push_back(u+x); containsX = containsY = true; break; } if(u - x - y >= 0 && contains(u - x - y)){ new_mark.push_back(u-x); containsX = containsY = true; break; } if(u + x <= l && contains(u + x - y)){ new_mark.push_back(u+x); containsX = containsY = true; break; } if(u + y <= l && contains(u + y - x)){ new_mark.push_back(u+y); containsX = containsY = true; break; } if(u - x >= 0 && contains(u - x + y)){ new_mark.push_back(u-x); containsX = containsY = true; break; } if(u - y >= l && contains(u - y + x)){ new_mark.push_back(u-y); containsX = containsY = true; break; } } } if(!containsX){ new_mark.push_back(x); } if(!containsY){ new_mark.push_back(y); } printf("%d", (int)new_mark.size()); for (int i = 0; i < new_mark.size(); ++i) { if(i)printf(" "); else printf("\n"); printf("%d",new_mark[i]); } printf("\n"); return 0; }

void st_create(vi &st, const vi &a) { int size = (int)(2 * pow(2.0, floor((log((double)a.size()) / log(2.0) + 1)))); st.assign(size, 0); st_build(st, a, 1, 0, (int)a.size() - 1); }

int main(){ FASTER; int n,m; while(cin >> n >> m, n || m){ A.assign(n+1,0); for (int i = 1; i <= n; ++i) { cin >> A[i]; } int s = 0; MEM(C,0); // pre-compute for (int i = 1; i <= n; ++i) { s = A[i]; for (int j = i+1; j <= n; ++j) { C[i][j] = s * A[j] + C[i][j-1]; s += A[j]; } } if(m == 0){ cout << C[1][n] << endl; continue; } MEM(dp,0); for (int i = 0; i <= n; ++i) for (int j = 0; j <= m+1; ++j) dp[i][j] = 1e9; // Slow // dp[0][0] = 0; // for (int i = 1; i <= n; ++i) { // for (int j = 1; j <= i; ++j) { // for (int k = 1; k <= i; ++ k) { // int tmp = dp[k-1][j-1] + C[k][i]; // dp[i][j] = min(dp[i][j], tmp); // } // } // } // cout << dp[n][m+1] << endl; // Fast dp[n+1][0] = 0; for (int i = n; i >= 1; --i) { for (int j = 1; j <= n; ++j) { for (int k = i; k <= n; ++k) { int tmp = dp[k+1][j-1] + C[i][k]; // optimization if(C[i][k] > dp[i][j] )break; dp[i][j] = min(dp[i][j], tmp); } } } cout << dp[1][m+1] << endl; } return 0; }