## 题解 P2201 【数列编辑器】

#include "iostream"
#include "stdio.h"
#include "algorithm"
#define II int
#define R register
#define U NULL
#define I 1000010
using namespace std;

struct node {
II o,sum,wei;
node *up,*next;
};
node * begin(R II x)
{
node * ret=new node;
ret->up=U;
ret->next=U;
ret->o=x;
ret->sum=0;
return ret;
}
node *root=begin(0),*now,*en;

II ans[I];

void ppp(node *pi)
{
pi->wei=pi->up->wei+1;
pi->sum=pi->up->sum+pi->o;
ans[pi->wei]=max(ans[pi->wei-1],pi->sum);
}

{
R II x;
scanf("%d",&x);
R node *pi=begin(x);
R node *qian=now->up;
now->up=pi;
pi->next=now;
pi->up=qian;
qian->next=pi;
ppp(pi);
ppp(now);
}

void del()
{
R node *de=now->up;
R node *qian=de->up;
qian->next=now;
now->up=qian;
ppp(now);
}

void just()
{
now=now->up;
ppp(now);
}

void back()
{
now=now->next;
ppp(now);
}

void query()
{
R II k;
scanf("%d",&k);
printf("%d\n",ans[k]);
}

int main()
{
//    freopen("1.in","r",stdin);

R II n; R char a;
scanf("%d",&n);
now=begin(0);
now->up=root; now->next=root;
root->up=now;
root->next=now;
ans[0]=-1e9;

while (n--) {
cin>>a;
}