RT,AC on #1
#include<bits/stdc++.h>
#define ls(x) tr[x].ls
#define rs(x) tr[x].rs
using namespace std;
mt19937 rnd(time(0));
const int N=5e5+5,inf=1e8;
int n,m,rt,sz,st[N],r;
struct FHQ_Treap{
int ls,rs,rd;
int sz,sw,w;
int lw,mxw,rw;
int fw;bool fr;
}tr[N];
inline void chgr(int x){
if(!x)return;
swap(tr[x].lw,tr[x].rw);
swap(ls(x),rs(x)),tr[x].fr^=1;
}
inline void chgw(int x,int w){
if(!x)return;
tr[x].sw=tr[x].sz*w,tr[x].fw=tr[x].w=w;
tr[x].lw=tr[x].mxw=tr[x].rw=(w<0?w:tr[x].sw);
}
inline void pushdown(int x){int w=tr[x].fw;
if(tr[x].fr)chgr(ls(x)),chgr(rs(x)),tr[x].fr=0;
if(tr[x].fw!=inf)chgw(ls(x),w),chgw(rs(x),w),tr[x].fw=inf;
}
inline void pushup(int x){
tr[x].sw=tr[ls(x)].sw+tr[x].w+tr[rs(x)].sw;
tr[x].lw=max(tr[ls(x)].sw+tr[x].w+tr[rs(x)].lw,tr[ls(x)].lw);
tr[x].rw=max(tr[rs(x)].sw+tr[x].w+tr[ls(x)].rw,tr[rs(x)].rw);
tr[x].mxw=max(max(tr[ls(x)].mxw,tr[rs(x)].mxw),tr[ls(x)].rw+tr[x].w+tr[rs(x)].lw);
tr[x].sz=tr[ls(x)].sz+tr[rs(x)].sz+1;
}
inline int makend(int w){
int x=(r?st[r--]:++sz);
tr[x].sz=1;tr[x].rd=rnd();tr[x].fw=inf;
tr[x].w=tr[x].lw=tr[x].mxw=tr[x].rw=tr[x].sw=w;
return x;
}
inline void split(int p,int k,int &x,int &y){
if(!p){x=y=0;return;}pushdown(p);
if(tr[ls(p)].sz<k)
split(rs(p),k-tr[ls(p)].sz-1,rs(x=p),y);
else split(ls(p),k,x,ls(y=p));pushup(p);
}
inline int merge(int x,int y){
if(!x||!y)return x|y;pushdown(x),pushdown(y);
if(tr[x].rd<tr[y].rd)return ls(y)=merge(x,ls(y)),pushup(y),y;
else return rs(x)=merge(rs(x),y),pushup(x),x;
}
inline void dfs(int x){
if(!x)return;dfs(ls(x)),dfs(rs(x));
st[++r]=x;tr[x]={0},tr[x].mxw=-inf;
}
inline void ins(int pos,int w){
int x,y;split(rt,pos,x,y);
rt=merge(x,merge(makend(w),y));
}
inline void del(int l,int r){
int x,y,z;split(rt,r,x,z);
split(x,l-1,x,y);dfs(y);rt=merge(x,z);
}
inline void upd(int l,int r,int w){
int x,y,z;
split(rt,r,x,z),split(x,l-1,x,y);
chgw(y,w),rt=merge(merge(x,y),z);
}
inline void revs(int l,int r){
int x,y,z;
split(rt,r,x,z),split(x,l-1,x,y);
chgr(y),rt=merge(merge(x,y),z);
}
inline void qry(int l,int r){
int x,y,z;
split(rt,r,x,z),split(x,l-1,x,y);
cout<<tr[y].sw<<'\n';
rt=merge(merge(x,y),z);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;int x;tr[0].mxw=-inf;
for(int i=1; i<=n; ++i)cin>>x,ins(i-1,x);
string op;int pos,tot,c;
while(m--){
cin>>op;
if(op=="INSERT"){cin>>pos>>tot;while(tot--)cin>>x,ins(pos++,x);}
if(op=="DELETE")cin>>pos>>tot,del(pos,pos+tot-1);
if(op=="MAKE-SAME")cin>>pos>>tot>>c,upd(pos,pos+tot-1,c);
if(op=="REVERSE")cin>>pos>>tot,revs(pos,pos+tot-1);
if(op=="GET-SUM")cin>>pos>>tot,qry(pos,pos+tot-1);
if(op=="MAX-SUM")cout<<tr[rt].mxw<<'\n';
}
return 0;
}