#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>

const int maxn=1e5+7;

using namespace std;

int n,m,x,op,y,k;

struct rec{
	int l,r,s;
};

rec operator + (rec x,rec y)
{
	return (rec){x.l,y.r,x.s+y.s};
}

bool operator < (rec x,rec y)
{
	return x.s<y.s;
}

struct node{
	rec smax,smin,lmax,rmax,lmin,rmin,sum;
	int rev;
}t[maxn*4];

queue <node> q;

void neww(int p,int l,int k)
{
	t[p].smax=(rec){l,l,k};
	t[p].lmax=(rec){l,l,k};
	t[p].rmax=(rec){l,l,k};
	t[p].smin=(rec){l,l,k};
	t[p].lmin=(rec){l,l,k};
	t[p].rmin=(rec){l,l,k};
	t[p].sum=(rec){l,l,k};
}

node merge(node x,node y)
{
	node z;
	z.smax=max(x.smax,y.smax);
	z.smax=max(z.smax,x.rmax+y.lmax);
	z.smin=min(x.smin,y.smin);
	z.smin=min(z.smin,x.rmin+y.lmin);
	z.lmax=max(x.lmax,x.sum+y.lmax);
	z.rmax=max(y.rmax,x.rmax+y.sum);
	z.lmin=min(x.lmin,x.sum+y.lmin);
	z.rmin=min(y.rmin,x.rmin+y.sum);
	z.sum=x.sum+y.sum;
	z.rev=0;
	return z;
}

void clean(int p)
{
	swap(t[p].smax,t[p].smin);
	swap(t[p].lmax,t[p].lmin);
	swap(t[p].rmax,t[p].rmin);
	t[p].smax.s*=-1;
	t[p].smin.s*=-1;
	t[p].lmax.s*=-1;
	t[p].lmin.s*=-1;
	t[p].rmax.s*=-1;
	t[p].rmin.s*=-1;
	t[p].sum.s*=-1;
	t[p].rev^=1;
}

void change(int p,int l,int r,int x,int k)
{
	if (l==r)
	{
		neww(p,l,k);
		return;
	}
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (x<=mid) change(p*2,l,mid,x,k);
	       else change(p*2+1,mid+1,r,x,k);
	t[p]=merge(t[p*2],t[p*2+1]);
}

void rev(int p,int l,int r,int x,int y)
{
	if ((l==x) && (r==y))
	{
		clean(p);
		return;
	}
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (y<=mid) rev(p*2,l,mid,x,y);
    else if (x>mid) rev(p*2+1,mid+1,r,x,y);
	else
	{
		rev(p*2,l,mid,x,mid);
		rev(p*2+1,mid+1,r,mid+1,y);
	}
	t[p]=merge(t[p*2],t[p*2+1]);
}

