#include<bits/stdc++.h>
#define ll long long
#define rll register ll
#define F(i,a,b) for(rll i=a;i<=b;i++)
#define Fdn(i,a,b) for(rll i=a;i>=b;i--)
#define int ll
using namespace std;
const int inf = 0x3f3f3f3f,mod = 1e9 + 7;
const int maxn = 5e5 + 7;
struct data{
int sum, pre, suc, mx;
inline void Modify(int x, int sz){
sum = x * sz, pre = suc = mx = x < 0 ? x : sum;
}
data operator + (const data &x) const {
data y;
y.sum = sum + x.sum, y.pre = max(pre, sum + x.pre),
y.suc = max(x.suc, x.sum + suc),
y.mx = max(max(mx, x.mx), suc + x.pre);
return y;
}
}d[maxn], emp;
int root, tot, siz[maxn], val[maxn], rnd[maxn], ls[maxn], rs[maxn], tag[maxn];
int top, stk[maxn], md[maxn];
inline void PushUp(int p){
siz[p] = siz[ls[p]] + 1 + siz[rs[p]],
d[p] = d[ls[p]] + (data){val[p], val[p], val[p], val[p]} + d[rs[p]];
}
inline void PushDown(int p){
if(tag[p]){
tag[ls[p]] ^= 1, tag[rs[p]] ^= 1;
swap(d[ls[p]].pre, d[ls[p]].suc),
swap(d[rs[p]].pre, d[rs[p]].suc),
swap(ls[p], rs[p]);
tag[p] = 0;
}
if(md[p] != inf){
val[ls[p]] = val[rs[p]] = md[ls[p]] = md[rs[p]] = md[p],
d[ls[p]].Modify(md[p], siz[ls[p]]),
d[rs[p]].Modify(md[p], siz[rs[p]]);
md[p] = inf;
}
}
inline int New(int v){
int x = top ? stk[top--] : ++tot;
siz[x] = 1, val[x] = v, rnd[x] = rand(),
d[x] = (data){v, v, v, v}, md[x] = inf;
return x;
}
inline void DeleteNode(int x){
siz[x] = val[x] = rnd[x] = ls[x] = rs[x] = tag[x] = 0,
d[x] = emp, stk[++top] = x;
}
inline void Split(int p, int v, int &x, int &y){
if(!p){
x = y = 0;
return;
}
PushDown(p);
if(siz[ls[p]] >= v) y = p, Split(ls[p], v, x, ls[y]);
else x = p, Split(rs[p], v - siz[ls[p]] - 1, rs[x], y);
PushUp(p);
}
inline int Merge(int x, int y){
if(!x || !y) return x | y;
PushDown(x), PushDown(y);
if(rnd[x] > rnd[y]) {
rs[x] = Merge(rs[x], y), PushUp(x);
return x;
} else {
ls[y] = Merge(x, ls[y]), PushUp(y);
return y;
}
}
inline void Empty(int p){
if(!p) return;
Empty(ls[p]), Empty(rs[p]), DeleteNode(p);
}
inline void Insert(){
int pos, n, x = 0, y = 0, rt = 0, num;
cin >> pos >> n;
F(i, 1, n) {
cin >> num;
rt = Merge(rt, New(num));
}
Split(root, pos, x, y), Merge(Merge(x, rt), y);
}
inline void YOASOBI(int &x, int &y, int &z){
int pos, n;
cin >> pos >> n;
pos--;
Split(root, pos + n, x, z), Split(x, pos, x, y);
}
inline void Delete(){
int x, y, z;
YOASOBI(x, y, z);
Empty(y), root = Merge(x, z);
}
inline void Update(){
int x, y, z;
YOASOBI(x, y, z);
int v; cin >> v;
val[y] = md[y] = v, d[y].Modify(v, siz[y]), root = Merge(Merge(x, y), z);
}
inline void Reverse() {
int x, y, z;
YOASOBI(x, y, z);
tag[y] ^= 1, root = Merge(Merge(x, y), z);
}
inline void GetSum() {
int x, y, z;
YOASOBI(x, y, z);
cout << d[y].sum << '\n';
root = Merge(Merge(x, y), z);
}
int yousa, _;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
d[0] = emp = (data){0, -inf, -inf, -inf};
cin >> yousa >> _;
int num;
F(i, 1, yousa) {
cin >> num;
root = Merge(root, New(num));
}
string s;
while(_--){
cin >> s;
if(s == "INSERT") Insert();
else if(s == "DELETE") Delete();
else if(s == "MAKE-SAME") Update();
else if(s == "REVERSE") Reverse();
else if(s == "GET-SUM") GetSum();
else cout << d[root].mx << '\n';
}
return 0;
}```