node query(int p,int l,int r,int x,int y)
{
	if ((l==x) && (r==y)) return t[p];
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (y<=mid) return query(p*2,l,mid,x,y);
	else if (x>mid) return query(p*2+1,mid+1,r,x,y);
	else
	{
		return merge(query(p*2,l,mid,x,mid),query(p*2+1,mid+1,r,mid+1,y));
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		change(1,1,n,i,x);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&op);
		if (op==0)
		{
			scanf("%d%d",&x,&k);
			change(1,1,n,x,k);
		}
		else
		{
			scanf("%d%d%d",&x,&y,&k);
			int ans=0;
			while (k--)
			{
				node d=query(1,1,n,x,y);
				if (d.smax.s<0) break;
				ans+=d.smax.s;
				rev(1,1,n,d.smax.l,d.smax.r);
				q.push(d);
			}
			printf("%d\n",ans);
			while (!q.empty())
			{
				node d=q.front();
				q.pop();
				rev(1,1,n,d.smax.l,d.smax.r);
			}
		}
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>

const int maxn=1e5+7;

using namespace std;

int n,m,x,op,y,k;

struct rec{
	int l,r,s;
};

rec operator + (rec x,rec y)
{
	return (rec){x.l,y.r,x.s+y.s};
}

bool operator < (rec x,rec y)
{
	return x.s<y.s;
}

struct node{
	rec smax,smin,lmax,rmax,lmin,rmin,sum;
	int rev;
}t[maxn*2];

queue <node> q;

void neww(int p,int l,int k)
{
	t[p].smax=(rec){l,l,k};
	t[p].lmax=(rec){l,l,k};
	t[p].rmax=(rec){l,l,k};
	t[p].smin=(rec){l,l,k};
	t[p].lmin=(rec){l,l,k};
	t[p].rmin=(rec){l,l,k};
	t[p].sum=(rec){l,l,k};
}

node merge(node x,node y)
{
	node z;
	z.smax=max(x.smax,y.smax);
	z.smax=max(z.smax,x.rmax+y.lmax);
	z.smin=min(x.smin,y.smin);
	z.smin=min(z.smin,x.rmin+y.lmin);
	z.lmax=max(x.lmax,x.sum+y.lmax);
	z.rmax=max(y.rmax,x.rmax+y.sum);
	z.lmin=min(x.lmin,x.sum+y.lmin);
	z.rmin=min(y.rmin,x.rmin+y.sum);
	z.sum=x.sum+y.sum;
	z.rev=0;
	return z;
}

void clean(int p)
{
	swap(t[p].smax,t[p].smin);
	swap(t[p].lmax,t[p].lmin);
	swap(t[p].rmax,t[p].rmin);
	t[p].smax.s*=-1;
	t[p].smin.s*=-1;
	t[p].lmax.s*=-1;
	t[p].lmin.s*=-1;
	t[p].rmin.s*=-1;
	t[p].rmin.s*=-1;
	t[p].rev^=1;
}

void change(int p,int l,int r,int x,int k)
{
	if (l==r)
	{
		neww(p,l,k);
		return;
	}
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (x<=mid) change(p*2,l,mid,x,k);
	       else change(p*2+1,mid+1,r,x,k);
	t[p]=merge(t[p*2],t[p*2+1]);
}

void rev(int p,int l,int r,int x,int y)
{
	if ((l==x) && (r==y))
	{
		clean(p);
		return;
	}
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (y<=mid) rev(p*2,l,mid,x,y);
    else if (x>mid) rev(p*2+1,mid+1,r,x,y);
	else
	{
		rev(p*2,l,mid,x,mid);
		rev(p*2+1,mid+1,r,mid+1,y);
	}
	t[p]=merge(t[p*2],t[p*2+1]);
}

node query(int p,int l,int r,int x,int y)
{
	if ((l==x) && (r==y)) return t[p];
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (y<=mid) return query(p*2,l,mid,x,y);
	else if (x>mid) return query(p*2+1,mid+1,r,x,y);
	else
	{
		return merge(query(p*2,l,mid,x,mid),query(p*2+1,mid+1,r,mid+1,y));
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		change(1,1,n,i,x);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&op);
		if (op==0)
		{
			scanf("%d%d",&x,&k);
			change(1,1,n,x,k);
		}
		else
		{
			scanf("%d%d%d",&x,&y,&k);
			int ans=0;
			while (k--)
			{
				node d=query(1,1,n,x,y);
				if (d.smax.s<0) break;
				ans+=d.smax.s;
				rev(1,1,n,d.smax.l,d.smax.r);
				q.push(d);
			}
			printf("%d\n",ans);
			while (!q.empty())
			{
				node d=q.front();
				q.pop();
				rev(1,1,n,d.smax.l,d.smax.r);
			}
		}
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>

const int maxn=1e5+7;

using namespace std;

int n,m,x,op,y,k;

struct rec{
	int l,r,s;
};

rec operator + (rec x,rec y)
{
	return (rec){x.l,y.r,x.s+y.s};
}

bool operator < (rec x,rec y)
{
	return x.s<y.s;
}

struct node{
	rec smax,smin,lmax,rmax,lmin,rmin,sum;
	int rev;
}t[maxn*2];

queue <node> q;

void neww(int p,int l,int k)
{
	t[p].smax=(rec){l,l,k};
	t[p].lmax=(rec){l,l,k};
	t[p].rmax=(rec){l,l,k};
	t[p].smin=(rec){l,l,k};
	t[p].lmin=(rec){l,l,k};
	t[p].rmin=(rec){l,l,k};
	t[p].sum=(rec){l,l,k};
}

node merge(node x,node y)
{
	node z;
	z.smax=max(x.smax,y.smax);
	z.smax=max(z.smax,x.rmax+y.lmax);
	z.smin=min(x.smin,y.smin);
	z.smin=min(z.smin,x.rmin+y.lmin);
	z.lmax=max(x.lmax,x.sum+y.lmax);
	z.rmax=max(y.rmax,x.rmax+y.sum);
	z.lmin=min(x.lmin,x.sum+y.lmin);
	z.rmin=min(y.rmin,x.rmin+y.sum);
	z.sum=x.sum+y.sum;
	z.rev=0;
	return z;
}

void clean(int p)
{
	swap(t[p].smax,t[p].smin);
	swap(t[p].lmax,t[p].lmin);
	swap(t[p].rmax,t[p].rmin);
	t[p].smax.s*=-1;
	t[p].smin.s*=-1;
	t[p].lmax.s*=-1;
	t[p].lmin.s*=-1;
	t[p].rmax.s*=-1;
	t[p].rmin.s*=-1;
	t[p].sum.s*=-1;
	t[p].rev^=1;
}

void change(int p,int l,int r,int x,int k)
{
	if (l==r)
	{
		neww(p,l,k);
		return;
	}
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (x<=mid) change(p*2,l,mid,x,k);
	       else change(p*2+1,mid+1,r,x,k);
	t[p]=merge(t[p*2],t[p*2+1]);
}

void rev(int p,int l,int r,int x,int y)
{
	if ((l==x) && (r==y))
	{
		clean(p);
		return;
	}
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (y<=mid) rev(p*2,l,mid,x,y);
    else if (x>mid) rev(p*2+1,mid+1,r,x,y);
	else
	{
		rev(p*2,l,mid,x,mid);
		rev(p*2+1,mid+1,r,mid+1,y);
	}
	t[p]=merge(t[p*2],t[p*2+1]);
}

node query(int p,int l,int r,int x,int y)
{
	if ((l==x) && (r==y)) return t[p];
	int mid=(l+r)/2;
	if (t[p].rev)
	{
		clean(p*2);
		clean(p*2+1);
		t[p].rev^=1;
	}
	if (y<=mid) return query(p*2,l,mid,x,y);
	else if (x>mid) return query(p*2+1,mid+1,r,x,y);
	else
	{
		return merge(query(p*2,l,mid,x,mid),query(p*2+1,mid+1,r,mid+1,y));
	}
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		change(1,1,n,i,x);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d",&op);
		if (op==0)
		{
			scanf("%d%d",&x,&k);
			change(1,1,n,x,k);
		}
		else
		{

			scanf("%d%d%d",&x,&y,&k);
			int ans=0;
			while (k--)
			{
				node d=query(1,1,n,x,y);
				if (d.smax.s<0) break;
				ans+=d.smax.s;
				rev(1,1,n,d.smax.l,d.smax.r);
				q.push(d);
			}
			printf("%d\n",ans);
			while (!q.empty())
			{
				node d=q.front();
				q.pop();
				rev(1,1,n,d.smax.l,d.smax.r);
			}
		}
	}
}


#include <cstdio>
#include <cassert>
#define rep(i,a,b) for(int i=a,i##_END_=b;i<i##_END_;++i)
const int N=(int)1e5;
int n,m,a[N+5];
void input(){
	scanf("%d",&n);
	rep(i,1,n+1)scanf("%d",&a[i]);
	scanf("%d",&m);
}
inline int max(int x,int y){
	if(x>y)return x;
	return y;
}
inline int min(int x,int y){
	if(x<y)return x;
	return y;
}
inline void swap(int &x,int &y){
	x^=y;y^=x;x^=y;
}
struct node{
	int pre_mx,pre_mxr,suf_mx,suf_mxl;
	int sub_mx,sub_mxl,sub_mxr;

	int pre_mi,pre_mir,suf_mi,suf_mil;
	int sub_mi,sub_mil,sub_mir;

	int sum,l,r;

	void print(){
		printf("pre_mx %d suf_mx %d sub_mx %d\n",pre_mx,suf_mx,sub_mx);
		printf("sum %d l %d r %d\n",sum,l,r);
	}
	void init(int pos,int val){
		pre_mx=val,pre_mxr=pos,suf_mx=val,suf_mxl=pos;
		sub_mx=val,sub_mxl=pos,sub_mxr=pos;

		pre_mi=val,pre_mir=pos,suf_mi=val,suf_mil=pos;
		sub_mi=val,sub_mil=pos,sub_mir=pos;

		sum=val,l=pos,r=pos;
	}
	void reverse(){
		swap(pre_mx,pre_mi);pre_mx=-pre_mx;pre_mi=-pre_mi;swap(pre_mxr,pre_mir);
		swap(suf_mx,suf_mi);suf_mx=-suf_mx;suf_mi=-suf_mi;swap(suf_mxl,suf_mil);

		swap(sub_mx,sub_mi);sub_mx=-sub_mx;sub_mi=-sub_mi;
		swap(sub_mxl,sub_mil);swap(sub_mxr,sub_mir);

		sum=-sum;
	}
};
struct Segment_Tree{
	node tree[N*4+5];
	bool rev[N*4+5];
	inline void modify(int p){
		rev[p]^=1;
		tree[p].reverse();
	}

	inline node push_up(const node x,const node y){
		node res;

		if(x.pre_mx>x.sum+y.pre_mx)res.pre_mx=x.pre_mx,res.pre_mxr=x.pre_mxr;
		else res.pre_mx=x.sum+y.pre_mx,res.pre_mxr=y.pre_mxr;
		if(y.suf_mx>x.suf_mx+y.sum)res.suf_mx=y.suf_mx,res.suf_mxl=y.suf_mx;
		else res.suf_mx=x.suf_mx+y.sum,res.suf_mxl=x.suf_mxl;
		if(x.suf_mx+y.pre_mx>max(x.sub_mx,y.sub_mx))res.sub_mx=x.suf_mx+y.pre_mx,res.sub_mxl=x.suf_mxl,res.sub_mxr=y.pre_mxr;
		else if(x.sub_mx>y.sub_mx)res.sub_mx=x.sub_mx,res.sub_mxl=x.sub_mxl,res.sub_mxr=x.sub_mxr;
		else res.sub_mx=y.sub_mx,res.sub_mxl=y.sub_mxl,res.sub_mxr=y.sub_mxr;

		if(x.pre_mi<x.sum+y.pre_mi)res.pre_mi=x.pre_mi,res.pre_mir=x.pre_mir;
		else res.pre_mi=x.sum+y.pre_mi,res.pre_mir=y.pre_mir;
		if(y.suf_mi<x.suf_mi+y.sum)res.suf_mi=y.suf_mi,res.suf_mil=y.suf_mi;
		else res.suf_mi=x.suf_mi+y.sum,res.suf_mil=x.suf_mil;
		if(x.suf_mi+y.pre_mi<min(x.sub_mi,y.sub_mi))res.sub_mi=x.suf_mi+y.pre_mi,res.sub_mil=x.suf_mil,res.sub_mir=y.pre_mir;
		else if(x.sub_mi<y.sub_mi)res.sub_mi=x.sub_mi,res.sub_mil=x.sub_mil,res.sub_mir=x.sub_mir;
		else res.sub_mi=y.sub_mi,res.sub_mil=y.sub_mil,res.sub_mir=y.sub_mir;

		res.sum=x.sum+y.sum;
		res.l=x.l;res.r=y.r;

		return res;
	}
	inline void push_down(int p){
		if(rev[p]){
			modify(p<<1);
			modify(p<<1|1);
			rev[p]=false;
		}
	}
	void build(int L,int R,int p){
		rev[p]=false;
		if(L==R){
			tree[p].init(L,a[L]);
			return;
		}

		int mid=(L+R)>>1;
		build(L,mid,p<<1);
		build(mid+1,R,p<<1|1);

		tree[p]=push_up(tree[p<<1],tree[p<<1|1]);
//		puts("_____________");
//		tree[p].print();
//		printf("[ %d , %d ] sub_mx %d sum %d lsum %d rsum %d\n",L,R,tree[p].sub_mx,tree[p].sum,tree[p<<1].sum,tree[p<<1|1].sum);
	}
	void update(int L,int R,int pos,int val,int p){
		if(L==R){
			tree[p].init(pos,val);
			return;
		}

		push_down(p);

		int mid=(L+R)>>1;
		if(pos<=mid)update(L,mid,pos,val,p<<1);
		else update(mid+1,R,pos,val,p<<1|1);

		tree[p]=push_up(tree[p<<1],tree[p<<1|1]);
	}
	node query(int L,int R,int l,int r,int p){
		if(L==l&&R==r)return tree[p];

		push_down(p);

		int mid=(L+R)>>1;
		if(r<=mid)return query(L,mid,l,r,p<<1);
		else if(mid<l)return query(mid+1,R,l,r,p<<1|1);
		else return push_up(query(L,mid,l,mid,p<<1),query(mid+1,R,mid+1,r,p<<1|1));
	}
	void reverse(int L,int R,int l,int r,int p){
//		printf("[ %d , %d ] [ %d , %d ]\n",L,R,l,r);
		if(L==l&&R==r){
			modify(p);
			return;
		}

		push_down(p);

		int mid=(L+R)>>1;
		if(r<=mid)reverse(L,mid,l,r,p<<1);
		else if(mid<l)reverse(mid+1,R,l,r,p<<1|1);
		else reverse(L,mid,l,mid,p<<1),reverse(mid+1,R,mid+1,r,p<<1|1);

		tree[p]=push_up(tree[p<<1],tree[p<<1|1]);
	}
}sgt;
int sl[25],sr[25];
void response(){
	int flag;scanf("%d",&flag);
	if(flag==0){
		int pos,val;
		scanf("%d %d",&pos,&val);
		sgt.update(1,n,pos,val,1);
	}else if(flag==1){
		int l,r,k;
		scanf("%d %d %d",&l,&r,&k);
		int res=0,tot=0;
		rep(i,0,k){
			node now=sgt.query(1,n,l,r,1);
			if(now.sub_mx<=0)break;
			res+=now.sub_mx;
			sl[tot]=now.sub_mxl;
			sr[tot]=now.sub_mxr;
//			printf("response reverse [ %d , %d ]\n",sl[tot],sr[tot]);
			sgt.reverse(1,n,sl[tot],sr[tot],1);
			tot++;
		}
		printf("%d\n",res);
		rep(i,0,tot)
			sgt.reverse(1,n,sl[i],sr[i],1);
	}else assert(false);
}
int main(){
	input();
	sgt.build(1,n,1);
	while(m--)response();
	return 0;
}


#include <cstdio>
#include <cassert>
#define rep(i,a,b) for(int i=a,i##_END_=b;i<i##_END_;++i)
const int N=(int)1e5;
int n,m,a[N+5];
void input(){
	scanf("%d",&n);
	rep(i,1,n+1)scanf("%d",&a[i]);
	scanf("%d",&m);
}
inline int max(int x,int y){
	if(x>y)return x;
	return y;
}
inline int min(int x,int y){
	if(x<y)return x;
	return y;
}
inline void swap(int &x,int &y){
	x^=y;y^=x;x^=y;
}
struct node{
	int pre_mx,pre_mxr,suf_mx,suf_mxl;
	int sub_mx,sub_mxl,sub_mxr;

	int pre_mi,pre_mir,suf_mi,suf_mil;
	int sub_mi,sub_mil,sub_mir;

	int sum,l,r;

	void print(){
		printf("pre_mx %d suf_mx %d sub_mx %d\n",pre_mx,suf_mx,sub_mx);
		printf("sum %d l %d r %d\n",sum,l,r);
	}
	void init(int pos,int val){
		pre_mx=suf_mx=sub_mx=val;
		pre_mxr=suf_mxl=sub_mxl=sub_mxr=pos;

		pre_mi=suf_mi=sub_mi=val;
		pre_mir=suf_mil=sub_mil=sub_mir=pos;

		sum=val,l=r=pos;
	}
	void reverse(){
		swap(pre_mx,pre_mi);pre_mx=-pre_mx;pre_mi=-pre_mi;swap(pre_mxr,pre_mir);
		swap(suf_mx,suf_mi);suf_mx=-suf_mx;suf_mi=-suf_mi;swap(suf_mxl,suf_mil);

		swap(sub_mx,sub_mi);sub_mx=-sub_mx;sub_mi=-sub_mi;
		swap(sub_mxl,sub_mil);swap(sub_mxr,sub_mir);

		sum=-sum;
	}
};
struct Segment_Tree{
	node tree[N*4+5];
	bool rev[N*4+5];
	inline void modify(int p){
		rev[p]^=1;
		tree[p].reverse();
	}

	inline node push_up(const node x,const node y){
		node res;

		if(x.pre_mx>x.sum+y.pre_mx)res.pre_mx=x.pre_mx,res.pre_mxr=x.pre_mxr;
		else res.pre_mx=x.sum+y.pre_mx,res.pre_mxr=y.pre_mxr;
		if(y.suf_mx>x.suf_mx+y.sum)res.suf_mx=y.suf_mx,res.suf_mxl=y.suf_mxl;
		else res.suf_mx=x.suf_mx+y.sum,res.suf_mxl=x.suf_mxl;
		if(x.suf_mx+y.pre_mx>max(x.sub_mx,y.sub_mx))res.sub_mx=x.suf_mx+y.pre_mx,res.sub_mxl=x.suf_mxl,res.sub_mxr=y.pre_mxr;
		else if(x.sub_mx>y.sub_mx)res.sub_mx=x.sub_mx,res.sub_mxl=x.sub_mxl,res.sub_mxr=x.sub_mxr;
		else res.sub_mx=y.sub_mx,res.sub_mxl=y.sub_mxl,res.sub_mxr=y.sub_mxr;

		if(x.pre_mi<x.sum+y.pre_mi)res.pre_mi=x.pre_mi,res.pre_mir=x.pre_mir;
		else res.pre_mi=x.sum+y.pre_mi,res.pre_mir=y.pre_mir;
		if(y.suf_mi<x.suf_mi+y.sum)res.suf_mi=y.suf_mi,res.suf_mil=y.suf_mil;
		else res.suf_mi=x.suf_mi+y.sum,res.suf_mil=x.suf_mil;
		if(x.suf_mi+y.pre_mi<min(x.sub_mi,y.sub_mi))res.sub_mi=x.suf_mi+y.pre_mi,res.sub_mil=x.suf_mil,res.sub_mir=y.pre_mir;
		else if(x.sub_mi<y.sub_mi)res.sub_mi=x.sub_mi,res.sub_mil=x.sub_mil,res.sub_mir=x.sub_mir;
		else res.sub_mi=y.sub_mi,res.sub_mil=y.sub_mil,res.sub_mir=y.sub_mir;

		res.sum=x.sum+y.sum;
		res.l=x.l;res.r=y.r;

		return res;
	}
	inline void push_down(int p){
		if(rev[p]){
			modify(p<<1);
			modify(p<<1|1);
			rev[p]=false;
		}
	}
//	inline void push_down(int p){
//		if(rev[p]^rev[p<<1])modify(p<<1);
//		if(rev[p]^rev[p<<1|1])modify(p<<1|1);
//	}
	void build(int L,int R,int p){
		rev[p]=false;
		if(L==R){
			tree[p].init(L,a[L]);
			return;
		}

		int mid=(L+R)>>1;
		build(L,mid,p<<1);
		build(mid+1,R,p<<1|1);

		tree[p]=push_up(tree[p<<1],tree[p<<1|1]);
//		puts("_____________");
//		tree[p].print();
//		printf("[ %d , %d ] sub_mx %d sum %d lsum %d rsum %d\n",L,R,tree[p].sub_mx,tree[p].sum,tree[p<<1].sum,tree[p<<1|1].sum);
	}
	void update(int L,int R,int pos,int val,int p){
		if(L==R){
			tree[p].init(pos,val);
			return;
		}

		push_down(p);

		int mid=(L+R)>>1;
		if(pos<=mid)update(L,mid,pos,val,p<<1);
		else update(mid+1,R,pos,val,p<<1|1);

		tree[p]=push_up(tree[p<<1],tree[p<<1|1]);
	}
	node query(int L,int R,int l,int r,int p){
		if(L==l&&R==r)return tree[p];

		push_down(p);

		int mid=(L+R)>>1;
		if(r<=mid)return query(L,mid,l,r,p<<1);
		else if(mid<l)return query(mid+1,R,l,r,p<<1|1);
		else return push_up(query(L,mid,l,mid,p<<1),query(mid+1,R,mid+1,r,p<<1|1));
	}
	void reverse(int L,int R,int l,int r,int p){
//		printf("[ %d , %d ] [ %d , %d ]\n",L,R,l,r);
		if(L==l&&R==r){
			modify(p);
			return;
		}

		push_down(p);

		int mid=(L+R)>>1;
		if(r<=mid)reverse(L,mid,l,r,p<<1);
		else if(mid<l)reverse(mid+1,R,l,r,p<<1|1);
		else reverse(L,mid,l,mid,p<<1),reverse(mid+1,R,mid+1,r,p<<1|1);

		tree[p]=push_up(tree[p<<1],tree[p<<1|1]);
	}
}sgt;
int sl[25],sr[25];
void response(){
	int flag;scanf("%d",&flag);
	if(flag==0){
		int pos,val;
		scanf("%d %d",&pos,&val);
		sgt.update(1,n,pos,val,1);
	}else if(flag==1){
		int l,r,k;
		scanf("%d %d %d",&l,&r,&k);
		int res=0,tot=0;
		rep(i,0,k){
			node now=sgt.query(1,n,l,r,1);
			if(now.sub_mx<=0)break;
			res+=now.sub_mx;
			sl[tot]=now.sub_mxl;
			sr[tot]=now.sub_mxr;
//			printf("response reverse [ %d , %d ]\n",sl[tot],sr[tot]);
			sgt.reverse(1,n,sl[tot],sr[tot],1);
			tot++;
		}
		printf("%d\n",res);
		rep(i,0,tot)
			sgt.reverse(1,n,sl[i],sr[i],1);
	}else assert(false);
}
int main(){

//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);

	input();
	sgt.build(1,n,1);
	while(m--)response();
	return 0;
}


#include <cstdio>
#include <cassert>
#define rep(i,a,b) for(int i=a,i##_END_=b;i<i##_END_;++i)
const int N=(int)1e5;
int n,m,a[N+5];
void input(){
	scanf("%d",&n);
	rep(i,1,n+1)scanf("%d",&a[i]);
	scanf("%d",&m);
}
inline int max(int x,int y){
	if(x>y)return x;
	return y;
}
inline int min(int x,int y){
	if(x<y)return x;
	return y;
}
inline void swap(int &x,int &y){
	x^=y;y^=x;x^=y;
}
struct node{
	int pre_mx,pre_mxr,suf_mx,suf_mxl;
	int sub_mx,sub_mxl,sub_mxr;

	int pre_mi,pre_mir,suf_mi,suf_mil;
	int sub_mi,sub_mil,sub_mir;

	int sum,l,r;

	void print(){
		printf("pre_mx %d suf_mx %d sub_mx %d\n",pre_mx,suf_mx,sub_mx);
		printf("sum %d l %d r %d\n",sum,l,r);
	}
	void init(int pos,int val){
		pre_mx=suf_mx=sub_mx=val;
		pre_mxr=suf_mxl=sub_mxl=sub_mxr=pos;

		pre_mi=suf_mi=sub_mi=val;
		pre_mir=suf_mil=sub_mil=sub_mir=pos;

		sum=val,l=r=pos;
	}
	void reverse(){
		swap(pre_mx,pre_mi);pre_mx=-pre_mx;pre_mi=-pre_mi;swap(pre_mxr,pre_mir);
		swap(suf_mx,suf_mi);suf_mx=-suf_mx;suf_mi=-suf_mi;swap(suf_mxl,suf_mil);

		swap(sub_mx,sub_mi);sub_mx=-sub_mx;sub_mi=-sub_mi;
		swap(sub_mxl,sub_mil);swap(sub_mxr,sub_mir);

		sum=-sum;
	}
};
#define mid ((L+R)>>1)
#define ls (p<<1)
#define rs (p<<1|1)
#define lson ls,L,mid
#define rson rs,mid+1,R
#define root 1,1,n
struct Segment_Tree{
	node tree[N*4+5];
	bool rev[N*4+5];

	inline void modify(int p){
		rev[p]^=1;
		tree[p].reverse();
	}

	inline node push_up(const node x,const node y){
		node res;

		if(x.pre_mx>x.sum+y.pre_mx)res.pre_mx=x.pre_mx,res.pre_mxr=x.pre_mxr;
		else res.pre_mx=x.sum+y.pre_mx,res.pre_mxr=y.pre_mxr;
		if(y.suf_mx>x.suf_mx+y.sum)res.suf_mx=y.suf_mx,res.suf_mxl=y.suf_mxl;
		else res.suf_mx=x.suf_mx+y.sum,res.suf_mxl=x.suf_mxl;
		if(x.suf_mx+y.pre_mx>max(x.sub_mx,y.sub_mx))res.sub_mx=x.suf_mx+y.pre_mx,res.sub_mxl=x.suf_mxl,res.sub_mxr=y.pre_mxr;
		else if(x.sub_mx>y.sub_mx)res.sub_mx=x.sub_mx,res.sub_mxl=x.sub_mxl,res.sub_mxr=x.sub_mxr;
		else res.sub_mx=y.sub_mx,res.sub_mxl=y.sub_mxl,res.sub_mxr=y.sub_mxr;

		if(x.pre_mi<x.sum+y.pre_mi)res.pre_mi=x.pre_mi,res.pre_mir=x.pre_mir;
		else res.pre_mi=x.sum+y.pre_mi,res.pre_mir=y.pre_mir;
		if(y.suf_mi<x.suf_mi+y.sum)res.suf_mi=y.suf_mi,res.suf_mil=y.suf_mil;
		else res.suf_mi=x.suf_mi+y.sum,res.suf_mil=x.suf_mil;
		if(x.suf_mi+y.pre_mi<min(x.sub_mi,y.sub_mi))res.sub_mi=x.suf_mi+y.pre_mi,res.sub_mil=x.suf_mil,res.sub_mir=y.pre_mir;
		else if(x.sub_mi<y.sub_mi)res.sub_mi=x.sub_mi,res.sub_mil=x.sub_mil,res.sub_mir=x.sub_mir;
		else res.sub_mi=y.sub_mi,res.sub_mil=y.sub_mil,res.sub_mir=y.sub_mir;

		res.sum=x.sum+y.sum;
		res.l=x.l;res.r=y.r;

		return res;
	}
	inline void push_down(int p){
		if(rev[p]){
			modify(p<<1);
			modify(p<<1|1);
			rev[p]=false;
		}
	}
//	inline void push_down(int p){
//		if(rev[p]^rev[p<<1])modify(p<<1);
//		if(rev[p]^rev[p<<1|1])modify(p<<1|1);
//	}
	void build(int p,int L,int R){
		rev[p]=false;
		if(L==R){
			tree[p].init(L,a[L]);
			return;
		}

		build(lson);
		build(rson);

		tree[p]=push_up(tree[ls],tree[rs]);
//		puts("_____________");
//		tree[p].print();
//		printf("[ %d , %d ] sub_mx %d sum %d lsum %d rsum %d\n",L,R,tree[p].sub_mx,tree[p].sum,tree[p<<1].sum,tree[p<<1|1].sum);
	}
	void update(int p,int L,int R,int pos,int val){
		if(L==R){
			tree[p].init(pos,val);
			return;
		}

		push_down(p);

		if(pos<=mid)update(lson,pos,val);
		else update(rson,pos,val);

		tree[p]=push_up(tree[ls],tree[rs]);
	}
	node query(int p,int L,int R,int l,int r){
		if(L==l&&R==r)return tree[p];

		push_down(p);

		if(r<=mid)return query(lson,l,r);
		else if(mid<l)return query(rson,l,r);
		else return push_up(query(lson,l,mid),query(rson,mid+1,r));
	}
	void reverse(int p,int L,int R,int l,int r){
//		printf("[ %d , %d ] [ %d , %d ]\n",L,R,l,r);
		if(L==l&&R==r){
			modify(p);
			return;
		}

		push_down(p);

		if(r<=mid)reverse(lson,l,r);
		else if(mid<l)reverse(rson,l,r);
		else reverse(lson,l,mid),reverse(rson,mid+1,r);

		tree[p]=push_up(tree[ls],tree[rs]);
	}
}sgt;
int sl[25],sr[25];
void response(){
	int flag;scanf("%d",&flag);
	if(flag==0){
		int pos,val;
		scanf("%d %d",&pos,&val);
		sgt.update(root,pos,val);
	}else if(flag==1){
		int l,r,k;
		scanf("%d %d %d",&l,&r,&k);
		int res=0,tot=0;
		rep(i,0,k){
			node now=sgt.query(root,l,r);
			if(now.sub_mx<=0)break;
			res+=now.sub_mx;
			sl[tot]=now.sub_mxl;
			sr[tot]=now.sub_mxr;
//			printf("response reverse [ %d , %d ]\n",sl[tot],sr[tot]);
			sgt.reverse(root,sl[tot],sr[tot]);
			tot++;
		}
		printf("%d\n",res);
		rep(i,0,tot)sgt.reverse(root,sl[i],sr[i]);
	}else assert(false);
}
int main(){

//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);

	input();
	sgt.build(root);
	while(m--)response();
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct apple{
     int mx,sum,l,r,lx,rx,lp,rp,p1,p2;
}t,now,tree[400010][2];
struct pear{
    int x,y;
}node[100010];
int cnt,nodel[400010],noder[400010],a[100010],rev[400010];
void add(int x,int y)
{
    node[++cnt].x=x;
    node[cnt].y=y;
}
void pushup(apple &x,apple l,apple r)
{
    x.sum=l.sum+r.sum;
        x.lx=l.lx;
        x.lp=l.lp;
        x.rx=r.rx;
        x.rp=r.rp;
        x.mx=l.mx;
        x.p1=l.p1;
        x.p2=l.p2;
    if(r.mx>x.mx)
    {
        x.mx=r.mx;
        x.p1=r.p1;
        x.p2=r.p2;
    }
    if(l.sum+r.lx>x.lx)
    {
      x.lx=l.sum+r.lx;
      x.lp=r.lp;
    }
    if(l.rx+r.sum>x.rx)
    {
        x.rx=l.rx+r.sum;
        x.rp=l.rp;
    }
    if(x.lx>x.mx)
    {
        x.mx=x.lx;
        x.p1=x.l;
        x.p2=x.lp;
    }
    if(x.rx>x.mx)
    {
        x.mx=x.rx;
        x.p1=x.rp;
        x.p2=x.r;
    }
    if(l.rx+r.lx>x.mx)
    {
       x.mx=l.rx+r.lx;
       x.p1=l.rp;
       x.p2=r.lp;

    }
}
void pushdown(int x)
{
    if(rev[x])
    {
       swap(tree[x*2][0],tree[x*2][1]);
       swap(tree[x*2+1][0],tree[x*2+1][1]);
       rev[x*2]^=1;
       rev[x*2+1]^=1;
       rev[x]=0;
    }
}
void rever(int x,int l,int r)
{
    pushdown(x);
    int ll,rr;
    ll=nodel[x];
    rr=noder[x];
    if(rr<l||r<ll)
       return;
    if(l<=ll&&rr<=r)
    {
        swap(tree[x][0],tree[x][1]);
        rev[x]^=1;
        //printf("[%d]\n",x);
        return;
    }
    rever(x*2,l,r);
    rever(x*2+1,l,r);
    /*printf("%d,%d\n",x*2,x*2+1);
    printf("%d(%d),(%d)\n",x*2,tree[x*2][0].sum,tree[x*2][1].sum);
    printf("%d(%d),(%d)\n",x*2+1,tree[x*2+1][0].sum,tree[x*2+1][1].sum);*/
    pushup(tree[x][0],tree[x*2][0],tree[x*2+1][0]);
    pushup(tree[x][1],tree[x*2][1],tree[x*2+1][1]);
}
void build(int x,int l,int r)
{
    nodel[x]=l;
    noder[x]=r;
    tree[x][0].l=tree[x][1].l=l;
    tree[x][1].r=tree[x][0].r=r;
    tree[x][0].mx=tree[x][0].sum=tree[x][0].lx=tree[x][0].rx=-1000000000;
    tree[x][1].mx=tree[x][1].sum=tree[x][1].lx=tree[x][1].rx=-1000000000;
    if(l==r)
    {
        tree[x][0].mx=tree[x][0].sum=tree[x][0].lx=tree[x][0].rx=a[l];
        tree[x][0].lp=tree[x][0].rp=tree[x][0].p1=tree[x][0].p2=l;
        tree[x][1].mx=tree[x][1].sum=tree[x][1].lx=tree[x][1].rx=-a[l];
        tree[x][1].lp=tree[x][1].rp=tree[x][1].p1=tree[x][1].p2=l;
        return;
    }
    int midd=(l+r)>>1;
    build(x*2,l,midd);
    build(x*2+1,midd+1,r);
    pushup(tree[x][0],tree[x*2][0],tree[x*2+1][0]);
    pushup(tree[x][1],tree[x*2][1],tree[x*2+1][1]);
}
apple query(int x,int l,int r)
{
    pushdown(x);
    int ll,rr;
    ll=nodel[x];
    rr=noder[x];
    if(l<=ll&&rr<=r)
    {
       return tree[x][0];
    }
    int midd=(ll+rr)>>1;
    if(midd<l)
        return query(x*2+1,l,r);
    if(midd>=r)
        return query(x*2,l,r);
    pushup(now,query(x*2,l,r),query(x*2+1,l,r));
    return now;
}
void solve(int l,int r,int k)
{
    cnt=0;
    int ans=0,i;
     /*for(int j=1;j<=20;j++)
       {
         printf("%d(%d,%d)\n",j,nodel[j],noder[j]);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)  %d\n",tree[j][0].lx,tree[j][0].lp,tree[j][0].rx,tree[j][0].rp,tree[j][0].mx,tree[j][0].p1,tree[j][0].p2,tree[j][0].sum);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)  %d\n",tree[j][1].lx,tree[j][1].lp,tree[j][1].rx,tree[j][1].rp,tree[j][1].mx,tree[j][1].p1,tree[j][1].p2,tree[j][1].sum);

       }*/
    //printf("\n\n");
    while(k)
    {
       k--;
       t=query(1,l,r);
       if(t.mx<0)
         break;
       ans+=t.mx;
       //printf("%d,(%d,%d)\n",t.mx,t.p1,t.p2);
       rever(1,t.p1,t.p2);
       add(t.p1,t.p2);
      // printf("\n");
       /*for(int j=1;j<=20;j++)
       {
         printf("%d(%d,%d)\n",j,nodel[j],noder[j]);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)\n",tree[j][0].lx,tree[j][0].lp,tree[j][0].rx,tree[j][0].rp,tree[j][0].mx,tree[j][0].p1,tree[j][0].p2);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)\n",tree[j][1].lx,tree[j][1].lp,tree[j][1].rx,tree[j][1].rp,tree[j][1].mx,tree[j][1].p1,tree[j][1].p2);

       }*/
    }
   /* for(int j=1;j<=20;j++)
       {
         printf("%d(%d,%d)\n",j,nodel[j],noder[j]);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)  %d\n",tree[j][0].lx,tree[j][0].lp,tree[j][0].rx,tree[j][0].rp,tree[j][0].mx,tree[j][0].p1,tree[j][0].p2,tree[j][0].sum);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)  %d\n",tree[j][1].lx,tree[j][1].lp,tree[j][1].rx,tree[j][1].rp,tree[j][1].mx,tree[j][1].p1,tree[j][1].p2,tree[j][1].sum);

       }*/
    for(i=1;i<=cnt;i++)
    {
        rever(1,node[i].x,node[i].y);
       /*for(int j=1;j<=20;j++)
       {
         printf("%d(%d,%d)\n",j,nodel[j],noder[j]);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)  %d\n",tree[j][0].lx,tree[j][0].lp,tree[j][0].rx,tree[j][0].rp,tree[j][0].mx,tree[j][0].p1,tree[j][0].p2,tree[j][0].sum);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)  %d\n",tree[j][1].lx,tree[j][1].lp,tree[j][1].rx,tree[j][1].rp,tree[j][1].mx,tree[j][1].p1,tree[j][1].p2,tree[j][1].sum);

       }*/
    }
    /*for(int j=1;j<=20;j++)
       {
         printf("%d(%d,%d)\n",j,nodel[j],noder[j]);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)\n",tree[j][0].lx,tree[j][0].lp,tree[j][0].rx,tree[j][0].rp,tree[j][0].mx,tree[j][0].p1,tree[j][0].p2);
         printf("l:%d(%d),r:%d(%d),mx:%d(%d,%d)\n",tree[j][1].lx,tree[j][1].lp,tree[j][1].rx,tree[j][1].rp,tree[j][1].mx,tree[j][1].p1,tree[j][1].p2);

       }*/
    printf("%d\n",ans);
}
void change(int x,int l,int v)
{
    int ll,rr;
    ll=nodel[x];
    rr=noder[x];
    pushdown(x);
    if(rr<l||l<ll)
       return;
    if(ll==l&&rr==l)
    {
        tree[x][0].mx=tree[x][0].sum=tree[x][0].lx=tree[x][0].rx=v;
        tree[x][0].lp=tree[x][0].rp=tree[x][0].p1=tree[x][0].p2=l;
        tree[x][1].mx=tree[x][1].sum=tree[x][1].lx=tree[x][1].rx=-v;
        tree[x][1].lp=tree[x][1].rp=tree[x][1].p1=tree[x][1].p2=l;
        return;
    }
    change(x*2,l,v);
    change(x*2+1,l,v);
    pushup(tree[x][0],tree[x*2][0],tree[x*2+1][0]);
    pushup(tree[x][1],tree[x*2][1],tree[x*2+1][1]);
}
int main(){
    int n,m,i,l,r,k,x,y,opt;
   // freopen("tree.in","r",stdin);
   // freopen("tree.out","w",stdout);
    scanf("%d",&n);
    for(i=1;i<=n;i++)
      scanf("%d",&a[i]);
    build(1,1,n);
    //for(i=1;i<=10;i++)
     // printf("%d(%d,%d)\n",i,nodel[i],noder[i]);
      //return 0;
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
     scanf("%d",&opt);
     if(!opt)
     {
        scanf("%d%d",&x,&y);
        change(1,x,y);
     }
     else
     {
        scanf("%d%d%d",&l,&r,&k);
        solve(l,r,k);
     }
    }
   return 0;
}


// Copyright 2018, Skqliao
#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = (l), _##i##_ = (r); i < _##i##_; ++i)
#define rof(i, l, r) for (int i = (l) - 1, _##i##_ = (r); i >= _##i##_; --i)
#define All(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
typedef long long ll;

#define ONLINE_JUDGE

namespace io {
#ifndef ONLINE_JUDGE
char gc() {
	return getchar();
}
template <class T> inline T gt() {
	static T x;
	std::cin >> x;
	return x;
}
template <typename T> inline void pt(T x, const char c = '\n') {
	std::cout << x << c;
}
void flush() {}
#else
const int MAXSIZE = 1 << 22;
inline char gc() {
	static char In[MAXSIZE], *at = In, *en = In;
	if (at == en) {
		en = (at = In) + fread(In, 1, MAXSIZE, stdin);
	}
	return at == en ? EOF : *at++;
}
template <class T> inline T gt() {
	char c;
	while (c = gc(), !isdigit(c) && c != '-') {}
	bool f = c == '-';
	T x = f ? 0 : c - '0';
	for (c = gc(); isdigit(c); c = gc()) {
		x = x * 10 + c - '0';
	}
	return f ? -x : x;
}
char Out[MAXSIZE], *cur = Out, *end = Out + MAXSIZE - 100;
void flush() {
	fwrite(Out, 1, cur - Out, stdout);
	cur = Out;
}
template <typename T> inline void pt(T x, char c = '\n') {
	static int S[20], *top;
	top = S;
	if (x < 0) {
		*cur++ = '-', x = -x;
	}
	do {
		*++top = x % 10, x /= 10;
	} while (x);
	while (top != S) {
		*cur++ = *top-- + '0';
	}
	*cur++ = c;
	if (cur >= end) {
		flush();
	}
}
#endif
}  // namespace io

const int MAXN = 1e5 + 5;

struct Line {
	int l, r, s;
	Line(int l = 0, int r = 0, int s = 0):
		l(l), r(r), s(s) {}
	Line operator + (const Line &x) {
		return (Line) {l, x.r, s + x.s};
	}
	bool operator < (const Line &x) const {
		return s < x.s;
	}
};

struct Node {
	Line lMax, lMin, rMax, rMin, vMax, vMin, v;
	int rev;
	Node (int l = 0, int r = 0, int s = 0) {
		lMax = lMin = rMax = rMin = vMax = vMin = v = Line(l, r, s);
		rev = 0;
	}
	void pushUp(Node ls, Node rs) {
		if (ls.lMax.l == 0) {
			*this = rs;
			return;
		}
		lMax = std::max(ls.lMax, ls.v + rs.lMax);
		lMin = std::min(ls.lMin, ls.v + rs.lMin);
		rMax = std::max(ls.rMax + rs.v, rs.rMax);
		rMin = std::min(ls.rMin + rs.v, rs.rMin);
		vMax = std::max(ls.rMax, rs.lMax);
		vMax = std::max(vMax, std::max(ls.vMax, rs.vMax));
		vMax = std::max(vMax, ls.rMax + rs.lMax);
		vMax = std::max(vMax, std::max(lMax, rMax));
		vMin = std::min(ls.rMin, rs.lMin);
		vMin = std::min(vMin, std::min(ls.vMin, rs.vMin));
		vMin = std::min(vMin, ls.rMin + rs.lMin);
		vMin = std::min(vMin, std::min(lMin, rMin));
		v = ls.v + rs.v;
	}
} T[MAXN << 2];

#define LT rt << 1, l, m
#define RT rt << 1 | 1, m + 1, r

void rev(Node& x) {
	x.rev ^= 1;
	std::swap(x.lMin, x.lMax);
	std::swap(x.rMin, x.rMax);
	std::swap(x.vMin, x.vMax);
	x.lMin.s *= -1, x.lMax.s *= -1;
	x.rMin.s *= -1, x.rMax.s *= -1;
	x.vMin.s *= -1, x.vMax.s *= -1;
	x.v.s *= -1;
}

void pushDown(int rt) {
	if(T[rt].rev) {
		rev(T[rt << 1]);
		rev(T[rt << 1 | 1]);
		T[rt].rev = 0;
	}
}

void build(int rt, int l, int r) {
	if (l == r) {
		T[rt] = Node(l, r, io::gt<int>());
		return ;
	}
	int m = (l + r) >> 1;
	build(LT);
	build(RT);
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

void modify(int rt, int l, int r, int p, int x) {
	if(l == r) {
		T[rt] = Node(l, r, x);
		return ;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if(p <= m) {
		modify(LT, p, x);
	} else {
		modify(RT, p, x);
	}
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

void reverse(int rt, int l, int r, int a, int b) {
	if(a <= l && r <= b) {
		rev(T[rt]);
		return ;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if(a <= m) {
		reverse(LT, a, b);
	}
	if(m < b) {
		reverse(RT, a, b);
	}
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

Node queryMax(int rt, int l, int r, int a, int b) {
	if(a <= l && r <= b) {
		return T[rt];
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	Node res;
	if(a <= m) {
		res.pushUp(res, queryMax(LT, a, b));
	}
	if(m < b) {
		res.pushUp(res, queryMax(RT, a, b));
	}
	return res;
}

int main() {
	int N = io::gt<int>();
	build(1, 1, N);
	int M = io::gt<int>();
	std::vector<Line> V;
	while(M--) {
		int opt = io::gt<int>();
		if(opt == 0) {
			int p = io::gt<int>(), x = io::gt<int>();
			modify(1, 1, N, p, x);
		} else {
			int l = io::gt<int>(), r = io::gt<int>(), k = io::gt<int>();
			int sum = 0;
			V.clear();
			rep(i, 0, k) {
				Line x = queryMax(1, 1, N, l, r).vMax;
				if(x.s < 0) {
					break;
				}
				sum += x.s;
				V.push_back(x);
				reverse(1, 1, N, x.l, x.r);
			}
			io::pt(sum);
			rep(i, 0, SZ(V)) {
				reverse(1, 1, N, V[i].l, V[i].r);
			}
		}
	}
	io::flush();
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 100010

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node operator+(const node&a,const node&b){
	node c;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinLeft);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

void modify(int pos,int j,int x){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void refresh(int idx){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;
			tree[idx+idx+1].push^=true;
		}
	}
}

void change(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)modify(1,x,y);else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
//				printf("%d %d\n",ct.MaxSum.Left,ct.MaxSum.Right);
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <cassert>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 100010

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node operator+(const node&a,const node&b){
	assert(a.push==false);
	assert(b.push==false);
	node c;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinLeft);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

void refresh(int idx){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;
			tree[idx+idx+1].push^=true;
		}
	}
}

void modify(int pos,int j,int x){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)modify(1,x,y);else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
//				printf("%d %d\n",ct.MaxSum.Left,ct.MaxSum.Right);
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <cassert>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 200020

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

inline MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node c;

inline node operator+(const node&a,const node&b){
	c.push=false;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinRight);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

inline void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

inline void refresh(int idx,int d){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;
			tree[idx+idx+1].push^=true;
		}
	}
}

void modify(int pos,int j,int x){
	refresh(pos,0);
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		refresh(pos+pos,0);
		refresh(pos+pos+1,0);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	refresh(pos,0);
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		refresh(pos,0);
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos,0);
		refresh(pos+pos+1,0);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	refresh(pos,0);
	if(tree[pos].l!=tree[pos].r){
		refresh(pos+pos,0);
		refresh(pos+pos+1,0);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0){
			a[x]=y;
			modify(1,x,y);
		}else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <cassert>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 200020

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node operator+(const node&a,const node&b){
	node c;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinLeft);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

void refresh(int idx){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;
			tree[idx+idx+1].push^=true;
		}
	}
}

void modify(int pos,int j,int x){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)modify(1,x,y);else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
//				printf("%d %d\n",ct.MaxSum.Left,ct.MaxSum.Right);
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 100010

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node operator+(const node&a,const node&b){
	node c;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinLeft);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

void modify(int pos,int j,int x){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)modify(1,x,y);else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
//				printf("%d %d\n",ct.MaxSum.Left,ct.MaxSum.Right);
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <cassert>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 200020

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

inline MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node c;

inline node operator+(const node&a,const node&b){
	c.push=false;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinRight);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

inline void refresh(int idx,int d){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;refresh(idx+idx,d);
			tree[idx+idx+1].push^=true;refresh(idx+idx+1,d);
		}
	}
}

void modify(int pos,int j,int x){
	refresh(pos,0);
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		refresh(pos+pos,0);
		refresh(pos+pos+1,0);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	refresh(pos,0);
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		refresh(pos,0);
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos,0);
		refresh(pos+pos+1,0);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	refresh(pos,0);
	if(tree[pos].l!=tree[pos].r){
		refresh(pos+pos,0);
		refresh(pos+pos+1,0);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0){
			a[x]=y;
			modify(1,x,y);
		}else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 100010

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node operator+(const node&a,const node&b){
	node c;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinLeft);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

void refresh(int idx){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;
			tree[idx+idx+1].push^=true;
		}
	}
}

void modify(int pos,int j,int x){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)modify(1,x,y);else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
//				printf("%d %d\n",ct.MaxSum.Left,ct.MaxSum.Right);
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include <cassert>
#include <algorithm>
#include <cstdio>
using namespace std;

#define maxn 100010

int a[maxn],n,i,m,op,x,y,j,k,X[maxn],Y[maxn],xn,ret;

struct MyPair{
	int val,Left,Right;
	MyPair(){}
	MyPair(int val,int Left,int Right):val(val),Left(Left),Right(Right){}
	bool operator<(const MyPair&he)const{
		return val<he.val;
	}
};

MyPair operator+(const MyPair&a,const MyPair&b){
	MyPair c=MyPair(a.val+b.val,min(a.Left,b.Left),max(b.Right,a.Right));
	return c;
}

struct node{
	bool push;
	int l,r,sum;
	MyPair MaxLeft,MaxRight,MaxSum;
	MyPair MinLeft,MinRight,MinSum;
	node():push(false){}
	node(int val,int idx):l(idx),r(idx),sum(val){
		MaxLeft=MaxRight=MaxSum=
		MinLeft=MinRight=MinSum=MyPair(val,idx,idx);
		push=false;
	}
}tree[maxn*4],ct;

node operator+(const node&a,const node&b){
	assert(a.push==false);
	assert(b.push==false);
	node c;
	c.l=a.l,c.r=b.r;
	c.sum=a.sum+b.sum;
	//maxsum
	c.MaxLeft=max(a.MaxLeft,MyPair(a.sum,a.l,a.r)+b.MaxLeft);
	c.MaxRight=max(b.MaxRight,MyPair(b.sum,b.l,b.r)+a.MaxRight);
	c.MaxSum=a.MaxRight+b.MaxLeft;
	c.MaxSum=max(c.MaxSum,a.MaxSum);
	c.MaxSum=max(c.MaxSum,b.MaxSum);
	//minsum
	c.MinLeft=min(a.MinLeft,MyPair(a.sum,a.l,a.r)+b.MinLeft);
	c.MinRight=min(b.MinRight,MyPair(b.sum,b.l,b.r)+a.MinLeft);
	c.MinSum=a.MinRight+b.MinLeft;
	c.MinSum=min(c.MinSum,a.MinSum);
	c.MinSum=min(c.MinSum,b.MinSum);
	return c;
}

void init(int pos,int l,int r){
	tree[pos].l=l,tree[pos].r=r;
	if(l<r){
		init(pos+pos,l,(l+r)/2);
		init(pos+pos+1,(l+r)/2+1,r);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}else tree[pos]=node(a[l],l);
}

void do_push(node&me){
	me.sum*=-1;
	swap(me.MaxLeft,me.MinLeft);me.MaxLeft.val*=-1;me.MinLeft.val*=-1;
	swap(me.MaxRight,me.MinRight);me.MaxRight.val*=-1,me.MinRight.val*=-1;
	swap(me.MaxSum,me.MinSum);me.MaxSum.val*=-1,me.MinSum.val*=-1;
	me.push=false;
}

void refresh(int idx){
	if(tree[idx].push){
		do_push(tree[idx]);
		if(tree[idx].l!=tree[idx].r){
			tree[idx+idx].push^=true;
			tree[idx+idx+1].push^=true;
		}
	}
}

void modify(int pos,int j,int x){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==tree[pos].r)tree[pos]=node(x,j);else{
		if(tree[pos+pos].r>=j)modify(pos+pos,j,x);else modify(pos+pos+1,j,x);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

void change(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r){
		tree[pos].push=true;
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}else{
		if(l<=min(r,tree[pos+pos].r))change(pos+pos,l,min(r,tree[pos+pos].r));
		if(max(l,tree[pos+pos+1].l)<=r)change(pos+pos+1,max(l,tree[pos+pos+1].l),r);
		refresh(pos+pos);
		refresh(pos+pos+1);
		tree[pos]=tree[pos+pos]+tree[pos+pos+1];
	}
}

node query(int pos,int l,int r){
	if(tree[pos].push){
		do_push(tree[pos]);
		if(tree[pos].l!=tree[pos].r){
			tree[pos+pos].push^=true;
			tree[pos+pos+1].push^=true;
		}
	}
	if(tree[pos].l==l&&tree[pos].r==r)return tree[pos];else{
		if(tree[pos+pos].r>=r)return query(pos+pos,l,r);
		if(tree[pos+pos+1].l<=l)return query(pos+pos+1,l,r);
		return query(pos+pos,l,tree[pos+pos].r)+query(pos+pos+1,tree[pos+pos+1].l,r);
	}
}

int main (int argc, char * const argv[]) {
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	init(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)modify(1,x,y);else{
			scanf("%d",&k);
			xn=ret=0;
			for(j=1;j<=k;j++){
				ct=query(1,x,y);
				if(ct.MaxSum.val<0)break;
				ret+=ct.MaxSum.val;
//				change(1,ct.MaxSum.Left,ct.MaxSum.Right);
//				X[++xn]=ct.MaxSum.Left;Y[xn]=ct.MaxSum.Right;
//				printf("%d %d\n",ct.MaxSum.Left,ct.MaxSum.Right);
			}
			for(j=1;j<=xn;j++)change(1,X[j],Y[j]);
			printf("%d\n",ret);
		}
	}
    return 0;
}


#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define inf 9000000000000000000LL
#define ll long long
using namespace std;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m;
int a[100005];
vector<pair<int,int> >q;
struct data{
    int lx,rx,mx,sum;
    int lp,rp,p1,p2;
    void ini(int l,int val){
        lp=rp=p1=p2=l;
        lx=rx=mx=val;
        sum=val;
    }
};
struct seg{
    int l,r,a,b;bool flag;
    data mn,mx;
    void ini(int val){
        mn.ini(l,-val);
        mx.ini(l,val);
    }
}t[400005];
void pushdown(int k)
{
    if(t[k].l==t[k].r)return;
    if(t[k].flag)
    {
        swap(t[k<<1].mn,t[k<<1].mx);
        swap(t[k<<1|1].mn,t[k<<1|1].mx);
        t[k<<1].flag^=1;t[k<<1|1].flag^=1;t[k].flag^=1;
    }
}
data merge(data a,data b)
{
    data t;
    t.sum=a.sum+b.sum;

    t.lx=a.lx;t.lp=a.lp;
    if(a.sum+b.lx>t.lx)t.lx=a.sum+b.lx,t.lp=b.lp;

    t.rx=b.rx;t.rp=b.rp;
    if(b.sum+a.rx>t.rx)t.rx=b.sum+a.rx,t.rp=a.rp;

    t.mx=a.rx+b.lx;t.p1=a.rp;t.p2=b.lp;
    if(t.mx<a.mx)t.mx=a.mx,t.p1=a.p1,t.p2=a.p2;
    if(t.mx<b.mx)t.mx=b.mx,t.p1=b.p1,t.p2=b.p2;
    return t;
}
void update(int k)
{
    t[k].mn=merge(t[k<<1].mn,t[k<<1|1].mn);
    t[k].mx=merge(t[k<<1].mx,t[k<<1|1].mx);
}
void build(int k,int l,int r)
{
    t[k].l=l;t[k].r=r;
    if(l==r)
    {
        t[k].ini(a[l]);
        return;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    update(k);
}
void rever(int k,int x,int y)
{
    pushdown(k);
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==x&&y==r)
    {
        swap(t[k].mn,t[k].mx);t[k].flag^=1;
        return;
    }
    if(y<=mid)rever(k<<1,x,y);
    else if(x>mid)rever(k<<1|1,x,y);
    else rever(k<<1,x,mid),rever(k<<1|1,mid+1,y);
    update(k);
}
data query(int k,int x,int y)
{
    pushdown(k);
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==x&&y==r)return t[k].mx;
    if(y<=mid)return query(k<<1,x,y);
    if(x>mid)return query(k<<1|1,x,y);
    return merge(query(k<<1,x,mid),query(k<<1|1,mid+1,y));
}
void solve(int l,int r,int k)
{
    int ans=0;
    q.clear();
    while(k--)
    {
        data t=query(1,l,r);
        if(t.mx>0)ans+=t.mx;
        else break;
        rever(1,t.p1,t.p2);
        q.push_back(make_pair(t.p1,t.p2));
    }
    for(int i=q.size()-1;i>=0;i--)
        rever(1,q[i].first,q[i].second);
    printf("%d\n",ans);
}
void change(int k,int pos,int val)
{
    pushdown(k);
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==r){t[k].ini(val);return;}
    if(pos<=mid)change(k<<1,pos,val);
    else change(k<<1|1,pos,val);
    update(k);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    build(1,1,n);
    int opt,l,r,k;
    m=read();
    while(m--)
    {
        opt=read();l=read();r=read();
        if(opt==1){k=read();solve(l,r,k);}
        else change(1,l,r);
    }
    return 0;
}


#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define inf 9000000000000000000LL
#define ll long long
using namespace std;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m;
int a[100005];
vector<pair<int,int> >q;
struct data{
    int lx,rx,mx,sum;
    int lp,rp,p1,p2;
    void ini(int l,int val){
        lp=rp=p1=p2=l;
        lx=rx=mx=val;
        sum=val;
    }
};
struct seg{
    int l,r,a,b;
    data mn,mx;
    void ini(int val){
        mn.ini(l,-val);
        mx.ini(l,val);
    }
}t[400005];
data merge(data a,data b)
{
    data t;
    t.sum=a.sum+b.sum;

    t.lx=a.lx;t.lp=a.lp;
    if(a.sum+b.lx>t.lx)t.lx=a.sum+b.lx,t.lp=b.lp;

    t.rx=b.rx;t.rp=b.rp;
    if(b.sum+a.rx>t.rx)t.rx=b.sum+a.rx,t.rp=a.rp;

    t.mx=a.rx+b.lx;t.p1=a.rp;t.p2=b.lp;
    if(t.mx<a.mx)t.mx=a.mx,t.p1=a.p1,t.p2=a.p2;
    if(t.mx<b.mx)t.mx=b.mx,t.p1=b.p1,t.p2=b.p2;
    return t;
}
void update(int k)
{
    t[k].mn=merge(t[k<<1].mn,t[k<<1|1].mn);
    t[k].mx=merge(t[k<<1].mx,t[k<<1|1].mx);
}
void build(int k,int l,int r)
{
    t[k].l=l;t[k].r=r;
    if(l==r)
    {
        t[k].ini(a[l]);
        return;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    update(k);
}
void rever(int k,int x,int y)
{
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==x&&y==r)
    {
        swap(t[k].mn,t[k].mx);
        return;
    }
    if(y<=mid)rever(k<<1,x,y);
    else if(x>mid)rever(k<<1|1,x,y);
    else rever(k<<1,x,mid),rever(k<<1|1,mid+1,y);
    update(k);
}
data query(int k,int x,int y)
{
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==x&&y==r)return t[k].mx;
    if(y<=mid)return query(k<<1,x,y);
    if(x>mid)return query(k<<1|1,x,y);
    return merge(query(k<<1,x,mid),query(k<<1|1,mid+1,y));
}
void solve(int l,int r,int k)
{
    int ans=0;q.clear();
    while(k--)
    {
        data t=query(1,l,r);
        if(t.mx>0)ans+=t.mx;
        else break;
        rever(1,t.p1,t.p2);
        q.push_back(make_pair(t.p1,t.p2));
    }
    for(int i=q.size()-1;i>=0;i--)
        rever(1,q[i].first,q[i].second);
    printf("%d\n",ans);
}
void change(int k,int pos,int val)
{
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==r){t[k].ini(val);return;}
    if(pos<=mid)change(k<<1,pos,val);
    else change(k<<1|1,pos,val);
    update(k);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    build(1,1,n);
    int opt,l,r,k;
    m=read();
    while(m--)
    {
        opt=read();l=read();r=read();
        if(opt==1){k=read();solve(l,r,k);}
        else change(1,l,r);
    }
    return 0;
}


#include <cmath>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <bitset>
#include <memory.h>
#include <functional>
#include <queue>
#include <fstream>
#include <ctime>
#include <deque>
#include <utility>
#include <stack>
#include <sstream>
#include <list>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <assert.h>
using namespace std;
#define sqr(_) ((_)*(_))

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<Pip, Pii> Ppp;
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define SZ(x) ((int)(x).size())
#define tl t[x].l
#define tr t[x].r
#define ls (x << 1)
#define rs ((x << 1) | 1)
#define mid ((tl + tr) >> 1)
#define LT x.y.x
#define RT x.y.y

typedef long long LL;

const int MAXN = 100000 + 10, INF = 0x3f3f3f3f;

struct Seg_tree
{
	int l, r, s;
	Pii mx, mn;
	Pip d1, d2;
}t[MAXN * 4];
priority_queue<Ppp> q;
int n, a[MAXN], s[MAXN];

void Clean(int x)
{
	if (!t[x].s) return ;
	int u = t[x].s;
	t[x].s = 0;
	t[x].mx.x += u, t[x].mn.x += u;
	if (tl != tr)
		t[ls].s += u, t[rs].s += u;
}
void Upd(int x)
{
	t[x].mx = max(t[ls].mx, t[rs].mx);
	t[x].mn = min(t[ls].mn, t[rs].mn);
	t[x].d1 = max(t[ls].d1, t[rs].d1);
	t[x].d1 = max(t[x].d1, mp(t[rs].mx.x - t[ls].mn.x, mp(t[ls].mn.y, t[rs].mx.y)));
	t[x].d2 = max(t[ls].d2, t[rs].d2);
	t[x].d2 = max(t[x].d2, mp(t[ls].mx.x - t[rs].mn.x, mp(t[ls].mx.y, t[rs].mn.y)));
}
void Build(int x, int l, int r)
{
	tl = l, tr = r;
	if (tl == tr)
	{
		t[x].mx = mp(s[l], l);
		t[x].mn = mp(s[l], l);
		return ;
	}
	Build(ls, l, mid), Build(rs, mid + 1, r);
	Upd(x);
}
void init()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
	Build(1, 0, n);
}
void Ins(int x, int l, int r, int d)
{
	Clean(x);
	if (tl == l && tr == r)
	{
		t[x].s += d;
		Clean(x);
		return ;
	}
	if (r <= mid)
		Ins(ls, l, r, d), Clean(rs);
	else
	if (l > mid)
		Ins(rs, l, r, d), Clean(ls);
	else
		Ins(ls, l, mid, d), Ins(rs, mid + 1, r, d);
	Upd(x);
}
Pii f1, f2;
Pip Query(int x, int l, int r, int ty)
{
	if (l > r)
		return mp(-INF, mp(0, 0));
	if (x == 1)
		f1.x = INF, f2.x = -INF;
	Clean(x);
	Pip Tohka = mp(-INF, mp(0, 0));
	if (tl == l && tr == r)
	{
		if (!ty)
			Tohka = mp(t[x].mx.x - f1.x, mp(f1.y, t[x].mx.y));
		else
			Tohka = mp(f2.x - t[x].mn.x, mp(f2.y, t[x].mn.y));
		Tohka = max(Tohka, ((!ty) ? t[x].d1 : t[x].d2));
		f1 = min(f1, t[x].mn);
		f2 = max(f2, t[x].mx);
		return Tohka;
	}
	if (r <= mid)
		Tohka = Query(ls, l, r, ty), Clean(rs);
	else
	if (l > mid)
		Tohka = Query(rs, l, r, ty), Clean(ls);
	else
	{
		Tohka = Query(ls, l, mid, ty);
		Tohka = max(Tohka, Query(rs, mid + 1, r, ty));
	}
	Upd(x);
	return Tohka;
}
int Ask(int l, int r, int k)
{
	for (; !q.empty(); q.pop());
	q.push(mp(Query(1, l - 1, r, 0), mp(l - 1, r)));
	int Tohka = 0;
	for (int i = 1; i <= k && !q.empty(); ++i)
	{
		Ppp w = q.top();
		q.pop();
		if (w.x.x <= 0)
			return Tohka;
		Tohka += w.x.x;
		int ty = 0;
		if (w.y.x < 0)
			w.y.x = -w.y.x, ty = 1;
		int p = 1;
		if (ty)
			p = -1;
		q.push(mp(Query(1, w.y.x, w.LT - 1, ty), mp(w.y.x * p, w.LT - 1)));
		q.push(mp(Query(1, w.LT + 1, w.RT - 1, ty ^ 1), mp((w.LT + 1) * -p, w.RT - 1)));
		q.push(mp(Query(1, w.RT + 1, w.y.y, ty), mp((w.RT + 1) * p, w.y.y)));
	}
	return Tohka;
}
void solve()
{
	int q;
	scanf("%d", &q);
	for (int i = 1; i <= q; ++i)
	{
		int ty, l, r, k;
		scanf("%d", &ty);
		if (!ty)
		{
			scanf("%d%d", &l, &k);
			Ins(1, l, n, k - a[l]);
			a[l] = k;
		}
		else
		{
			scanf("%d%d%d", &l, &r, &k);
			printf("%d\n", Ask(l, r, k));
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif

	init();
	solve();

	fclose(stdin);
	fclose(stdout);
	return 0;
}


#include <cmath>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <bitset>
#include <memory.h>
#include <functional>
#include <queue>
#include <fstream>
#include <ctime>
#include <deque>
#include <utility>
#include <stack>
#include <sstream>
#include <list>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <assert.h>
using namespace std;
#define sqr(_) ((_)*(_))

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<Pip, Pii> Ppp;
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define SZ(x) ((int)(x).size())
#define tl t[x].l
#define tr t[x].r
#define ls (x << 1)
#define rs ((x << 1) | 1)
#define mid ((tl + tr) >> 1)
#define LT x.y.x
#define RT x.y.y

typedef long long LL;

const int MAXN = 100000 + 10, INF = 0x3f3f3f3f;

struct Seg_tree
{
	int l, r, s;
	Pii mx, mn;
	Pip d1, d2;
}t[MAXN * 4];
priority_queue<Ppp> q;
int n, a[MAXN], s[MAXN];

inline void Clean(int x)
{
	if (!t[x].s) return ;
	int u = t[x].s;
	t[x].s = 0;
	t[x].mx.x += u, t[x].mn.x += u;
	if (tl != tr)
		t[ls].s += u, t[rs].s += u;
}
inline void Upd(int x)
{
	t[x].mx = max(t[ls].mx, t[rs].mx);
	t[x].mn = min(t[ls].mn, t[rs].mn);
	t[x].d1 = max(t[ls].d1, t[rs].d1);
	t[x].d1 = max(t[x].d1, mp(t[rs].mx.x - t[ls].mn.x, mp(t[ls].mn.y, t[rs].mx.y)));
	t[x].d2 = max(t[ls].d2, t[rs].d2);
	t[x].d2 = max(t[x].d2, mp(t[ls].mx.x - t[rs].mn.x, mp(t[ls].mx.y, t[rs].mn.y)));
}
inline void Build(int x, int l, int r)
{
	tl = l, tr = r;
	if (tl == tr)
	{
		t[x].mx = mp(s[l], l);
		t[x].mn = mp(s[l], l);
		return ;
	}
	Build(ls, l, mid), Build(rs, mid + 1, r);
	Upd(x);
}
inline int Read()
{
	char ch = getchar();
	while (!((ch >= '0' && ch <= '9') || ch == '-')) ch = getchar();
	int x = 0, p = 1;
	if (ch == '-') p = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x * p;
}
void init()
{
//	scanf("%d", &n);
	n = Read();
	for (int i = 1; i <= n; ++i)
//		scanf("%d", &a[i]),
		a[i] = Read(), s[i] = s[i - 1] + a[i];
	Build(1, 0, n);
}
inline void Ins(int x, int l, int r, int d)
{
	Clean(x);
	if (tl == l && tr == r)
	{
		t[x].s += d;
		Clean(x);
		return ;
	}
	if (r <= mid)
		Ins(ls, l, r, d), Clean(rs);
	else
	if (l > mid)
		Ins(rs, l, r, d), Clean(ls);
	else
		Ins(ls, l, mid, d), Ins(rs, mid + 1, r, d);
	Upd(x);
}
Pii f1, f2;
Pip Query(int x, int l, int r, int ty)
{
	if (l > r)
		return mp(-INF, mp(0, 0));
	if (x == 1)
		f1.x = INF, f2.x = -INF;
	Clean(x);
	Pip Tohka = mp(-INF, mp(0, 0));
	if (tl == l && tr == r)
	{
		if (!ty)
			Tohka = mp(t[x].mx.x - f1.x, mp(f1.y, t[x].mx.y));
		else
			Tohka = mp(f2.x - t[x].mn.x, mp(f2.y, t[x].mn.y));
		Tohka = max(Tohka, ((!ty) ? t[x].d1 : t[x].d2));
		f1 = min(f1, t[x].mn);
		f2 = max(f2, t[x].mx);
		return Tohka;
	}
	if (r <= mid)
		Tohka = Query(ls, l, r, ty), Clean(rs);
	else
	if (l > mid)
		Tohka = Query(rs, l, r, ty), Clean(ls);
	else
	{
		Tohka = Query(ls, l, mid, ty);
		Tohka = max(Tohka, Query(rs, mid + 1, r, ty));
	}
	Upd(x);
	return Tohka;
}
inline int Ask(int l, int r, int k)
{
	for (; !q.empty(); q.pop());
	q.push(mp(Query(1, l - 1, r, 0), mp(l - 1, r)));
	int Tohka = 0;
	for (int i = 1; i <= k && !q.empty(); ++i)
	{
		Ppp w = q.top();
		q.pop();
		if (w.x.x <= 0)
			return Tohka;
		Tohka += w.x.x;
		int ty = 0;
		if (w.y.x < 0)
			w.y.x = -w.y.x, ty = 1;
		int p = 1;
		if (ty)
			p = -1;
		if (w.y.x < w.LT - 1)
			q.push(mp(Query(1, w.y.x, w.LT - 1, ty), mp(w.y.x * p, w.LT - 1)));
		if (w.LT + 1 < w.RT - 1)
			q.push(mp(Query(1, w.LT + 1, w.RT - 1, ty ^ 1), mp((w.LT + 1) * -p, w.RT - 1)));
		if (w.RT + 1 < w.y.y)
			q.push(mp(Query(1, w.RT + 1, w.y.y, ty), mp((w.RT + 1) * p, w.y.y)));
	}
	return Tohka;
}
inline void solve()
{
	int q;
	scanf("%d", &q);
	for (int i = 1; i <= q; ++i)
	{
		int ty, l, r, k;
//		scanf("%d", &ty);
		ty = Read();
		if (!ty)
		{
//			scanf("%d%d", &l, &k);
			l = Read(); k = Read();
			Ins(1, l, n, k - a[l]);
			a[l] = k;
		}
		else
		{
//			scanf("%d%d%d", &l, &r, &k);
			l = Read(); r = Read(); k = Read();
			printf("%d\n", Ask(l, r, k));
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif

	init();
	solve();

	fclose(stdin);
	fclose(stdout);
	return 0;
}


#include <cmath>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <bitset>
#include <memory.h>
#include <functional>
#include <queue>
#include <fstream>
#include <ctime>
#include <deque>
#include <utility>
#include <stack>
#include <sstream>
#include <list>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <assert.h>
using namespace std;
#define sqr(_) ((_)*(_))

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<Pip, Pii> Ppp;
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define SZ(x) ((int)(x).size())
#define tl t[x].l
#define tr t[x].r
#define ls (x << 1)
#define rs ((x << 1) | 1)
#define mid ((tl + tr) >> 1)
#define LT x.y.x
#define RT x.y.y

typedef long long LL;

const int MAXN = 100000 + 10, INF = 0x3f3f3f3f;

struct Seg_tree
{
	int l, r, s;
	bool f;
	Pii mx, mn;
	Pip d1, d2;
}t[MAXN * 4];
priority_queue<Ppp> q;
int n, a[MAXN], s[MAXN];

inline void Clean(int x)
{
	if (!t[x].s) return ;
	int u = t[x].s;
	t[x].s = 0;
	t[x].f = 1;
	t[x].mx.x += u, t[x].mn.x += u;
	if (tl != tr)
		t[ls].s += u, t[rs].s += u;
}
inline void Upd(int x)
{
	if (!t[ls].f && !t[rs].f) return ;
	t[ls].f = t[rs].f = 0;
	t[x].f = 1;
	t[x].mx = max(t[ls].mx, t[rs].mx);
	t[x].mn = min(t[ls].mn, t[rs].mn);
	t[x].d1 = max(t[ls].d1, t[rs].d1);
	t[x].d1 = max(t[x].d1, mp(t[rs].mx.x - t[ls].mn.x, mp(t[ls].mn.y, t[rs].mx.y)));
	t[x].d2 = max(t[ls].d2, t[rs].d2);
	t[x].d2 = max(t[x].d2, mp(t[ls].mx.x - t[rs].mn.x, mp(t[ls].mx.y, t[rs].mn.y)));
}
inline void Build(int x, int l, int r)
{
	tl = l, tr = r;
	if (tl == tr)
	{
		t[x].mx = mp(s[l], l);
		t[x].mn = mp(s[l], l);
		t[x].f = 1;
		return ;
	}
	Build(ls, l, mid), Build(rs, mid + 1, r);
	Upd(x);
}
inline int Read()
{
	char ch = getchar();
	while (!((ch >= '0' && ch <= '9') || ch == '-')) ch = getchar();
	int x = 0, p = 1;
	if (ch == '-') p = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x * p;
}
void init()
{
//	scanf("%d", &n);
	n = Read();
	for (int i = 1; i <= n; ++i)
//		scanf("%d", &a[i]),
		a[i] = Read(), s[i] = s[i - 1] + a[i];
	Build(1, 0, n);
}
inline void Ins(int x, int l, int r, int d)
{
	Clean(x);
	if (tl == l && tr == r)
	{
		t[x].s += d;
		Clean(x);
		return ;
	}
	if (r <= mid)
		Ins(ls, l, r, d), Clean(rs);
	else
	if (l > mid)
		Ins(rs, l, r, d), Clean(ls);
	else
		Ins(ls, l, mid, d), Ins(rs, mid + 1, r, d);
	Upd(x);
}
Pii f1, f2;
Pip Query(int x, int l, int r, int ty)
{
	if (l > r)
		return mp(-INF, mp(0, 0));
	if (x == 1)
		f1.x = INF, f2.x = -INF;
	Clean(x);
	Pip Tohka = mp(-INF, mp(0, 0));
	if (tl == l && tr == r)
	{
		if (!ty)
			Tohka = mp(t[x].mx.x - f1.x, mp(f1.y, t[x].mx.y));
		else
			Tohka = mp(f2.x - t[x].mn.x, mp(f2.y, t[x].mn.y));
		Tohka = max(Tohka, ((!ty) ? t[x].d1 : t[x].d2));
		f1 = min(f1, t[x].mn);
		f2 = max(f2, t[x].mx);
		return Tohka;
	}
	if (r <= mid)
		Tohka = Query(ls, l, r, ty), Clean(rs);
	else
	if (l > mid)
		Tohka = Query(rs, l, r, ty), Clean(ls);
	else
	{
		Tohka = Query(ls, l, mid, ty);
		Tohka = max(Tohka, Query(rs, mid + 1, r, ty));
	}
	Upd(x);
	return Tohka;
}
inline int Ask(int l, int r, int k)
{
	for (; !q.empty(); q.pop());
	q.push(mp(Query(1, l - 1, r, 0), mp(l - 1, r)));
	int Tohka = 0;
	for (int i = 1; i <= k && !q.empty(); ++i)
	{
		Ppp w = q.top();
		q.pop();
		if (w.x.x <= 0)
			return Tohka;
		Tohka += w.x.x;
		int ty = 0;
		if (w.y.x < 0)
			w.y.x = -w.y.x, ty = 1;
		int p = 1;
		if (ty)
			p = -1;
		if (w.y.x < w.LT - 1)
			q.push(mp(Query(1, w.y.x, w.LT - 1, ty), mp(w.y.x * p, w.LT - 1)));
		if (w.LT + 1 < w.RT - 1)
			q.push(mp(Query(1, w.LT + 1, w.RT - 1, ty ^ 1), mp((w.LT + 1) * -p, w.RT - 1)));
		if (w.RT + 1 < w.y.y)
			q.push(mp(Query(1, w.RT + 1, w.y.y, ty), mp((w.RT + 1) * p, w.y.y)));
	}
	return Tohka;
}
inline void solve()
{
	int q;
	scanf("%d", &q);
	for (int i = 1; i <= q; ++i)
	{
		int ty, l, r, k;
//		scanf("%d", &ty);
		ty = Read();
		if (!ty)
		{
//			scanf("%d%d", &l, &k);
			l = Read(); k = Read();
			Ins(1, l, n, k - a[l]);
			a[l] = k;
		}
		else
		{
//			scanf("%d%d%d", &l, &r, &k);
			l = Read(); r = Read(); k = Read();
			printf("%d\n", Ask(l, r, k));
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif

	init();
	solve();

	fclose(stdin);
	fclose(stdout);
	return 0;
}


// Copyright 2018, Skqliao
#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = (l), _##i##_ = (r); i < _##i##_; ++i)
#define rof(i, l, r) for (int i = (l) - 1, _##i##_ = (r); i >= _##i##_; --i)
#define All(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
typedef long long ll;

namespace io {
#ifndef ONLINE_JUDGE
char gc() {
	return getchar();
}
template <class T> inline T gt() {
	static T x;
	std::cin >> x;
	return x;
}
template <typename T> inline void pt(T x, const char c = '\n') {
	std::cout << x << c;
}
void flush() {}
#else
const int MAXSIZE = 1 << 22;
inline char gc() {
	static char In[MAXSIZE], *at = In, *en = In;
	if (at == en) {
		en = (at = In) + fread(In, 1, MAXSIZE, stdin);
	}
	return at == en ? EOF : *at++;
}
template <class T> inline T gt() {
	char c;
	while (c = gc(), !isdigit(c) && c != '-') {}
	bool f = c == '-';
	T x = f ? 0 : c - '0';
	for (c = gc(); isdigit(c); c = gc()) {
		x = x * 10 + c - '0';
	}
	return f ? -x : x;
}
char Out[MAXSIZE], *cur = Out, *end = Out + MAXSIZE - 100;
void flush() {
	fwrite(Out, 1, cur - Out, stdout);
	cur = Out;
}
template <typename T> inline void pt(T x, char c = '\n') {
	static int S[20], *top;
	top = S;
	if (x < 0) {
		*cur++ = '-', x = -x;
	}
	do {
		*++top = x % 10, x /= 10;
	} while (x);
	while (top != S) {
		*cur++ = *top-- + '0';
	}
	*cur++ = c;
	if (cur >= end) {
		flush();
	}
}
#endif
}  // namespace io

const int MAXN = 1e5 + 5;

struct Line {
	int l, r, s;
	Line(int l = 0, int r = 0, int s = 0):
		l(l), r(r), s(s) {}
	Line operator + (const Line &x) {
		return (Line) {l, x.r, s + x.s};
	}
	bool operator < (const Line &x) const {
		return s < x.s;
	}
};

struct Node {
	Line lMax, lMin, rMax, rMin, vMax, vMin, v;
	int rev;
	Node (int l = 0, int r = 0, int s = 0) {
		lMax = lMin = rMax = rMin = vMax = vMin = v = Line(l, r, s);
		rev = 0;
	}
	void pushUp(Node ls, Node rs) {
		if (ls.lMax.l == 0) {
			*this = rs;
			return;
		}
		lMax = std::max(ls.lMax, ls.v + rs.lMax);
		lMin = std::min(ls.lMin, ls.v + rs.lMin);
		rMax = std::max(ls.rMax + rs.v, rs.rMax);
		rMin = std::min(ls.rMin + rs.v, rs.rMin);
		vMax = std::max(ls.rMax, rs.lMax);
		vMax = std::max(vMax, std::max(ls.vMax, rs.vMax));
		vMax = std::max(vMax, ls.rMax + rs.lMax);
		vMax = std::max(vMax, std::max(lMax, rMax));
		vMin = std::min(ls.rMin, rs.lMin);
		vMin = std::min(vMin, std::min(ls.vMin, rs.vMin));
		vMin = std::min(vMin, ls.rMin + rs.lMin);
		vMin = std::min(vMin, std::min(lMin, rMin));
		v = ls.v + rs.v;
	}
} T[MAXN << 2];

#define LT rt << 1, l, m
#define RT rt << 1 | 1, m + 1, r

void rev(Node& x) {
	x.rev ^= 1;
	std::swap(x.lMin, x.lMax);
	std::swap(x.rMin, x.rMax);
	std::swap(x.vMin, x.vMax);
	x.lMin.s *= -1, x.lMax.s *= -1;
	x.rMin.s *= -1, x.rMax.s *= -1;
	x.vMin.s *= -1, x.vMax.s *= -1;
	x.v.s *= -1;
}

void pushDown(int rt) {
	if(T[rt].rev) {
		rev(T[rt << 1]);
		rev(T[rt << 1 | 1]);
		T[rt].rev = 0;
	}
}

void build(int rt, int l, int r) {
	if (l == r) {
		T[rt] = Node(l, r, io::gt<int>());
		return ;
	}
	int m = (l + r) >> 1;
	build(LT);
	build(RT);
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

void modify(int rt, int l, int r, int p, int x) {
	if(l == r) {
		T[rt] = Node(l, r, x);
		return ;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if(p <= m) {
		modify(LT, p, x);
	} else {
		modify(RT, p, x);
	}
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

void reverse(int rt, int l, int r, int a, int b) {
	if(a <= l && r <= b) {
		rev(T[rt]);
		return ;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if(a <= m) {
		reverse(LT, a, b);
	}
	if(m < b) {
		reverse(RT, a, b);
	}
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

Node queryMax(int rt, int l, int r, int a, int b) {
	if(a <= l && r <= b) {
		return T[rt];
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	Node res;
	if(a <= m) {
		res.pushUp(res, queryMax(LT, a, b));
	}
	if(m < b) {
		res.pushUp(res, queryMax(RT, a, b));
	}
	return res;
}

int main() {
	int N = io::gt<int>();
	build(1, 1, N);
	int M = io::gt<int>();
	std::vector<Line> V;
	while(M--) {
		int opt = io::gt<int>();
		if(opt == 0) {
			int p = io::gt<int>(), x = io::gt<int>();
			modify(1, 1, N, p, x);
		} else {
			int l = io::gt<int>(), r = io::gt<int>(), k = io::gt<int>();
			int sum = 0;
			V.clear();
			rep(i, 0, k) {
				Line x = queryMax(1, 1, N, l, r).vMax;
				if(x.s < 0) {
					break;
				}
				sum += x.s;
				V.push_back(x);
				reverse(1, 1, N, x.l, x.r);
			}
			io::pt(sum);
			rep(i, 0, SZ(V)) {
				reverse(1, 1, N, V[i].l, V[i].r);
			}
		}
	}
	io::flush();
	return 0;
}


// Copyright 2018, Skqliao
#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = (l), _##i##_ = (r); i < _##i##_; ++i)
#define rof(i, l, r) for (int i = (l) - 1, _##i##_ = (r); i >= _##i##_; --i)
#define All(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
typedef long long ll;

namespace io {
#ifndef ONLINE_JUDGE
char gc() {
	return getchar();
}
template <class T> inline T gt() {
	static T x;
	std::cin >> x;
	return x;
}
template <typename T> inline void pt(T x, const char c = '\n') {
	std::cout << x << c;
}
void flush() {}
#else
const int MAXSIZE = 1 << 22;
inline char gc() {
	static char In[MAXSIZE], *at = In, *en = In;
	if (at == en) {
		en = (at = In) + fread(In, 1, MAXSIZE, stdin);
	}
	return at == en ? EOF : *at++;
}
template <class T> inline T gt() {
	char c;
	while (c = gc(), !isdigit(c) && c != '-') {}
	bool f = c == '-';
	T x = f ? 0 : c - '0';
	for (c = gc(); isdigit(c); c = gc()) {
		x = x * 10 + c - '0';
	}
	return f ? -x : x;
}
char Out[MAXSIZE], *cur = Out, *end = Out + MAXSIZE - 100;
void flush() {
	fwrite(Out, 1, cur - Out, stdout);
	cur = Out;
}
template <typename T> inline void pt(T x, char c = '\n') {
	static int S[20], *top;
	top = S;
	if (x < 0) {
		*cur++ = '-', x = -x;
	}
	do {
		*++top = x % 10, x /= 10;
	} while (x);
	while (top != S) {
		*cur++ = *top-- + '0';
	}
	*cur++ = c;
	if (cur >= end) {
		flush();
	}
}
#endif
}  // namespace io

const int MAXN = 1e5 + 5;

struct Line {
	int l, r, s;
	Line(int l = 0, int r = 0, int s = 0):
		l(l), r(r), s(s) {}
	Line operator + (const Line &x) {
		return (Line) {l, x.r, s + x.s};
	}
	bool operator < (const Line &x) const {
		return s < x.s;
	}
};

struct Node {
	Line lMax, lMin, rMax, rMin, vMax, vMin, v;
	int rev;
	Node (int l = 0, int r = 0, int s = 0) {
		lMax = lMin = rMax = rMin = vMax = vMin = v = Line(l, r, s);
		rev = 0;
	}
	void pushUp(Node ls, Node rs) {
		if (ls.lMax.l == 0) {
			*this = rs;
			return;
		}
		lMax = std::max(ls.lMax, ls.v + rs.lMax);
		lMin = std::min(ls.lMin, ls.v + rs.lMin);
		rMax = std::max(ls.rMax + rs.v, rs.rMax);
		rMin = std::min(ls.rMin + rs.v, rs.rMin);
		vMax = std::max(ls.rMax, rs.lMax);
		vMax = std::max(vMax, std::max(ls.vMax, rs.vMax));
		vMax = std::max(vMax, ls.rMax + rs.lMax);
		vMax = std::max(vMax, std::max(lMax, rMax));
		vMin = std::min(ls.rMin, rs.lMin);
		vMin = std::min(vMin, std::min(ls.vMin, rs.vMin));
		vMin = std::min(vMin, ls.rMin + rs.lMin);
		vMin = std::min(vMin, std::min(lMin, rMin));
		v = ls.v + rs.v;
	}
} T[MAXN << 2];

#define LT rt << 1, l, m
#define RT rt << 1 | 1, m + 1, r

void rev(Node& x) {
	x.rev ^= 1;
	std::swap(x.lMin, x.lMax);
	std::swap(x.rMin, x.rMax);
	std::swap(x.vMin, x.vMax);
	x.lMin.s *= -1, x.lMax.s *= -1;
	x.rMin.s *= -1, x.rMax.s *= -1;
	x.vMin.s *= -1, x.vMax.s *= -1;
	x.v.s *= -1;
}

void pushDown(int rt) {
	if(T[rt].rev) {
		rev(T[rt << 1]);
		rev(T[rt << 1 | 1]);
		T[rt].rev = 0;
	}
}

void build(int rt, int l, int r) {
	if (l == r) {
		T[rt] = Node(l, r, io::gt<int>());
		return ;
	}
	int m = (l + r) >> 1;
	build(LT);
	build(RT);
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

void modify(int rt, int l, int r, int p, int x) {
	if(l == r) {
		T[rt] = Node(l, r, x);
		return ;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if(p <= m) {
		modify(LT, p, x);
	} else {
		modify(RT, p, x);
	}
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

void reverse(int rt, int l, int r, int a, int b) {
	if(a <= l && r <= b) {
		rev(T[rt]);
		return ;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if(a <= m) {
		reverse(LT, a, b);
	}
	if(m < b) {
		reverse(RT, a, b);
	}
	T[rt].pushUp(T[rt << 1], T[rt << 1 | 1]);
}

Node queryMax(int rt, int l, int r, int a, int b) {
	if(a <= l && r <= b) {
		return T[rt];
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	Node res;
	if(a <= m) {
		res.pushUp(res, queryMax(LT, a, b));
	}
	if(m < b) {
		res.pushUp(res, queryMax(RT, a, b));
	}
	return res;
}

int main() {
	int N = io::gt<int>();
	build(1, 1, N);
	int M = io::gt<int>();
	std::vector<Line> V;
	while(M--) {
		int opt = io::gt<int>();
		if(opt == 0) {
			int p = io::gt<int>(), x = io::gt<int>();
			modify(1, 1, N, p, x);
		} else {
			int l = io::gt<int>(), r = io::gt<int>(), k = io::gt<int>();
			int sum = 0;
			V.clear();
			rep(i, 0, k) {
				Line x = queryMax(1, 1, N, l, r).vMax;
				if(x.s < 0) {
					break;
				}
				sum += x.s;
				V.push_back(x);
				reverse(1, 1, N, x.l, x.r);
			}
			io::pt(sum);
			rof(i, SZ(V), 0) {
				reverse(1, 1, N, V[i].l, V[i].r);
			}
		}
	}
	return 0;
}


#include <iostream>
#include <cmath>

using namespace std;

const int primero=1<<17;
const int tope=1<<18;
const int infinito=1000000000;

void maximiza(int &val,int newval)
{
  if (newval>val) val=newval;
}

int maximo[tope][21][2][2];
int pendiente[tope];

void insertar(int pos,int val)
{
  pos+=primero;
  for (int j=1;j<=20;j++) {
    for (int i0=0;i0<2;i0++)
      for (int i1=0;i1<2;i1++)
	maximo[pos][j][i0][i1]=val;
    maximiza(maximo[pos][j][0][0],0);
  }
  while (pos>1) {
    pos>>=1;
    pendiente[pos]=1;
  }
}

void actualizar(int pos)
{
  if (not pendiente[pos]) return;
  pendiente[pos]=0;
  int pos0=pos<<1;
  int pos1=pos0|1;
  actualizar(pos0);
  actualizar(pos1);
  for (int j=1;j<=20;j++) {
    int (&m)[2][2]=maximo[pos][j];
    for (int i0=0;i0<2;i0++)
      for (int i1=0;i1<2;i1++)
	m[i0][i1]=-infinito;
    m[0][0]=0;
    for (int j0=0;j0<=j;j0++) {
      int j1=j-j0;
      int (&m0)[2][2]=maximo[pos0][j0];
      int (&m1)[2][2]=maximo[pos1][j1];
      maximiza(m[0][0],m0[0][0]+m1[0][0]);
      maximiza(m[0][1],m0[0][0]+m1[0][1]);
      maximiza(m[1][0],m0[1][0]+m1[0][0]);
      maximiza(m[1][1],m0[1][0]+m1[0][1]);
    }
    for (int j0=1;j0<=j;j0++) {
      int j1=j-j0+1;
      int (&m0)[2][2]=maximo[pos0][j0];
      int (&m1)[2][2]=maximo[pos1][j1];
      maximiza(m[0][0],m0[0][1]+m1[1][0]);
      maximiza(m[0][1],m0[0][1]+m1[1][1]);
      maximiza(m[1][0],m0[1][1]+m1[1][0]);
      maximiza(m[1][1],m0[1][1]+m1[1][1]);
    }
  }
}

int maximole[21][2],maximori[21][2];
int nextmaximole[21][2],nextmaximori[21][2];

int maximoentre(int posle,int posri,int k)
{
  posle+=primero;
  posri+=primero;
  if (posle==posri) return maximo[posle][1][1][1];
  for (int j=1;j<=k;j++) {
    maximole[j][0]=maximo[posle][1][1][1];
    maximole[j][1]=maximo[posle][1][1][1];
    maximori[j][0]=maximo[posri][1][1][1];
    maximori[j][1]=maximo[posri][1][1][1];
  }
  while (posle+1<posri) {
    if ((posle&1)==0) {
      int pos1=posle+1;
      actualizar(pos1);
      for (int j=1;j<=k;j++) {
	for (int i0=0;i0<2;i0++)
	  nextmaximole[j][i0]=0;
	for (int j0=0;j0<=j;j0++) {
	  int j1=j-j0;
	  int (&m1)[2][2]=maximo[pos1][j1];
	  maximiza(nextmaximole[j][0],maximole[j0][0]+m1[0][0]);
	  maximiza(nextmaximole[j][1],maximole[j0][0]+m1[0][1]);
	}
	for (int j0=1;j0<=j;j0++) {
	  int j1=j-j0+1;
	  int (&m1)[2][2]=maximo[pos1][j1];
	  maximiza(nextmaximole[j][0],maximole[j0][1]+m1[1][0]);
	  maximiza(nextmaximole[j][1],maximole[j0][1]+m1[1][1]);
	}
      }
      for (int j=1;j<=k;j++) {
	maximole[j][0]=nextmaximole[j][0];
	maximole[j][1]=nextmaximole[j][1];
      }
    }
    if (posri&1) {
      int pos0=posri-1;
      actualizar(pos0);
      for (int j=1;j<=k;j++) {
	for (int i0=0;i0<2;i0++)
	  nextmaximori[j][i0]=0;
	for (int j0=0;j0<=j;j0++) {
	  int j1=j-j0;
	  int (&m0)[2][2]=maximo[pos0][j0];
	  maximiza(nextmaximori[j][0],m0[0][0]+maximori[j1][0]);
	  maximiza(nextmaximori[j][1],m0[1][0]+maximori[j1][0]);
	}
	for (int j0=1;j0<=j;j0++) {
	  int j1=j-j0+1;
	  int (&m0)[2][2]=maximo[pos0][j0];
	  maximiza(nextmaximori[j][0],m0[0][1]+maximori[j1][1]);
	  maximiza(nextmaximori[j][1],m0[1][1]+maximori[j1][1]);
	}
      }
      for (int j=1;j<=k;j++) {
	maximori[j][0]=nextmaximori[j][0];
	maximori[j][1]=nextmaximori[j][1];
      }
    }
    posle>>=1;
    posri>>=1;
  }
  int sol=0;
  {
    int j=k;
    for (int j0=0;j0<=j;j0++) {
      int j1=j-j0;
      maximiza(sol,max(maximole[j0][0]+max(maximori[j1][0],maximori[j1][1]),
		       maximole[j0][1]+maximori[j1][0]));
    }
    for (int j0=1;j0<=j;j0++) {
      int j1=j-j0+1;
      maximiza(sol,maximole[j0][1]+maximori[j1][1]);
    }
  }
  return sol;
}

int main()
{
  ios::sync_with_stdio(false);
  for (int pos=1;pos<tope;pos++)
    maximo[pos][0][1][1]=maximo[pos][0][0][1]=maximo[pos][0][1][0]=-infinito;
  int n;
  cin>>n;
  for (int i=1;i<=n;i++) {
    int a;
    cin>>a;
    insertar(i,a);
  }
  int casos;
  cin>>casos;
  for (int cas=0;cas<casos;cas++) {
    int tipo;
    cin>>tipo;
    if (tipo==0) {
      int i,val;
      cin>>i>>val;
      insertar(i,val);
    } else {
      int l,r,k;
      cin>>l>>r>>k;
      cout<<maximoentre(l,r,k)<<endl;
    }
  }
}