#include<cstdio>
#define MAXN 100010
using namespace std;
struct Tree
{
	int l,r;
	long long sum;
}tree[MAXN*3];
int N,Q,A[MAXN];
void Pushup(int o)
{
	tree[o].sum=tree[o<<1].sum+tree[o<<1|1].sum;
}
void build(int o,int l,int r)
{
	tree[o].l=l,tree[o].r=r;
	if(l==r)
	{
		tree[o].sum=A[l];
		return;
	}
	int mid=(l+r)>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
	Pushup(o);
}
void changepoint(int o,int chp,int x)
{
	int L=tree[o].l,R=tree[o].r;
	if(chp<L || R<chp)return;
	if(L == R && chp == L)
	{
		tree[o].sum=x;
		return;
	}
	int mid=(L+R)>>1;
	if(chp<=mid) changepoint(o<<1,chp,x);
	else changepoint(o<<1|1,chp,x);
	Pushup(o);
}
void modulo(int o,int l,int r,int x)
{
	int L=tree[o].l,R=tree[o].r;
	if(r<L || R<l)return;
	if(L==R && l<=L && R<=r)
	{
		tree[o].sum%=x;
		return;
	}
	int mid=(L+R)>>1;
	if(r<=mid)modulo(o<<1,l,r,x);
	else if(mid<l) modulo(o<<1|1,l,r,x);
	else
	{
		modulo(o<<1,l,mid,x);
		modulo(o<<1|1,mid+1,r,x);
	}
	Pushup(o);
}
long long getsum(int o,int l,int r)
{
	int L=tree[o].l,R=tree[o].r;
	if(r<L || R<l)return 0;
	if(L==l && R==r)return tree[o].sum;
	int mid=(L+R)>>1;
	if(r<=mid)return getsum(o<<1,l,r);
	if(mid<l) return getsum(o<<1|1,l,r);
	return (long long)(getsum(o<<1,l,mid)+getsum(o<<1|1,mid+1,r));
}
int main()
{
	scanf("%d%d",&N,&Q);
	for(int i=1;i<=N;i++)scanf("%d",&A[i]);
	build(1,1,N);
	while(Q--)
	{
		int type;
		scanf("%d",&type);
		if(3 == type)
		{
			int k,x;
			scanf("%d%d",&k,&x);
			changepoint(1,k,x);
		}
		else if(2 == type)
		{
			int l,r,x;
			scanf("%d%d%d",&l,&r,&x);
			modulo(1,l,r,x);
		}
		else if(1 == type)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			printf("%I64d\n",getsum(1,l,r));
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>
using namespace std;
#define ll long long int
#define MAX 100005
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int a[MAX], mi[MAX<<2], ma[MAX<<2], lz[MAX<<2];
ll st[MAX<<2];
void build(int si, int ss, int se) {
	if(ss == se) {
		st[si] = a[ss];
		mi[si] = ma[si] = a[ss];
		return;
	}
	int mid = (ss+se)>>1;
	build(si<<1,ss,mid);
	build(si<<1|1,mid+1,se);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
void push(int si, int ss, int se) {
	st[si] = 1LL*lz[si]*(se-ss+1);
	ma[si] = mi[si] = lz[si];
	lz[si<<1] = lz[si<<1|1] = lz[si];
	lz[si] = -1;
}
void updm(int si, int ss, int se, int qs, int qe, int x) {
	if(lz[si] != -1) push(si,ss,se);
	//cerr<<ss<<" "<<se<<" - "<<ma[si]<<" "<<mi[si]<<endl;
	if(qs > se or qe < ss or ma[si] < x) return;
	if(qs<=ss and se<=qe and mi[si] == ma[si]) {
		lz[si] = ma[si]%x;
		push(si,ss,se);
		//cerr<<ss<<" "<<se<<" - "<<st[si]<<endl;
		return;
	}
	int mid = (ss+se)>>1;
	updm(si<<1,ss,mid,qs,qe,x);
	updm(si<<1|1,mid+1,se,qs,qe,x);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
void updp(int si, int ss, int se, int pos, int val) {
	if(lz[si] != -1) push(si,ss,se);
	if(pos>se or pos<ss) return;
	if(ss == se) {
		st[si] = mi[si] = ma[si] = val;
		return;
	}
	int mid = (ss+se)>>1;
	updp(si<<1,ss,mid,pos,val);
	updp(si<<1|1,mid+1,se,pos,val);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
ll que(int si, int ss, int se, int qs, int qe) {
	if(lz[si] != -1) push(si,ss,se);
	if(qs > se or qe < ss) return 0;
	if(qs<=ss and se<=qe) {
		//cerr<<ss<<" "<<se<<" "<<st[si]<<endl;
		return st[si];
	}
	int mid = (ss+se)>>1;
	ll val = que(si<<1,ss,mid,qs,qe) + que(si<<1|1,mid+1,se,qs,qe);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
	return val;
}
int main()
{
	int n,m,i,j,typ,x,l,r;
	for(i=0;i<MAX*4;++i) lz[i] = -1;

	scanf("%d %d",&n,&m);
	for(i=1;i<=n;++i)
		scanf("%d",a+i);
	build(1,1,n);
	while(m--) {
		scanf("%d",&typ);
		if(typ == 1) {
			scanf("%d %d",&l,&r);
			printf("%I64d\n",que(1,1,n,l,r));
		} else if(typ == 2) {
			scanf("%d %d %d",&l,&r,&x);
			updm(1,1,n,l,r,x);
		} else {
			scanf("%d %d",&r,&x);
			updp(1,1,n,r,x);
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>
using namespace std;
#define ll long long int
#define MAX 100005
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int a[MAX], mi[MAX<<2], ma[MAX<<2], lz[MAX<<2];
ll st[MAX<<2];
void upd(int si) {
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1] + st[si<<1|1];
}
void build(int si, int ss, int se) {
	lz[si] = -1;
	if(ss == se) {
		st[si] = a[ss];
		mi[si] = ma[si] = a[ss];
		return;
	}
	int mid = (ss+se)>>1;
	build(si<<1,ss,mid);
	build(si<<1|1,mid+1,se);
	upd(si);
}
void push(int si, int ss, int se) {
	st[si] = 1LL*lz[si]*(se-ss+1);
	ma[si] = mi[si] = lz[si];
	if(ss^se) lz[si<<1] = lz[si<<1|1] = lz[si];
	lz[si] = -1;
}
void updm(int si, int ss, int se, int qs, int qe, int x) {
	if(lz[si] != -1) push(si,ss,se);
	if(qs > se or qe < ss or ma[si] < x) return;
	if(qs<=ss and se<=qe and mi[si] == ma[si]) {
		lz[si] = ma[si]%x;
		push(si,ss,se);
		return;
	}
	int mid = (ss+se)>>1;
	updm(si<<1,ss,mid,qs,qe,x);
	updm(si<<1|1,mid+1,se,qs,qe,x);
	upd(si);
}
void updp(int si, int ss, int se, int pos, int val) {
	if(lz[si] != -1) push(si,ss,se);
	if(pos>se or pos<ss) return;
	if(ss == se) {
		st[si] = mi[si] = ma[si] = val;
		return;
	}
	int mid = (ss+se)>>1;
	updp(si<<1,ss,mid,pos,val);
	updp(si<<1|1,mid+1,se,pos,val);
	upd(si);
}
ll que(int si, int ss, int se, int qs, int qe) {
	if(lz[si] != -1) push(si,ss,se);
	if(qs > se or qe < ss) return 0;
	if(qs<=ss and se<=qe) {
		return st[si];
	}
	int mid = (ss+se)>>1;
	ll val = que(si<<1,ss,mid,qs,qe) + que(si<<1|1,mid+1,se,qs,qe);
	upd(si);
	return val;
}
int main()
{
	int n,m,i,j,typ,x,l,r;
	scanf("%d %d",&n,&m);
	for(i=1;i<=n;++i)
		scanf("%d",a+i);
	build(1,1,n);
	while(m--) {
		scanf("%d",&typ);
		if(typ == 1) {
			scanf("%d %d",&l,&r);
			printf("%I64d\n",que(1,1,n,l,r));
		} else if(typ == 2) {
			scanf("%d %d %d",&l,&r,&x);
			updm(1,1,n,l,r,x);
		} else {
			scanf("%d %d",&r,&x);
			updp(1,1,n,r,x);
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>
using namespace std;
#define ll long long int
#define MAX 100005
int a[MAX];
int main()
{
	int n,m,i,j,typ,x,l,r;
	scanf("%d %d",&n,&m);
	for(i=1;i<=n;++i)
		scanf("%d",a+i);
	while(m--) {
		scanf("%d",&typ);
		if(typ == 1) {
			scanf("%d %d",&l,&r);
			ll s = 0;
			for(i=l;i<=r;++i)
				s += a[i];
			printf("%I64d\n",s);
		} else if(typ == 2) {
			scanf("%d %d %d",&l,&r,&x);
			for(i=l;i<=r;++i)
				a[i] = a[i]>x?a[i]%x:a[i];
		} else {
			scanf("%d %d",&r,&x);
				a[r] = x;
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>
using namespace std;
#define ll long long int
#define MAX 100005
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int a[MAX], mi[MAX<<2], ma[MAX<<2], lz[MAX];
ll st[MAX<<2];
void build(int si, int ss, int se) {
	if(ss == se) {
		st[si] = a[ss];
		mi[si] = ma[si] = a[ss];
		return;
	}
	int mid = (ss+se)>>1;
	build(si<<1,ss,mid);
	build(si<<1|1,mid+1,se);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
void push(int si, int ss, int se) {
	st[si] = 1LL*lz[si]*(se-ss+1);
	ma[si] = mi[si] = lz[si];
	lz[si<<1] = lz[si<<1|1] = lz[si];
	lz[si] = 0;
}
void updm(int si, int ss, int se, int qs, int qe, int x) {
	if(lz[si]) push(si,ss,se);
	//cerr<<ss<<" "<<se<<" - "<<ma[si]<<" "<<mi[si]<<endl;
	if(qs > se or qe < ss or ma[si] < x) return;
	if(qs<=ss and se<=qe and mi[si] == ma[si]) {
		lz[si] = ma[si]%x;
		push(si,ss,se);
		//cerr<<ss<<" "<<se<<" - "<<st[si]<<endl;
		return;
	}
	int mid = (ss+se)>>1;
	updm(si<<1,ss,mid,qs,qe,x);
	updm(si<<1|1,mid+1,se,qs,qe,x);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
void updp(int si, int ss, int se, int pos, int val) {
	if(lz[si]) push(si,ss,se);
	if(pos>se or pos<ss) return;
	if(ss == se) {
		st[si] = mi[si] = ma[si] = val;
		return;
	}
	int mid = (ss+se)>>1;
	updp(si<<1,ss,mid,pos,val);
	updp(si<<1|1,mid+1,se,pos,val);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
ll que(int si, int ss, int se, int qs, int qe) {
	if(lz[si]) push(si,ss,se);
	if(qs > se or qe < ss) return 0;
	if(qs<=ss and se<=qe) {
		//cerr<<ss<<" "<<se<<" "<<st[si]<<endl;
		return st[si];
	}
	int mid = (ss+se)>>1;
	ll val = que(si<<1,ss,mid,qs,qe) + que(si<<1|1,mid+1,se,qs,qe);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
	return val;
}
int main()
{
	int n,m,i,j,typ,x,l,r;
	scanf("%d %d",&n,&m);
	for(i=1;i<=n;++i)
		scanf("%d",a+i);
	build(1,1,n);
	while(m--) {
		scanf("%d",&typ);
		if(typ == 1) {
			scanf("%d %d",&l,&r);
			printf("%I64d\n",que(1,1,n,l,r));
		} else if(typ == 2) {
			scanf("%d %d %d",&l,&r,&x);
			updm(1,1,n,l,r,x);
		} else {
			scanf("%d %d",&r,&x);
			updp(1,1,n,r,x);
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>
using namespace std;
#define ll long long int
#define MAX 100005
int a[MAX];
int main()
{
	int n,m,i,j,typ,x,l,r;
	scanf("%d %d",&n,&m);
	for(i=1;i<=n;++i)
		scanf("%d",a+i);
	while(m--) {
		scanf("%d",&typ);
		if(typ == 1) {
			scanf("%d %d",&l,&r);
			ll s = 0;
			for(i=l;i<=r;++i)
				s += a[i];
			printf("%I64d\n",s);
		} else if(typ == 2) {
			scanf("%d %d %d",&l,&r,&x);
			for(i=l;i<=r;++i)
				a[i] = a[i]>=x?a[i]%x:a[i];
		} else {
			scanf("%d %d",&r,&x);
				a[r] = x;
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>
using namespace std;
#define ll long long int
#define MAX 100005
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int a[MAX], mi[MAX<<2], ma[MAX<<2], lz[MAX<<2];
ll st[MAX<<2];
void build(int si, int ss, int se) {
	if(ss == se) {
		st[si] = a[ss];
		mi[si] = ma[si] = a[ss];
		return;
	}
	int mid = (ss+se)>>1;
	build(si<<1,ss,mid);
	build(si<<1|1,mid+1,se);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
void push(int si, int ss, int se) {
	st[si] = 1LL*lz[si]*(se-ss+1);
	ma[si] = mi[si] = lz[si];
	lz[si<<1] = lz[si<<1|1] = lz[si];
	lz[si] = 0;
}
void updm(int si, int ss, int se, int qs, int qe, int x) {
	if(lz[si]) push(si,ss,se);
	//cerr<<ss<<" "<<se<<" - "<<ma[si]<<" "<<mi[si]<<endl;
	if(qs > se or qe < ss or ma[si] < x) return;
	if(qs<=ss and se<=qe and mi[si] == ma[si]) {
		lz[si] = ma[si]%x;
		push(si,ss,se);
		//cerr<<ss<<" "<<se<<" - "<<st[si]<<endl;
		return;
	}
	int mid = (ss+se)>>1;
	updm(si<<1,ss,mid,qs,qe,x);
	updm(si<<1|1,mid+1,se,qs,qe,x);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
void updp(int si, int ss, int se, int pos, int val) {
	if(lz[si]) push(si,ss,se);
	if(pos>se or pos<ss) return;
	if(ss == se) {
		st[si] = mi[si] = ma[si] = val;
		return;
	}
	int mid = (ss+se)>>1;
	updp(si<<1,ss,mid,pos,val);
	updp(si<<1|1,mid+1,se,pos,val);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
}
ll que(int si, int ss, int se, int qs, int qe) {
	if(lz[si]) push(si,ss,se);
	if(qs > se or qe < ss) return 0;
	if(qs<=ss and se<=qe) {
		//cerr<<ss<<" "<<se<<" "<<st[si]<<endl;
		return st[si];
	}
	int mid = (ss+se)>>1;
	ll val = que(si<<1,ss,mid,qs,qe) + que(si<<1|1,mid+1,se,qs,qe);
	mi[si] = min(mi[si<<1], mi[si<<1|1]);
	ma[si] = max(ma[si<<1], ma[si<<1|1]);
	st[si] = st[si<<1]+st[si<<1|1];
	return val;
}
int main()
{
	int n,m,i,j,typ,x,l,r;
	scanf("%d %d",&n,&m);
	for(i=1;i<=n;++i)
		scanf("%d",a+i);
	build(1,1,n);
	while(m--) {
		scanf("%d",&typ);
		if(typ == 1) {
			scanf("%d %d",&l,&r);
			printf("%I64d\n",que(1,1,n,l,r));
		} else if(typ == 2) {
			scanf("%d %d %d",&l,&r,&x);
			updm(1,1,n,l,r,x);
		} else {
			scanf("%d %d",&r,&x);
			updp(1,1,n,r,x);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 3000000+100
#define inf 2147483647
#define LL long long
using namespace std;
int n,m,a[N],bl[N],f,bansum[N];
inline int read()
{
    int f=1,x=0;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 getsum(int l,int r)
{
    int sum=0;
    if(bl[l]==bl[r])
    {
        for(int i=l;i<=r;i++)
        {
            sum+=a[i];
        }
    }
    else
    {
        for(int i=l;i<=bl[l]*f;i++)
        {
            sum+=a[i];
        }
        for(int i=(bl[r]-1)*f+1;i<=r;i++)
        {
            sum+=a[i];
        }
        for(int i=bl[l]+1;i<=bl[r]-1;i++)
        {
            sum+=bansum[i];
        }
    }
    return sum;
}
void getmod(int l,int r,int z)
{
    for(int i=l;i<=r;i++)
    {
        if(z>a[i])continue;
        bansum[bl[i]]+=(a[i]%z-a[i]);
        a[i]%=z;
    }
}
void getchange(int l,int z)
{
    bansum[bl[l]]+=(z-a[l]);
    a[l]=z;
}
int main()
{
    n=read(),m=read();
    f=sqrt(n);
    for(int i=1;i<=n;i++)
    {
        bl[i]=(i-1)/f+1;
        a[i]=read();
        bansum[bl[i]]+=a[i];
    }
    for(int i=1;i<=m;i++)
    {
        int x,l,r,z;
        x=read();
        if(x==1)
        {
            l=read(),r=read();
            printf("%d\n",getsum(l,r));
        }
        else if(x==2)
        {
            l=read(),r=read(),z=read();
            getmod(l,r,z);
        }
        else if(x==3)
        {
            l=read(),z=read();
            getchange(l,z);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 200000+100
using namespace std;
inline int read()
{
    int f=1,x=0;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 f*x;
}
struct node
{
    int left;
    int right;
    int mx;
    long long sum;
}a[4*N];
int n,m,data[N];
void pushup(int p)
{
    a[p].sum=a[2*p].sum+a[2*p+1].sum;
    a[p].mx=max(a[2*p].mx,a[2*p+1].mx);
}
void build(int p,int l,int r)
{
    a[p].left=l;
    a[p].right=r;
    if(l==r){a[p].sum=a[p].mx=data[l];return;}
    int mid=(l+r)/2;
    build(2*p,l,mid);
    build(2*p+1,mid+1,r);
    pushup(p);
}
void update(int p,int l,int r,int d)
{
    if(a[p].left==l&&a[p].right==r){a[p].sum=a[p].mx=d;return;}
    int mid=(a[p].left+a[p].right)/2;
    if(r<=mid)update(2*p,l,r,d);
    else if(l>mid)update(2*p+1,l,r,d);
    else {update(2*p,l,mid,d);update(2*p+1,mid+1,r,d);}
    pushup(p);
}
void getmod(int p,int l,int r,int d)
{
    if(a[p].mx<d)
        return;
    if(a[p].left==a[p].right){a[p].sum%=d,a[p].mx%=d;return;}
    int mid=(a[p].left+a[p].right)/2;
    if(r<=mid)getmod(2*p,l,r,d);
    else if(l>mid)getmod(2*p+1,l,r,d);
    else {getmod(2*p,l,mid,d);getmod(2*p+1,mid+1,r,d);}
    pushup(p);
}
long long query(int p,int l,int r)
{
    if(a[p].left==l&&a[p].right==r)return a[p].sum;
    int mid=(a[p].left+a[p].right)/2;
    if(r<=mid)return query(2*p,l,r);
    else if(l>mid)return query(2*p+1,l,r);
    else return query(2*p,l,mid)+query(2*p+1,mid+1,r);
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)data[i]=read();
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        int x,y,z,k;
        z=read();
        if(z==1)
        {
            x=read(),y=read();
            printf("%lld\n",query(1,x,y));
        }
        else if(z==2)
        {
            x=read(),y=read(),k=read();
            getmod(1,x,y,k);
        }
        else if(z==3)
        {
            x=read(),y=read();
            update(1,x,x,y);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 200000+100
using namespace std;
inline int read()
{
    int f=1,x=0;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 f*x;
}
struct node
{
    int left;
    int right;
    long long val;
    long long sum;
}a[4*N];
int n,m,data[N];
void pushup(int p){a[p].sum=a[2*p].sum+a[2*p+1].sum;}
void build(int p,int l,int r)
{
    a[p].left=l;
    a[p].right=r;
    if(l==r){a[p].sum=data[l];return;}
    int mid=(l+r)/2;
    build(2*p,l,mid);
    build(2*p+1,mid+1,r);
    pushup(p);
}
void update(int p,int l,int r,int d)
{
    if(a[p].left==l&&a[p].right==r){a[p].sum=d;return;}
    int mid=(a[p].left+a[p].right)/2;
    if(r<=mid)update(2*p,l,r,d);
    else if(l>mid)update(2*p+1,l,r,d);
    else {update(2*p,l,mid,d);update(2*p+1,mid+1,r,d);}
    pushup(p);
}
void getmod(int p,int l,int r,int d)
{
    if(a[p].left==a[p].right){a[p].sum%=d;return ;}
    int mid=(a[p].left+a[p].right)/2;
    if(r<=mid)getmod(2*p,l,r,d);
    else if(l>mid)getmod(2*p+1,l,r,d);
    else {getmod(2*p,l,mid,d);getmod(2*p+1,mid+1,r,d);}
    pushup(p);
}
long long query(int p,int l,int r)
{
    if(a[p].left==l&&a[p].right==r)return a[p].sum;
    int mid=(a[p].left+a[p].right)/2;
    if(r<=mid)return query(2*p,l,r);
    else if(l>mid)return query(2*p+1,l,r);
    else return query(2*p,l,mid)+query(2*p+1,mid+1,r);
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)data[i]=read();
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        int x,y,z,k;
        z=read();
        if(z==1)
        {
            x=read(),y=read();
            printf("%lld\n",query(1,x,y));
        }
        else if(z==2)
        {
            x=read(),y=read(),k=read();
            getmod(1,x,y,k);
        }
        else if(z==3)
        {
            x=read(),y=read();
            update(1,x,x,y);
        }
    }
    return 0;
}
/*
10 10
6 9 6 7 6 1 10 10 9 5
1 3 9
2 7 10 9
2 5 10 8
1 4 7
3 3 7
2 7 9 9
1 2 4
1 6 6
1 5 9
3 1 10
*/


#include <bits/stdc++.h>
#define N 3000000+100
#define inf 2147483647
#define LL long long
using namespace std;
int n,m,a[N],bl[N],f;
long long bansum[N];
inline int read()
{
    int f=1,x=0;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;
}
long long getsum(int l,int r)
{
    long long sum=0;
    if(bl[l]==bl[r])
    {
        for(int i=l;i<=r;i++)
        {
            sum+=a[i];
        }
    }
    else
    {
        for(int i=l;i<=bl[l]*f;i++)
        {
            sum+=a[i];
        }
        for(int i=(bl[r]-1)*f+1;i<=r;i++)
        {
            sum+=a[i];
        }
        for(int i=bl[l]+1;i<=bl[r]-1;i++)
        {
            sum+=bansum[i];
        }
    }
    return sum;
}
void getmod(int l,int r,int z)
{
    for(int i=l;i<=r;i++)
    {
        if(z>a[i])continue;
        bansum[bl[i]]+=(a[i]%z-a[i]);
        a[i]%=z;
    }
}
void getchange(int l,int z)
{
    bansum[bl[l]]+=(z-a[l]);
    a[l]=z;
}
int main()
{
    n=read(),m=read();
    f=sqrt(n);
    for(int i=1;i<=n;i++)
    {
        bl[i]=(i-1)/f+1;
        a[i]=read();
        bansum[bl[i]]+=a[i];
    }
    for(int i=1;i<=m;i++)
    {
        int x,l,r,z;
        x=read();
        if(x==1)
        {
            l=read(),r=read();
            cout<<getsum(l,r)<<endl;
        }
        else if(x==2)
        {
            l=read(),r=read(),z=read();
            getmod(l,r,z);
        }
        else if(x==3)
        {
            l=read(),z=read();
            getchange(l,z);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define N 3000000+100
#define inf 2147483647
#define LL long long
using namespace std;
int n,m,a[N],bl[N],f;
long long bansum[N];
inline int read()
{
    int f=1,x=0;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;
}
long long getsum(int l,int r)
{
    long long sum=0;
    if(bl[l]==bl[r])
    {
        for(int i=l;i<=r;i++)
        {
            sum+=a[i];
        }
    }
    else
    {
        for(int i=l;i<=bl[l]*f;i++)
        {
            sum+=a[i];
        }
        for(int i=(bl[r]-1)*f+1;i<=r;i++)
        {
            sum+=a[i];
        }
        for(int i=bl[l]+1;i<=bl[r]-1;i++)
        {
            sum+=bansum[i];
        }
    }
    return sum;
}
void getmod(int l,int r,int z)
{
    for(int i=l;i<=r;i++)
    {
        if(z>a[i])continue;
        bansum[bl[i]]+=(a[i]%z-a[i]);
        a[i]%=z;
    }
}
void getchange(int l,int z)
{
    bansum[bl[l]]+=(z-a[l]);
    a[l]=z;
}
int main()
{
    n=read(),m=read();
    f=sqrt(n);
    for(int i=1;i<=n;i++)
    {
        bl[i]=(i-1)/f+1;
        a[i]=read();
        bansum[bl[i]]+=a[i];
    }
    for(int i=1;i<=m;i++)
    {
        int x,l,r,z;
        x=read();
        if(x==1)
        {
            l=read(),r=read();
            printf("%lld\n",getsum(l,r));
        }
        else if(x==2)
        {
            l=read(),r=read(),z=read();
            getmod(l,r,z);
        }
        else if(x==3)
        {
            l=read(),z=read();
            getchange(l,z);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct Tree{
    long long left,right,sum,val,mma;
};
long long a[111211];
Tree tree[511121];
long long max(long long a,long long b)
{
    return a>b?a:b;
}
long long min(long long a,long long b)
{
    return a<b?a:b;
}
void pushup(int u)
{
    tree[u].sum = tree[u*2].sum + tree[u*2+1].sum ;
    tree[u].mma = max(tree[u].mma,max(tree[u*2].mma,tree[u*2+1].mma));
}
long long ans ;
void build(int l,int r,int u)
{
    tree[u].left = l;
    tree[u].right = r;
    tree[u].mma = 0;
    if(l==r)
    {
        tree[u].sum = a[l];
        tree[u].val = a[l];
        tree[u].mma = a[l];
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,u*2);
    build(m+1,r,u*2+1);
    pushup(u);
}
void update(int x,int y,int z,int u)
{
    if(tree[u].mma<z) return ;
    if(tree[u].left==tree[u].right)
    {
        int t = tree[u].left ;
        a[t] %= z ;
        tree[u].mma = a[t];
        tree[u].val = a[t] ;
        tree[u].sum = a[t] ;
        return ;
    }
    if(tree[u].left==tree[u].right)  return ;
    int m = (tree[u].left + tree[u].right)>>1;
    if(x<=m)  update(x,min(m,y),z,u*2);
    if(y>m) update(max(x,m+1),y,z,u*2+1);
    pushup(u);
}
void update2(int pos,int c,int u)
{
    if(tree[u].left==tree[u].right)
    {
        if(tree[u].left==pos)
        {
            a[pos] = c ;
            tree[u].mma = a[pos] ;
            tree[u].val = a[pos] ;
            tree[u].sum = a[pos] ;
            return ;
        }
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(pos<=m)  update2(pos,c,u*2);
    else  update2(pos,c,u*2+1);
    pushup(u);
}
void query(int l,int r,int u)
{
    if(tree[u].left==l&&tree[u].right==r)
    {
        ans += tree[u].sum ;
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(r<=m)
        query(l,r,u*2);
    else if(l>m)
        query(l,r,u*2+1);
    else
    {
        query(l,m,u*2);
        query(m+1,r,u*2+1);
    }
}
int main()
{
    int n,m;
    int i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    scanf("%I64d",&a[i]);
    build(1,n,1);
    int k,x,y,z;
    int j;
    for(i=1;i<=m;i++)
    {
        scanf("%d",&k);
        if(k==1)
        {
            ans = 0;
            scanf("%d%d",&x,&y);
            query(x,y,1);
            printf("%I64d\n",ans);
        }
        else if(k==2)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(x,y,z,1);
        }
        else if(k==3)
        {
            scanf("%d%d",&x,&y);
            update2(x,y,1);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct Tree{
	long long left,right,sum,val,mma;
};
long long a[111211];
Tree tree[5111121];
long long max(long long a,long long b)
{
	return a>b?a:b;
}
long long min(long long a,long long b)
{
	return a<b?a:b;
}
void pushup(int u)
{
	tree[u].sum = tree[u*2].sum + tree[u*2+1].sum ;
	tree[u].mma = max(tree[u].mma,max(tree[u*2].mma,tree[u*2+1].mma));
}
long long ans ;
void build(int l,int r,int u)
{
	tree[u].left = l;
	tree[u].right = r;
	tree[u].mma = 0;
	if(l==r)
	{
		tree[u].sum = a[l];
		tree[u].mma = a[l];
		return ;
	}
	int m = (l+r)>>1;
	build(l,m,u*2);
	build(m+1,r,u*2+1);
	pushup(u);
}
void update(int x,int y,int z,int u)
{
	if(tree[u].mma<z) return ;
	if(tree[u].left==tree[u].right)
	{
		int t = tree[u].left ;
		a[t] %= z ;
		tree[u].mma = a[t];
		tree[u].sum = a[t] ;
		return ;
	}
	int m = (tree[u].left + tree[u].right)>>1;
	if(y<=m)  update(x,y,z,u*2);
	else if(x>m) update(x,y,z,u*2+1);
	else
	{
		update(x,m,z,u*2);
		update(m+1,y,z,u*2+1);
	}
	pushup(u);
}
void update2(int pos,int c,int u)
{
	if(tree[u].left==pos)
	{
		a[pos] = c ;
		tree[u].mma = a[pos] ;
		tree[u].sum = a[pos] ;
		return ;
	}
	int m = (tree[u].left + tree[u].right)>>1;
	if(pos<=m)  update2(pos,c,u*2);
	else  update2(pos,c,u*2+1);
	pushup(u);
}
void query(int l,int r,int u)
{
	if(tree[u].left==l&&tree[u].right==r)
	{
		ans += tree[u].sum ;
		return ;
	}
	int m = (tree[u].left + tree[u].right)>>1;
	if(r<=m)
		query(l,r,u*2);
	else if(l>m)
		query(l,r,u*2+1);
	else
	{
		query(l,m,u*2);
		query(m+1,r,u*2+1);
	}
}
int main()
{
	int n,m;
	int i;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	scanf("%I64d",&a[i]);
	build(1,n,1);
	int k,x,y,z;
	int j;
	for(i=1;i<=m;i++)
	{
		scanf("%d",&k);
		if(k==1)
		{
			ans = 0;
			scanf("%d%d",&x,&y);
			query(x,y,1);
			printf("%I64d\n",ans);
		}
		else if(k==2)
		{
			scanf("%d%d%d",&x,&y,&z);
			update(x,y,z,1);
		}
		else if(k==3)
		{
			scanf("%d%d",&x,&y);
			update2(x,y,1);
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct Tree{
    long long left,right,sum,mma;
};
long long a[111211];
Tree tree[5111121];
long long max(long long a,long long b)
{
    return a>b?a:b;
}
long long min(long long a,long long b)
{
    return a<b?a:b;
}
void pushup(int u)
{
    tree[u].sum = tree[u*2].sum + tree[u*2+1].sum ;
    tree[u].mma = max(tree[u].mma,max(tree[u*2].mma,tree[u*2+1].mma));
}
long long ans ;
void build(int l,int r,int u)
{
    tree[u].left = l;
    tree[u].right = r;
    if(l==r)
    {
        tree[u].sum = a[l];
        tree[u].mma = a[l];
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,u*2);
    build(m+1,r,u*2+1);
    pushup(u);
}
void update(int x,int y,int z,int u)
{
    if(tree[u].mma<z) return ;
    if(tree[u].left==tree[u].right)
    {
        int t = tree[u].left ;
        a[t] %= z ;
        tree[u].mma = a[t];
        tree[u].sum = a[t] ;
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(y<=m)  update(x,y,z,u*2);
    else if(x>m) update(x,y,z,u*2+1);
    else
    {
        update(x,m,z,u*2);
        update(m+1,y,z,u*2+1);
    }
    pushup(u);
}
void update2(int pos,int c,int u)
{
    if(tree[u].left==pos&&tree[u].right==tree[u].left)
    {
        a[pos] = c ;
        tree[u].mma = a[pos] ;
        tree[u].sum = a[pos] ;
        return ;
    }
    if(tree[u].left==tree[u].right)  return ;
    int m = (tree[u].left + tree[u].right)>>1;
    if(pos<=m)  update2(pos,c,u*2);
    else  update2(pos,c,u*2+1);
    pushup(u);
}
void query(int l,int r,int u)
{
    if(tree[u].left==l&&tree[u].right==r)
    {
        ans += tree[u].sum ;
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(r<=m)
        query(l,r,u*2);
    else if(l>m)
        query(l,r,u*2+1);
    else
    {
        query(l,m,u*2);
        query(m+1,r,u*2+1);
    }
}
int main()
{
    int n,m;
    int i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    scanf("%I64d",&a[i]);
    build(1,n,1);
    int k,x,y,z;
    int j;
    for(i=1;i<=m;i++)
    {
        scanf("%d",&k);
        if(k==1)
        {
            ans = 0;
            scanf("%d%d",&x,&y);
            query(x,y,1);
            printf("%I64d\n",ans);
        }
        else if(k==2)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(x,y,z,1);
        }
        else if(k==3)
        {
            scanf("%d%d",&x,&y);
            update2(x,y,1);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct Tree{
    long long left,right,sum,val,mma;
};
long long a[11111];
Tree tree[51111];
long long max(long long a,long long b)
{
    return a>b?a:b;
}
long long min(long long a,long long b)
{
    return a<b?a:b;
}
void pushup(int u)
{
    tree[u].sum = tree[u*2].sum + tree[u*2+1].sum ;
    tree[u].mma = max(tree[u].mma,max(tree[u*2].mma,tree[u*2+1].mma));
}
long long ans ;
void build(int l,int r,int u)
{
    tree[u].left = l;
    tree[u].right = r;
    tree[u].mma = 0;
    if(l==r)
    {
        tree[u].sum = a[l];
        tree[u].val = a[l];
        tree[u].mma = a[l];
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,u*2);
    build(m+1,r,u*2+1);
    pushup(u);
}
void update(int x,int y,int z,int u)
{
    if(tree[u].mma<z) return ;
    if(tree[u].left==tree[u].right)
    {
        int t = tree[u].left ;
        a[t] %= z ;
        tree[u].mma = a[t];
        tree[u].val = a[t] ;
        tree[u].sum = a[t] ;
        return ;
    }
    if(tree[u].left==tree[u].right)  return ;
    int m = (tree[u].left + tree[u].right)>>1;
    if(x<=m)  update(x,min(m,y),z,u*2);
    if(y>m) update(max(x,m+1),y,z,u*2+1);
    pushup(u);
}
void update2(int pos,int c,int u)
{
    if(tree[u].left==tree[u].right)
    {
        if(tree[u].left==pos)
        {
            a[pos] = c ;
            tree[u].mma = a[pos] ;
            tree[u].val = a[pos] ;
            tree[u].sum = a[pos] ;
            return ;
        }
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(pos<=m)  update2(pos,c,u*2);
    else  update2(pos,c,u*2+1);
    pushup(u);
}
void query(int l,int r,int u)
{
    if(tree[u].left==l&&tree[u].right==r)
    {
        ans += tree[u].sum ;
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(r<=m)
        query(l,r,u*2);
    else if(l>m)
        query(l,r,u*2+1);
    else
    {
        query(l,m,u*2);
        query(m+1,r,u*2+1);
    }
}
int main()
{
    int n,m;
    int i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    scanf("%I64d",&a[i]);
    build(1,n,1);
    int k,x,y,z;
    int j;
    for(i=1;i<=m;i++)
    {
        scanf("%d",&k);
        if(k==1)
        {
            ans = 0;
            scanf("%d%d",&x,&y);
            query(x,y,1);
            printf("%I64d\n",ans);
        }
        else if(k==2)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(x,y,z,1);
        }
        else if(k==3)
        {
            scanf("%d%d",&x,&y);
            update2(x,y,1);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct Tree{
	long long left,right,sum,mma;
};
long long a[111211];
Tree tree[5111121];
long long max(long long a,long long b)
{
	return a>b?a:b;
}
long long min(long long a,long long b)
{
	return a<b?a:b;
}
void pushup(int u)
{
	tree[u].sum = tree[u*2].sum + tree[u*2+1].sum ;
	tree[u].mma = max(tree[u*2].mma,tree[u*2+1].mma);
}
long long ans ;
void build(int l,int r,int u)
{
	tree[u].left = l;
	tree[u].right = r;
	if(l==r)
	{
		tree[u].sum = a[l];
		tree[u].mma = a[l];
		return ;
	}
	int m = (l+r)>>1;
	build(l,m,u*2);
	build(m+1,r,u*2+1);
	pushup(u);
}
void update(int x,int y,int z,int u)
{
	if(tree[u].mma<z) return ;
	if(tree[u].left==tree[u].right)
	{
		int t = tree[u].left ;
		a[t] %= z ;
		tree[u].mma = a[t];
		tree[u].sum = a[t] ;
		return ;
	}
	int m = (tree[u].left + tree[u].right)>>1;
	if(y<=m)  update(x,y,z,u*2);
	else if(x>m) update(x,y,z,u*2+1);
	else
	{
		update(x,m,z,u*2);
		update(m+1,y,z,u*2+1);
	}
	pushup(u);
}
void update2(int pos,int c,int u)
{
	if(tree[u].left==pos&&tree[u].right==tree[u].left)
	{
		a[pos] = c ;
		tree[u].mma = a[pos] ;
		tree[u].sum = a[pos] ;
		return ;
	}
	if(tree[u].left==tree[u].right)  return ;
	int m = (tree[u].left + tree[u].right)>>1;
	if(pos<=m)  update2(pos,c,u*2);
	else  update2(pos,c,u*2+1);
	pushup(u);
}
void query(int l,int r,int u)
{
	if(tree[u].left==l&&tree[u].right==r)
	{
		ans += tree[u].sum ;
		return ;
	}
	int m = (tree[u].left + tree[u].right)>>1;
	if(r<=m)
		query(l,r,u*2);
	else if(l>m)
		query(l,r,u*2+1);
	else
	{
		query(l,m,u*2);
		query(m+1,r,u*2+1);
	}
}
int main()
{
	int n,m;
	int i;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	scanf("%I64d",&a[i]);
	build(1,n,1);
	int k,x,y,z;
	int j;
	for(i=1;i<=m;i++)
	{
		scanf("%d",&k);
		if(k==1)
		{
			ans = 0;
			scanf("%d%d",&x,&y);
			query(x,y,1);
			printf("%I64d\n",ans);
		}
		else if(k==2)
		{
			scanf("%d%d%d",&x,&y,&z);
			update(x,y,z,1);
		}
		else if(k==3)
		{
			scanf("%d%d",&x,&y);
			update2(x,y,1);
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
struct Tree{
    long long left,right,sum,val,mma;
};
long long a[1111111];
Tree tree[5111111];
long long max(long long a,long long b)
{
    return a>b?a:b;
}
long long min(long long a,long long b)
{
    return a<b?a:b;
}
void pushup(int u)
{
    tree[u].sum = tree[u*2].sum + tree[u*2+1].sum ;
    tree[u].mma = max(tree[u].mma,max(tree[u*2].mma,tree[u*2+1].mma));
}
long long ans ;
void build(int l,int r,int u)
{
    tree[u].left = l;
    tree[u].right = r;
    tree[u].mma = 0;
    if(l==r)
    {
        tree[u].sum = a[l];
        tree[u].val = a[l];
        tree[u].mma = a[l];
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,u*2);
    build(m+1,r,u*2+1);
    pushup(u);
}
void update(int x,int y,int z,int u)
{
    if(tree[u].mma<z) return ;
    if(tree[u].left==tree[u].right)
    {
        int t = tree[u].left ;
        a[t] %= z ;
        tree[u].mma = a[t];
        tree[u].val = a[t] ;
        tree[u].sum = a[t] ;
        return ;
    }
    if(tree[u].left==tree[u].right)  return ;
    int m = (tree[u].left + tree[u].right)>>1;
    if(x<=m)  update(x,min(m,y),z,u*2);
    if(y>m) update(max(x,m+1),y,z,u*2+1);
    pushup(u);
}
void update2(int pos,int c,int u)
{
    if(tree[u].left==tree[u].right)
    {
        if(tree[u].left==pos)
        {
            a[pos] = c ;
            tree[u].mma = a[pos] ;
            tree[u].val = a[pos] ;
            tree[u].sum = a[pos] ;
            return ;
        }
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(pos<=m)  update2(pos,c,u*2);
    else  update2(pos,c,u*2+1);
    pushup(u);
}
void query(int l,int r,int u)
{
    if(tree[u].left==l&&tree[u].right==r)
    {
        ans += tree[u].sum ;
        return ;
    }
    int m = (tree[u].left + tree[u].right)>>1;
    if(r<=m)
        query(l,r,u*2);
    else if(l>m)
        query(l,r,u*2+1);
    else
    {
        query(l,m,u*2);
        query(m+1,r,u*2+1);
    }
}
int main()
{
    int n,m;
    int i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    scanf("%I64d",&a[i]);
    build(1,n,1);
    int k,x,y,z;
    int j;
    for(i=1;i<=m;i++)
    {
        scanf("%d",&k);
        if(k==1)
        {
            ans = 0;
            scanf("%d%d",&x,&y);
            query(x,y,1);
            printf("%I64d\n",ans);
        }
        else if(k==2)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(x,y,z,1);
        }
        else if(k==3)
        {
            scanf("%d%d",&x,&y);
            update2(x,y,1);
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
	long long int val;
}all[2123456];

int element[112345];

inline int max(int a, int b)
{
	return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
		all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
	all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

	if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
	if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
	return all[now].val;
}

long long int search(int now, int beg, int end)
{
    if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

long long int push_mod(int now, int beg, int end, int mod)
{
	if (all[now].max < mod)
	{
		return 0;
	}
	if (all[now].end - all[now].beg == 1)
	{
		int old(element[beg]);
		element[beg] %= mod;
		all[now].val = element[beg];
		return old - element[beg];
	}
	int mid((all[now].beg + all[now].end) / 2);
	if (mid >= end)
	{
		long long int a = push_mod(now * 2, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	if (mid <= beg)
	{
		long long int a = push_mod(now * 2 + 1, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	long long int sub = push_mod(now * 2, beg, mid, mod);
	sub += push_mod(now * 2 + 1, mid, end, mod);
	all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
	all[now].val -= sub;
	return sub;
}

int change(int now, int pos, int val)
{
	if (all[now].end - all[now].beg == 1)
	{
		int old(element[pos]);
		element[pos] = val;
		all[now].val = val;
		all[now].max = val;
		return old - val;
	}
	if (val > all[now].max) all[now].max = val;
	int mid((all[now].beg + all[now].end) / 2);
	if (mid > pos)
	{
		int a = change(now * 2, pos, val);
		all[now].val -= a;
		return a;
	}
	else
	{
		int a = change(now * 2 + 1, pos, val);
		all[now].val -= a;
		return a;
	}
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%lld\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
			push_mod(1, left - 1, right, x);
        }
		if (cmd == 3)
		{
			int pos, val;
			scanf("%d%d", &pos, &val);
			change(1, pos - 1, val);
		}
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
    long long int val;
}all[4123456];

int element[112345];

inline int max(int a, int b)
{
    return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
        all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
    all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

    if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
    if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
    return all[now].val;
}

long long int search(int now, int beg, int end)
{
    if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

void push_mod(int now, int beg, int end, int mod)
{
    if (all[now].max < mod)
    {
        return;
    }
    if (all[now].end - all[now].beg == 1)
    {
        element[beg] %= mod;
		all[now].max = element[beg];
        all[now].val = element[beg];
		return;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        push_mod(now * 2, beg, end, mod);
        all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val = all[2 * now].val + all[2 * now + 1].val;
		return;
    }
    if (mid <= beg)
    {
        push_mod(now * 2 + 1, beg, end, mod);
        all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val = all[2 * now].val + all[2 * now + 1].val;
		return;
    }
    push_mod(now * 2, beg, mid, mod);
    push_mod(now * 2 + 1, mid, end, mod);
    all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
    all[now].val = all[2 * now].val + all[2 * now + 1].val;
}

int change(int now, int pos, int val)
{
    if (all[now].end - all[now].beg == 1)
    {
        int old(element[pos]);
        element[pos] = val;
        all[now].val = val;
        all[now].max = val;
        return old - val;
    }
    if (val > all[now].max) all[now].max = val;
    int mid((all[now].beg + all[now].end) / 2);
    if (mid > pos)
    {
        int a = change(now * 2, pos, val);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val -= a;
        return a;
    }
    else
    {
        int a = change(now * 2 + 1, pos, val);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val -= a;
        return a;
    }
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%lld\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
            push_mod(1, left - 1, right, x);
        }
        if (cmd == 3)
        {
            int pos, val;
            scanf("%d%d", &pos, &val);
            change(1, pos - 1, val);
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

class tree
{
public:
	int beg, end, max;
	long long int val;
}all[2123456];

int element[112345];

long long int build(int now, int beg, int end, int& max)
{
	int a, b;
	all[now].beg = beg;
	all[now].end = end;
	if (end - beg > 1)
	{
		int mid((beg + end) / 2);
		all[now].val = build(now * 2, beg, mid, a);
		all[now].val += build(now * 2 + 1, mid, end, b);
		if (a > b)
		{
			max = a;
		}
		else
		{
			max = b;
		}
		all[now].max = max;
	}
	else
	{
		all[now].val = element[beg];
		all[now].max = element[beg];
		max = element[beg];
	}
	return all[now].val;
}

long long int search(int now, int beg, int end)
{
	if (end - beg == 1) return element[beg];
	if (beg == all[now].beg && end == all[now].end) return all[now].val;
	int mid((all[now].beg + all[now].end) / 2);
	if (beg >= mid)
	{
		return search(now * 2 + 1, beg, end);
	}
	if (end <= mid)
	{
		return search(now * 2, beg, end);
	}
	return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

long long int push_mod(int now, int beg, int end, int x, int& max)
{

	if (end - beg == 1)
	{
		long long int old(element[beg]);
		element[beg] %= x;
		if (element[beg] > all[now].max)
		{
			all[now].max = element[beg];
		}
		all[now].val -= old - element[beg];
		max = element[beg];
		return old - element[beg];
	}
	if (all[now].max < x)
	{
		max = all[now].max;
		return 0;
	}
	int mid((beg + end) / 2);
	if (beg >= mid)
	{
		int a;
		long long int ret = push_mod(now * 2, beg, end, x, a);
		if (all[now * 2 + 1].max > a)
		{
			max = all[now * 2 + 1].max;
		}
		else
		{
			max = a;
		}
		all[now].val -= ret;
		return ret;
	}
	if (end <= mid)
	{
		int a;
		long long int ret(push_mod(now * 2 + 1, beg, end, x, a));
		if (all[now * 2].max > a)
		{
			max = all[now * 2].max;
		}
		else
		{
			max = a;
		}
		all[now].val -= ret;
		return ret;
	}
	int a, b;
	long long int ret(push_mod(now * 2, beg, mid, x, a));
	ret += push_mod(now * 2 + 1, mid, end, x, b);
	all[now].val -= ret;
	if (a < b) a = b;
	all[now].max = a;
	max = a;
	return ret;
}

int n, m;

int change(int now, int k, int v)
{
	if (all[now].end - all[now].beg == 1)
	{
		int old(element[k]);
		element[k] = v;
		all[now].max = v;
		return old - v;
	}
	int mid((all[now].beg + all[now].end)/2);
	if (mid > k)
	{
		int ret(change(now * 2, k, v));
		if (v > all[now].max) all[now].max = v;
		all[now].val -= ret;
		return ret;
	}
	else
	{
		int ret(change(now * 2 + 1, k, v));
		if (v > all[now].max) all[now].max = v;
		all[now].val -= ret;
		return ret;
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i(0); i != n; ++i)
	{
		scanf("%d", &element[i]);
	}
	int x;
	build(1, 0, n, x);
	for (int i(0); i != m; ++i)
	{
		int cmd;
		int l, r, x;
		scanf("%d", &cmd);
		switch (cmd)
		{
			case 1:
				scanf("%d%d", &l, &r);
				printf("%lld\n", search(1, l - 1, r));
				break;
			case 2:
				scanf("%d%d%d", &l, &r, &x);
				push_mod(1, l - 1, r, x, cmd);
				break;
			case 3:
				scanf("%d%d", &l, &r);
				change(1, l - 1, r);
				break;
		}
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
	bool mark;
	int pos, change;
	long long int val;
}all[2123456];

int element[112345];

inline int max(int a, int b)
{
	return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
		all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
	all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

	if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
	if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
	return all[now].val;
}

void push_change(int now, int pos, int val);

long long int search(int now, int beg, int end)
{
    if (all[now].mark)
	{
		all[now].mark = false;
		all[now].val -= element[all[now].pos] - all[now].change;
		int mid((all[now].beg + all[now].end) / 2);
		if (mid > all[now].pos)
		{
			push_change(now * 2, all[now].pos, all[now].change);
		}
		else
		{
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
	}
	if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

long long int push_mod(int now, int beg, int end, int mod)
{
	if (all[now].max < mod)
	{
		return 0;
	}
	if (all[now].end - all[now].beg == 1)
	{
		int old(element[beg]);
		element[beg] %= mod;
		all[now].val = element[beg];
		return old - element[beg];
	}
	int mid((all[now].beg + all[now].end) / 2);

	if (all[now].mark)
	{
		all[now].mark = false;
		if (all[now].pos < mid)
		{
			push_change(now * 2, all[now].pos, all[now].change);
		}
		else
		{
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
	}

	if (mid >= end)
	{
		long long int a = push_mod(now * 2, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	if (mid <= beg)
	{
		long long int a = push_mod(now * 2 + 1, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	long long int sub = push_mod(now * 2, beg, mid, mod);
	sub += push_mod(now * 2 + 1, mid, end, mod);
	all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
	all[now].val -= sub;
	return sub;
}

void push_change(int now, int pos, int val)
{
	if (all[now].end - all[now].beg == 1)
	{
		element[pos] = val;
		all[now].val = val;
		all[now].max = val;
		all[now].mark = false;
		return;
	}
	if (val > all[now].max) all[now].max = val;
	int mid((all[now].beg + all[now].end) / 2);
	if (mid > pos)
	{
		if (all[now].mark)
		{
			all[now].val -= element[pos] - val;
			push_change(now * 2, pos, val);
		}
		all[now].mark = true;
		all[now].pos = pos;
		all[now].change = val;
	}
	else
	{
		if (all[now].mark)
		{
			all[now].val -= element[pos] - val;
			push_change(now * 2 + 1, pos, val);
		}
		all[now].mark = true;
		all[now].pos = pos;
		all[now].change = val;
	}
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%lld\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
			push_mod(1, left - 1, right, x);
        }
		if (cmd == 3)
		{
			int pos, val;
			scanf("%d%d", &pos, &val);
			push_change(1, pos - 1, val);
		}
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
	bool mark;
	int pos, change;
	long long int val;
}all[2123456];

int element[112345];

inline int max(int a, int b)
{
	return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
		all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
	all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

	if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
	if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
	return all[now].val;
}

void push_change(int now, int pos, int val);

long long int search(int now, int beg, int end)
{
    if (all[now].mark)
	{
		all[now].mark = false;
		all[now].val -= element[all[now].pos] - all[now].change;
		int mid((all[now].beg + all[now].end) / 2);
		if (mid > all[now].pos)
		{
			push_change(now * 2, all[now].pos, all[now].change);
		}
		else
		{
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
	}
	if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

long long int push_mod(int now, int beg, int end, int mod)
{
	if (all[now].max < mod)
	{
		return 0;
	}
	if (all[now].end - all[now].beg == 1)
	{
		int old(element[beg]);
		element[beg] %= mod;
		all[now].val = element[beg];
		return old - element[beg];
	}
	int mid((all[now].beg + all[now].end) / 2);

	if (all[now].mark)
	{
		all[now].mark = false;
		all[now].val -= element[all[now].pos] - all[now].change;
		if (all[now].pos < mid)
		{
			push_change(now * 2, all[now].pos, all[now].change);
		}
		else
		{
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
	}

	if (mid >= end)
	{
		long long int a = push_mod(now * 2, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	if (mid <= beg)
	{
		long long int a = push_mod(now * 2 + 1, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	long long int sub = push_mod(now * 2, beg, mid, mod);
	sub += push_mod(now * 2 + 1, mid, end, mod);
	all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
	all[now].val -= sub;
	return sub;
}

void push_change(int now, int pos, int val)
{
	if (all[now].end - all[now].beg == 1)
	{
		element[pos] = val;
		all[now].val = val;
		all[now].max = val;
		all[now].mark = false;
		return;
	}
	if (val > all[now].max) all[now].max = val;
	int mid((all[now].beg + all[now].end) / 2);
	if (mid > pos)
	{
		if (all[now].mark)
		{
			all[now].val -= element[pos] - val;
			push_change(now * 2, pos, val);
		}
		all[now].mark = true;
		all[now].pos = pos;
		all[now].change = val;
	}
	else
	{
		if (all[now].mark)
		{
			all[now].val -= element[pos] - val;
			push_change(now * 2 + 1, pos, val);
		}
		all[now].mark = true;
		all[now].pos = pos;
		all[now].change = val;
	}
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%lld\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
			push_mod(1, left - 1, right, x);
        }
		if (cmd == 3)
		{
			int pos, val;
			scanf("%d%d", &pos, &val);
			push_change(1, pos - 1, val);
		}
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
    long long int val;
}all[4123456];

int element[112345];

inline int max(int a, int b)
{
    return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
        all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
    all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

    if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
    if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
    return all[now].val;
}

long long int search(int now, int beg, int end)
{
    if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

long long int push_mod(int now, int beg, int end, int mod)
{
    if (all[now].max < mod)
    {
        return 0;
    }
    if (all[now].end - all[now].beg == 1)
    {
        int old(element[beg]);
        element[beg] %= mod;
        all[now].val = element[beg];
        return old - element[beg];
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        long long int a = push_mod(now * 2, beg, end, mod);
        all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val -= a;
        return a;
    }
    if (mid <= beg)
    {
        long long int a = push_mod(now * 2 + 1, beg, end, mod);
        all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val -= a;
        return a;
    }
    long long int sub = push_mod(now * 2, beg, mid, mod);
    sub += push_mod(now * 2 + 1, mid, end, mod);
    all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
    all[now].val -= sub;
    return sub;
}

int change(int now, int pos, int val)
{
    if (all[now].end - all[now].beg == 1)
    {
        int old(element[pos]);
        element[pos] = val;
        all[now].val = val;
        all[now].max = val;
        return old - val;
    }
    if (val > all[now].max) all[now].max = val;
    int mid((all[now].beg + all[now].end) / 2);
    if (mid > pos)
    {
        int a = change(now * 2, pos, val);
        all[now].val -= a;
        return a;
    }
    else
    {
        int a = change(now * 2 + 1, pos, val);
        all[now].val -= a;
        return a;
    }
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%lld\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
            push_mod(1, left - 1, right, x);
        }
        if (cmd == 3)
        {
            int pos, val;
            scanf("%d%d", &pos, &val);
            change(1, pos - 1, val);
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
    long long int val;
}all[4123456];

int element[112345];

inline int max(int a, int b)
{
    return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
        all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
    all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

    if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
    if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
    return all[now].val;
}

long long int search(int now, int beg, int end)
{
    if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

void push_mod(int now, int beg, int end, int mod)
{
    if (all[now].max < mod)
    {
        return;
    }
    if (all[now].end - all[now].beg == 1)
    {
        element[beg] %= mod;
        all[now].val = element[beg];
		return;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        push_mod(now * 2, beg, end, mod);
        all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val = all[2 * now].val + all[2 * now + 1].val;
		return;
    }
    if (mid <= beg)
    {
        push_mod(now * 2 + 1, beg, end, mod);
        all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
        all[now].val = all[2 * now].val + all[2 * now + 1].val;
		return;
    }
    push_mod(now * 2, beg, mid, mod);
    push_mod(now * 2 + 1, mid, end, mod);
    all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
    all[now].val = all[2 * now].val + all[2 * now + 1].val;
}

int change(int now, int pos, int val)
{
    if (all[now].end - all[now].beg == 1)
    {
        int old(element[pos]);
        element[pos] = val;
        all[now].val = val;
        all[now].max = val;
        return old - val;
    }
    if (val > all[now].max) all[now].max = val;
    int mid((all[now].beg + all[now].end) / 2);
    if (mid > pos)
    {
        int a = change(now * 2, pos, val);
        all[now].val -= a;
        return a;
    }
    else
    {
        int a = change(now * 2 + 1, pos, val);
        all[now].val -= a;
        return a;
    }
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%lld\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
            push_mod(1, left - 1, right, x);
        }
        if (cmd == 3)
        {
            int pos, val;
            scanf("%d%d", &pos, &val);
            change(1, pos - 1, val);
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>

#include <iostream>

using namespace std;

class tree
{
public:
    int beg, end, max;
	bool mark;
	int pos, change;
	long long int val;
}all[2123456];

int element[112345];

inline int max(int a, int b)
{
	return a > b ? a : b;
}

long long int build(int now, int beg, int end)
{
    all[now].beg = beg;
    all[now].end = end;
    if (end - beg == 1)
    {
        all[now].val = element[beg];
		all[now].max = element[beg];
        return element[beg];
    }
    all[now].val = build(now * 2, beg, (beg + end) / 2);
	all[now].val += build(now * 2 + 1, (beg + end) / 2, end);

	if (all[now].max < all[now * 2].max) all[now].max = all[now * 2].max;
	if (all[now].max < all[now * 2 + 1].max) all[now].max = all[now * 2 + 1].max;
	return all[now].val;
}

void push_change(int now, int pos, int val);

long long int search(int now, int beg, int end)
{
    if (all[now].mark)
	{
		all[now].mark = false;
		all[now].val += all[now].change;
		int mid((all[now].beg + all[now].end) / 2);
		if (mid > all[now].pos)
		{
			push_change(now * 2, all[now].pos, all[now].change);
		}
		else
		{
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
	}
	if (all[now].beg == beg && all[now].end == end)
    {
        return all[now].val;
    }
    int mid((all[now].beg + all[now].end) / 2);
    if (mid >= end)
    {
        return search(now * 2, beg, end);
    }
    if (mid <= beg)
    {
        return search(now * 2 + 1, beg, end);
    }
    return search(now * 2, beg, mid) + search(now * 2 + 1, mid, end);
}

long long int push_mod(int now, int beg, int end, int mod)
{
	if (all[now].max < mod)
	{
		return 0;
	}
	if (all[now].end - all[now].beg == 1)
	{
		int old(element[beg]);
		element[beg] %= mod;
		all[now].val = element[beg];
		return old - element[beg];
	}
	int mid((all[now].beg + all[now].end) / 2);

	if (all[now].mark)
	{
		all[now].mark = false;
		all[now].val += all[now].change;
		if (all[now].pos < mid)
		{
			push_change(now * 2, all[now].pos, all[now].change);
		}
		else
		{
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
	}

	if (mid >= end)
	{
		long long int a = push_mod(now * 2, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	if (mid <= beg)
	{
		long long int a = push_mod(now * 2 + 1, beg, end, mod);
		all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
		all[now].val -= a;
		return a;
	}
	long long int sub = push_mod(now * 2, beg, mid, mod);
	sub += push_mod(now * 2 + 1, mid, end, mod);
	all[now].max = max(all[2 * now].max, all[2 * now + 1].max);
	all[now].val -= sub;
	return sub;
}

void push_change(int now, int pos, int val)
{
	if (all[now].end - all[now].beg == 1)
	{
		all[now].val = element[pos];
		all[now].max = element[pos];
		all[now].mark = false;
		return;
	}
	if (val > all[now].max) all[now].max = val;
	int mid((all[now].beg + all[now].end) / 2);
	if (mid > pos)
	{
		if (all[now].mark)
		{
			all[now].val += all[now].change;
			push_change(now * 2, all[now].pos, all[now].change);
		}
		all[now].mark = true;
		all[now].pos = pos;
		all[now].change = val;
	}
	else
	{
		if (all[now].mark)
		{
			all[now].val += all[now].change;
			push_change(now * 2 + 1, all[now].pos, all[now].change);
		}
		all[now].mark = true;
		all[now].pos = pos;
		all[now].change = val;
	}
}

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i(0); i != n; ++i)
    {
        scanf("%d", &element[i]);
    }
    build(1, 0, n);
    for (int i(0); i != m; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        if (cmd == 1)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            printf("%I64d\n", search(1, left - 1, right));
        }
        if (cmd == 2)
        {
            int left, right, x;
            scanf("%d%d%d", &left, &right, &x);
			push_mod(1, left - 1, right, x);
        }
		if (cmd == 3)
		{
			int pos, val;
			scanf("%d%d", &pos, &val);
			push_change(1, pos - 1, val - element[pos - 1]);
			element[pos - 1] = val;
		}
    }
    return 0;
}


#include<stdio.h>

int main()
{
	int n,a[1001],s=0,t=0,result=1,i;
	scanf("%d",&n);

	for(i=0;i<n-1;i++)
		scanf("%d ",&a[i]);
	scanf("%d",&a[n-1]);

	i=0;
	while(a[i]==0)
			i++;

	for(i;i<n;i++)

	{
		if(a[i]==1)
			s++;
		else
			t++;
	}
		if(t==0&&s==0)
			result=0;
		if(t==0)
			result=s;

		else
			result =t+s-1;
		printf("%d\n",result);
		return 0;
}


#include <stdio.h>

int main() {
	int n, a, a_, one, cnt;

	scanf("%d", &n);
	a_ = 0;
	cnt = 0;
	one = 0;
	while (n-- > 0) {
		scanf("%d", &a);
		if (a == 1 || a_ == 1) {
			one = 1;
			cnt++;
		}
		a_ = a;
	}
	printf("%d\n", a_ == 0 && one ? cnt - 1 : cnt);
	return 0;
}


#include<stdio.h>

int main()
{

    int n;
    scanf("%d",&n);
    int ans = 0;
    int val;
    val = 1;
    int i,a;
    int t = 0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a);

        if(a==1)
        {
            if(val==0)
                ans++;
            ans++;
            val = 1;
            t = 1;
        }
        else if (t==0)
        {

        }
        else
        {
            val = 0;
        }
    }

    printf("%d",ans);

    return 0;
}


#include<stdio.h>

int main()
{

    int n;
    scanf("%d",&n);
    int ans = 0;
    int val;
    val = 1;
    int i,a;
    int t = 0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a);

        if(a==1)
        {
            if(val==0)
                ans++;
            ans++;
            val = 1;
            t = 1;
        }
        else if (t==0)
        {

        }
        else
        {
            val = 0;
        }
    }

    printf("%d",ans);
}


#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
int cmp(const void *a,const void *b)
{
	return(*(int*)a-*(int*)b);

}

int main()
{
	int n,a[1005],a1[1005],a0[1005];
	int j=0,k=0,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]==1)
		{
			a1[j]=i;j++;
		}
//		else {a0[k]=i;k++;}
	}
	int count=1;
	for(i=0;i<j-1;i++)
	{
/*		if(a1[i]==a1[i+1])
		{
			count++;
		}
*/
		if(a1[i+1]-a1[i]==1)
		{
			count+=1;
		}
		else
//		if(a1[i+1]-a1[i]==2)
		{
			count+=2;
		}

	}
	if(j==0) count=0;
//	if(n==1 && a[0]==1) count=2;
	printf("%d\n",count);

	return 0;
}


#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
int cmp(const void *a,const void *b)
{
	return(*(int*)a-*(int*)b);

}

int main()
{
	int n,a[1005],a1[1005],a0[1005];
	int j=0,k=0,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]==1)
		{
			a1[j]=i;j++;
		}
//		else {a0[k]=i;k++;}
	}
	int count=1;
	for(i=0;i<j-1;i++)
	{
/*		if(a1[i]==a1[i+1])
		{
			count++;
		}
*/
		if(a1[i+1]-a1[i]==1)
		{
			count+=1;
		}
		else
//		if(a1[i+1]-a1[i]==2)
		{
			count+=2;
		}

	}
	if(j==0) count=0;
	if(n==1 && a[0]==1) count=2;
	printf("%d\n",count);

	return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main(){
	int n,i,*a,d,count=0;
	scanf("%d",&n);
	a=(int *)malloc(sizeof(int)*n);
	for(i=0;i<n;i++)
	  scanf("%d",(a+i));
	for(i=0;i<n;i++)
	{
		if(a[i]==0){

		  continue;
	     }
		while(a[i]==1)
		  {
		  	a[i]=0;
		  	count++;
		  	i++;

		  }
		  for(d=i;d<n;d++){
	       if(a[d]==1){

		  count++;
		  break;
	}
	}
	}
	printf("%d",count);
	return 0;
}


#include<stdio.h>

int main()
{
	int n;
	int count =0;
	scanf("%d",&n);
	int a[1001];
	int k;
	int i;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++){
		if(a[i]==1){
			count++;
		}
	}
	int p[count];
	int j=0;
	for(i=0;i<n;i++){
		if(a[i]==1){
			p[j]=i;
			j++;
		}
	}
	 k=count;
	for(i=0;i<k-1;i++){
		p[i]=p[i+1]-p[i];
		if(p[i]>1){
			count ++;
		//	printf("%d\n",count);
		}
	}

	printf("%d\n",count);
	return 0;
	}


#include<stdio.h>
int main()
{
int n,s[1010],ans=0,i,t,ref=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
    scanf("%d",&s[i]);
}
for(i=0;i<n;i++)
{
  if(s[i]==1){
   t=i;
   break;
   }
}
while(n-t+1>=0)
{
    if(s[t]==1){
     if(ref>0)
     {
         ans=ans+2;
         ref=0;
         t++;
         continue;
     }
     ans++;
    }
    else{
    ref++;}
 t++;
}
printf("%d",ans);
return 0;
}


#include<stdio.h>
int main()
{
int n,s[1010],ans=0,i,t,ref,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
    scanf("%d",&s[i]);
    if(s[i]==1)
     count++;
}
for(i=0;i<n;i++)
{
  if(s[i]==1){
   t=i;
   break;
   }
}
while(n-t>0&&count>0)
{
    if(s[t]==1){
     if(ref>=2)
     {
         ans=ans+2;
         ref=0;
         continue;
     }
     ref=0;
     ans++;
    }
    else
    ref++;
 t++;
 count--;
}
printf("%d",ans);
return 0;
}


#include <stdio.h>

int main(){

	int lng;
	int i=1,state=0,masuk=0,cnt=0,out=0,ada=0;
	scanf("%d",&lng);
	getchar();

	char s[1000];
	gets(s);

	for (i=1;i<lng*2;){
		if (s[i-1]=='0') {i+=2;continue;}
		if (s[i-1]=='1' && masuk!=0 && (i-masuk)>1) out+=2;
		while (s[i-1]=='1') {ada=1;masuk=i;state=1;cnt++;i+=2;}
		if (cnt>0 && ada) out+=cnt-1;
		cnt=0;
		if (!ada) i+=2;
		ada=0;
}

	if (state==1) printf("%d\n",out+1);
	else printf("%d\n",out);
return 0;}


#include <stdio.h>

int main(){

	int lng;
	int i=1,state=0,masuk=0,cnt=0,out=0,ada=0;
	scanf("%d",&lng);
	getchar();

	char s[1000];
	gets(s);

	for (i=1;i<lng*2;){
		if (s[i-1]=='1' && masuk!=0 && (i-masuk)>1) out+=2;
		while (s[i-1]=='1') {ada=1;masuk=i;state=1;cnt++;i+=2;}
		if (cnt>0 && ada) {out+=cnt-1;}
		cnt=0;
		if (!ada) i+=2;
		ada=0;
}

	if (state==1) printf("%d\n",out+1);
	else printf("%d\n",out);
return 0;}


#include <stdio.h>

int main(){

	int num,a[10001]={0},out=0,cek=0;
	scanf("%d",&num);

	int b;
	for (b=1;b<=num;b++){
		scanf("%d",&a[b]);
		if (a[b]==1) {
			cek=1;
			out++;
			if (a[b-1]==0) out++;
		}
	}

	if (num==0 || cek==0) printf("0\n");
	else printf("%d\n",out-1);

	return 0;
}


#include<stdio.h>
int main (){int n,letters[1000],operation=0,key=0,index,i,start=1;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&letters[i]);
if(letters[i]==1 && start==1)operation++,key=1,index=i;    if(letters[i]==1 && start==0){if(i-index==1)operation++,index=i; else operation=operation+2,index=i;}
if(key==1)start=0;}printf("%d\n",operation);    return 0;}


#include<stdio.h>

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    int n,i,len;
    int a[1005];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    int cnt=0;
    i=0;
    while(i<n)
    {

        if(a[i]==1)
        break;
        i++;
    }
    if(i==n)
    printf("%d\n",cnt);
    else
    {
        i++;
        cnt=1;
        while(i<n)
        {
            if(a[i]==1&&a[i-1]==1)
            {
                cnt++;
                i++;
            }
            else if(a[i]==1&&a[i-1]==0)
            {
                cnt+=2;
                i++;
            }
            else
            i++;

        }
        printf("%d\n",cnt);
    }
}


#include<stdio.h>

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    int n,i,len;
    int a[1005];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    int cnt=0;
    i=0;
    while(i<n)
    {

        if(a[i]==1)
        break;
        i++;
    }
    if(i==n)
    printf("%d\n",cnt);
    else
    {
        i++;
        cnt=1;
        while(i<n)
        {
            if(a[i]==1&&a[i-1]==1)
            {
                cnt++;
                i++;
            }
            else if(a[i]==1&&a[i-1]==0)
            {
                cnt+=2;
                i++;
            }
            else
            i++;

        }
        printf("%d\n",cnt);
    }
    return 0;
}


#include<stdio.h>
int main()
{
	int i,j=-5,k,l,n,count=0;
	scanf("%d",&n);
	int a,sum=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a);
		if(a==1)
		{
			if(k==1)
			{
				sum+=1;
			}
			else
			{
				sum+=2;
			}
		}
		k=a;
	}
		if(sum)
		{
			sum-=1;
		}
			printf("%d",sum);

	return 0;
}


#include <stdio.h>

int main(void) {
    int n ,arr[1003],i,k=0,one[1003],flag=0,prev,ans=0;
    scanf("%d",&n);
    for(i=0;i<n;++i)
    {
        scanf("%d",&arr[i]);
        if(arr[i]==1)
        one[k++]=i;
    }
    for(i=0;i<k;++i)
    {
        if(!flag)
        {
            flag++;
            ans++;
            continue;
        }
        if((prev+1)==one[i])
        {

            ans++;
        }
        else
        {

            ans+=2;
        }

        prev=one[i];
    }
    printf("%d\n",ans);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,temp;
    scanf("%d",&n);
    int a[n];
    int flag=0;
    int flag0=0;
    int count=0;
    for(i=0;i<n;i++)
    {

        scanf("%d",&a[i]);
        if(flag==0 && a[i]==1)
        {
            temp=i;
            flag=1;
            count=1;
        }
    }

    for(i=temp+1;i<n;i++)
    {
        if(a[i]==0)
        {
            flag0=1;
        }
        if(a[i]==1)
        {
            if(flag0==1)
            count=count+2;
            else count++;
            flag0=0;
        }

    }

    printf("%d",count);
}


#include<stdio.h>
#include<stdlib.h>
int main()
{

    int n,i,temp;
    scanf("%d",&n);
    int a[n];
    int flag=0;
    int flag0=0;
    int count=0;
    for(i=0;i<n;i++)
    {

        scanf("%d",&a[i]);
        if(flag==0 && a[i]==1)
        {
            temp=i;
            flag=1;
            count=1;
        }
    }

    for(i=temp+1;i<n;i++)
    {
        if(a[i]==0)
        {
            flag0=1;
        }
        if(a[i]==1)
        {
            if(flag0==1)
            count=count+2;
            else count++;
            flag0=0;
        }

    }

    printf("%d\n",count);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    int arr[n];
    int ans[n];
    int j=0;
    int count=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
        if(arr[i]==1 && j==0)
        {
            ans[j]=i;
            j=j+1;
        }
        else if(arr[i]==1 && j>0)
        {
            ans[j]=i-ans[j-1];
            j=j+1;

        }

    }
  /* for(i=0;i<j;i++)
    {
        printf("%d ",ans[i]);
    }*/
    for(i=0;i<j;i++)
    {
        if(i==0)
        {
            count=count+1;
        }
        else
        {
            if(ans[i]>2)
            {
                count=count+2;
            }
            else
                count=count+ans[i];
        }

    }
    printf("%d\n",count);
    return 0;

}


#include<stdio.h>
int main()
{
    int n,i,k;
    scanf("%d",&n);
    int arr[n];
    int ans[n];
    int j=0;
    int count=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
        if(arr[i]==1 && j==0)
        {
            ans[j]=i;
            j=j+1;
        }
        else if(arr[i]==1 && j>0)
        {
            ans[j]=i;
            for(k=0;k<j;k++)
            ans[j]=ans[j]-ans[k];
            j=j+1;

        }

    }
  /* for(i=0;i<j;i++)
    {
        printf("%d ",ans[i]);
    }*/
    for(i=0;i<j;i++)
    {
        if(i==0)
        {
            count=count+1;
        }
        else
        {
            if(ans[i]>2)
            {
                count=count+2;
            }
            else
                count=count+ans[i];
        }

    }
    printf("%d\n",count);
    return 0;

}


#include<stdio.h>
int main()
{
   int n;

   scanf("%d",&n);
   int a[n];
   scanf("%d",&a[0]);
   int count=0,i,flag=2;
   if(a[0]==1)
	count+=2;
   for(i=1;i<n;i++)
   {   scanf("%d",&a[i]);
        if(a[i]==1)
         {   if(a[i-1]==1)
		flag=1,count++;
             else
		flag=0,count+=2;

         }
   }
if(flag==0)
	count--;
printf("%d\n",count);
return 0;
}


#include<stdio.h>
int main()
{
   int n;

   scanf("%d",&n);
   int a[n];
//   scanf("%d",&a[0]);
   int count=0,i,flag=2;
   //if(a[0]==1)
//	count+=2;
   for(i=0;i<n;i++)
      scanf("%d",&a[i]);
   for(i=0;i<n;i++)
   {   //scanf("%d",&a[i]);
        if(a[i]==1)
         {   if(i+1<n&&a[i+1]==1)
		flag=1,count++;
             else
		flag=0,count+=2;

         }
   }
if(flag==0)
	count--;
printf("%d\n",count);
return 0;
}


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

int main() {
    int n, num[1010];
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &num[i]);
    int ans = 0;
    for (int i = 1; i < n; i++)
    {
        if (num[i] && num[i+1])
            ans++;
        else if (num[i] && !num[i+1])
            ans += 2;
    }
    if (!num[n] && ans)
        ans--;
    else if (num[n])
        ans++;
    printf("%d\n", ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int i, j, len,count=0;
    scanf("%d", &len);
    int c[len];
    for(i=0;i<len;i++) {
        scanf("%d",&c[i]);
        if(c[i] == 1) count++;
    }
    for(i=0;i<len;i++) {
        if(c[i] == 1){
        j = i;
        break;
        }
    }
    for(i=len-1;i>j+1;i--) {
        if(c[i] == 1 && c[i-1] == 0) {
            count++;
        }
    }
    printf("%d", count);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int i, j, len,count=0;
    scanf("%d", &len);
    int c[len];
    for(i=0;i<len;i++) {
        scanf("%d",&c[i]);
        if(c[i] == 1) count++;
    }
    for(i=0;i<len;i++) {
        if(c[i] == 1);
        j = i;
        break;
    }
    for(i=len-1;i>j+2;i--) {
        if(c[i] == 1 && c[i-1] == 0) {
            count++;
        }
    }
    printf("%d", count);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int i, j, len,count=0;
    scanf("%d", &len);
    int c[len];
    for(i=0;i<len;i++) {
        scanf("%d",&c[i]);
        if(c[i] == 1) count++;
    }
    for(i=0;i<len;i++) {
        if(c[i] == 1);
        j = i;
        break;
    }
    for(i=j+3;i<len;i++) {
        if(c[i] == 1 && c[i-1] == 0) {
            count++;
        }
    }
    printf("%d", count);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int i, j, len,count=0;
    scanf("%d", &len);
    int c[len];
    for(i=0;i<len;i++) {
        scanf("%d",&c[i]);
        if(c[i] == 1) count++;
    }
    for(i=0;i<len;i++) {
        if(c[i] == 1);
        j = i;
        break;
    }
    for(i=len-1;i>1;i--) {
        if(c[i] == 1 && c[i-1] == 0) {
            count++;
        }
    }
    printf("%d", count);
    return 0;
}


#include <stdio.h>

int main(void) {
    int num,i,dt,op = 0, sp = -1;
    scanf("%d",&num);

    for(i = 0 ; i < num ; i++) {
        scanf("%d",&dt);
        if(dt == 1 && (sp == -1 || (i-sp) < 2)) {
            op++;
            sp = i;
        }
        else if(dt == 1) {
            op = op+2;
            sp = i;
        }
    }

    printf("%d\n",op);
    return 0;
}


#include <stdio.h>

int main(void) {
    int num,i,dt,op = 0, sp = 0;
    scanf("%d",&num);

    for(i = 0 ; i < num ; i++) {
        scanf("%d",&dt);
        if(dt == 1 && (i-sp) < 2) {
            op++;
            sp = i;
        }
        else if(dt == 1) {
            op = op+2;
            sp = i;
        }
    }

    printf("%d\n",op);
    return 0;
}


#include<stdio.h>

int main(){

    int n,i,j,k,check,sum;

    int mail[99999]={0};

    scanf("%d",&n);

    for(i=0;i<n;i++){
        scanf("%d",&mail[i]);
    }
    check = 0;
    i = 0;
    sum = 0;
    while(i<n){
        if(mail[i]==1){
            sum = sum +1;
            //printf("%d sum %d\n",i,sum);
            check = 1;
            i = i+1;
        }
        else if((mail[i] == 0) && (check==1)){
            if((mail[i+1] == 0) && (i<(n-1))){
                    //sum = sum +1;
                    //printf("%d sum %d\n",i,sum);
                    j = i +1;
                    while((mail[j] != 1) && (j<n)){
                        j = j+1;
                    }
                    if(j!=n){
                            sum = sum +1;

                    }
                    i = j;
            }
            else if(mail[i+1]==1){
                 sum = sum +1;
                //printf("%d sum %d\n",i,sum);
                 i = i+1;
            }
            else{
                i= i+1;
            }
        }
        else{
                i = i+1;

        }
    }
    printf("%d\n",sum);

    return 0;
}


#include<stdio.h>

int main(){

    int n,i,j,k,check,sum;

    int mail[99999]={0};

    scanf("%d",&n);

    for(i=0;i<n;i++){
        scanf("%d",&mail[i]);
    }
    check = 0;
    i = 0;
    sum = 0;
    while(i<n){
        if(mail[i]==1){
            sum = sum +1;
            //printf("%d sum %d\n",i,sum);
            check = 1;
            i = i+1;
        }
        else if((mail[i] == 0) && (check==1)){
            if((mail[i+1] == 0) && (i<(n-1))){
                    sum = sum +1;
                    //printf("%d sum %d\n",i,sum);
                    j = i +1;
                    while((mail[j] != 1) && (j<n)){
                        j = j+1;
                    }
                    i = j;
            }
            else if(mail[i+1]==1){
                 sum = sum +1;
                //printf("%d sum %d\n",i,sum);
                 i = i+1;
            }
            else{
                i= i+1;
            }
        }
        else{
                i = i+1;

        }
    }
    printf("%d\n",sum);

    return 0;
}


#include <stdio.h>

int main(void) {
    // your code goes here
    int i,n,ans=0,pre=-1,cur=-1;
    int a[1005];
    scanf("%d",&n);
    for(i=0;i<n;++i)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n;++i)
    {
        if(a[i]==1)
        {
            if(cur==-1)
            {
                pre=i;
                cur=i;
                ++ans;
            }
            else
            {
                cur=i;
                if(cur-pre>1)
                {
                    ans+=2;
                }
                else
                {
                    ++ans;

                }
                    pre=cur;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>

int main()
{
    int N, i, Pre, Num, Ans;
    scanf("%d", &N);
    Pre = -1;
    Ans = 0;
    for(i=0; i<N; ++i)
    {
        scanf("%d ", &Num);
        if(Num == 1)
        {
            if(Pre == -1)
            {
                Ans = 1;
            }
            else
            {
                if(i-Pre >= 2)
                {
                    Ans += 2;
                }
                else
                {
                    ++Ans;
                }
            }
            Pre = i;
        }
    }
    printf("%d\n", Ans);
    return 0;
}


#include <stdio.h>

int main()
{
    int N, i, Pre, Num, Ans;
    scanf("%d", &N);
    Pre = -1;
    Ans = 0;
    for(i=0; i<N; ++i)
    {
        scanf("%d ", &Num);
        if(Num == 1)
        {
            if(i-Pre >= 2)
            {
                Ans += 2;
            }
            else
            {
                ++Ans;
            }
            Pre = i;
        }
    }
    printf("%d\n", Ans);
    return 0;
}


#include <stdio.h>

int main()
{
    int n,i,p,sum=0;
    scanf("%d",&n);
    int ara[n];
    for(i=0;i<n;i++){
        scanf("%d",&ara[i]);
    }
    for(i=0;i<n;){
        if(ara[i]==1){
            sum=sum+1;
            p=0;
            i++;
            while(ara[i]==1&&i<n){
                p++;
                i++;

            }
            if(i==n){
                sum=sum+p;
            }
            else{
                sum=sum+p+1;
            }
        }
        else{
            i=i+1;
        }

    }
    if(ara[n-1]==0&&sum!=0){
        sum=sum-1;
    }
    printf("%d",sum);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,i,j,k,count=0,flag=0;
    scanf("%d",&n);
    int array[n];
    for(i=0;i<n;i++)
        scanf("%d",&array[i]);
    for(i=0;i<n;i++)
    {
        if(array[i]==1)
        {
            count++;
            for(i++;i<n;i++)
            {
                if(array[i]==1)
                    count++;
                else
                {
                    count++;
                    break;
                }
            }
        }
    }
    if(array[n-1]||!count)
        printf("%d",count);
    else
        printf("%d",count-1);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,i,j,k,count=0,flag=0;
    scanf("%d",&n);
    int array[n];
    for(i=0;i<n;i++)
        scanf("%d",&array[i]);
    for(i=0;i<n;i++)
    {
        if(array[i]==1)
        {
            count++;
            for(i++;i<n;i++)
            {
                if(array[i]==1)
                    count++;
                else
                {
                    count++;
                    break;
                }
            }
        }
    }
    if(array[n-1])
        printf("%d",count);
    else
        printf("%d",count-1);
    return 0;
}


#include<stdio.h>

int main()

{
    int n,p,q;
    scanf("%d",&n);

    int a[n+1];

    int i,count=0;

    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);

    }
    for(i=0;i<n;i++)
    {
        if(a[i]==1){
            count++;
            break;
        }
    }
    p=i;
    for(i+1;i<n;i++)
    {
        if(a[i]==1)
        {
            q=i-p;
            if(q<=2) count=count+q;
            else count=count+2;

            p=i;
        }
    }

    printf("%d",count);

    return 0;
}


#include<stdio.h>

int main()
{
int n,i;
scanf("%d",&n);
int arr[1000],flag=0;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int count=0;
for(i=0;i<n;i++)
{
if(flag==0) {if(arr[i]==1){flag=1;count++;}}
else
{
if(arr[i]==1)
{
if(arr[i-1]==1) count++;
else count+=2;
}
}
}
printf("%d",count);
return 0;
}


#include<stdio.h>
int main()
{
	int n,arr[1001],i=0;
	scanf("%d",&n);
	int count=0,previ=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		if(arr[i]==1)
		{
			if(i-previ>=2)
			{
				count+=2;
			}
			else{count++;}
			previ=i;
		}
	}
	printf("%d\n",count);
	return 0;
}


#include<stdio.h>
int main()
{
	int n,arr[1001],i=0;
	scanf("%d",&n);
	int count=0,previ=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		if(arr[i]==1)
		{
			if(i-previ>=2 && arr[previ]!=0)
			{
				count+=2;
			}
			else{count++;}
			previ=i;
		}
	}
	printf("%d\n",count);
	return 0;
}


#include<stdio.h>
int main()
{
	int n,arr[1001],i;
	scanf("%d",&n);
	int count=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		if(arr[i]==1)count++;
	}
	printf("%d",count==0?0:count+1);
	return 0;
}


#include<stdio.h>
int main()
{
	int n,arr[1001],i=0;
	scanf("%d",&n);
	int count=0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
		if(arr[i]==1)
		{
			count++;
			if(arr[i-1]==0 && count!=1)count++;
		}
	}
	printf("%d\n",count);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int cmpfunc(const void *a,const void *b){
	return (*(int*)a)-(*(int*)b);
}

int main(void){
    int n,i,j,ans=0;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    	scanf("%d",&a[i]);
    for(i=0;i<n;i++){
    	if(a[i]==1){
    		for(j=i+1;j<n;j++){
    			if(a[j]==0) break;
    		}
    		ans+=j-i;
    		i=j;
    	}
    }

    for(j=0;j<n;j++){
    	if(a[j]==1) break;
    }

    for(i=j+1;i<n-1;i++){
    	if(a[i]==0 && a[i+1]==1) ans++;
    }

    if(n==1){
    	if(a[0]==1) ans=1;
    	else ans=0;
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>

int main()
{
	int arr[1000], i, j, s = 0, n, t;
	scanf("%d", &n);
	for(i = 0; i < n; i++)
		scanf("%d", &arr[i]);
	for(i = 0; i < n; i++)
		if(arr[i] == 1)
		{
			t = i;
			s = 1;
			break;
		}

	for(i = t; i < n; i++)
	{
		if(arr[i + 1] == 1)
		{
			s++;
			continue;
		}
		else
		{
			for(j = i + 1; j < n; j++)
				if(arr[j] == 1)
				{
					i = j;
					s += 2;
					i--;
					continue;
				}
		}
	}

	printf("%d", s);

	return 0;
}


#include<stdio.h>

int main()
{
	int arr[1000], i, j, s = 0, n, t;
	scanf("%d", &n);
	for(i = 0; i < n; i++)
		scanf("%d", &arr[i]);
	for(i = 0; i < n; i++)
		if(arr[i] == 1)
		{
			t = i;
			s = 1;
			break;
		}

	for(i = t; i < n; i++)
	{
		if(arr[i + 1] == 1)
		{
			s++;
			continue;
		}
		else
		{
			for(j = i + 1; j < n; j++)
				if(arr[j] == 1)
				{
					i = j;
					//printf("%d\n", i);
					s += 2;
					i--;
					break;
				}
		}
	}

	printf("%d", s);

	return 0;
}


#include <stdio.h>

int main()
{
    int n, i;
    int ara[1100];
    scanf("%d", &n);
    int ones = 0, ubuntu = 0;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &ara[i]);
        if(ara[i] == 1) ones++;
        else if(i >= 1 && ara[i-1] == 1 && ara[i] == 0)
        {
            ubuntu += ones+1;
            ones = 0;
        }
    }
    ubuntu += ones;
    (ara[n-1] == 1 || ubuntu == 0)?printf("%d\n", ubuntu):printf("%d\n", ubuntu-1);
    return 0;
}


#include <stdio.h>

int main()
{
    int n, i;
    int ara[110];
    scanf("%d", &n);
    int ones = 0, ubuntu = 0;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &ara[i]);
        if(ara[i] == 1) ones++;
        else if(i >= 1 && ara[i-1] == 1 && ara[i] == 0)
        {
            ubuntu += ones+1;
            ones = 0;
        }
    }
    ubuntu += ones;
    (ara[n-1] == 1 || ubuntu == 0)?printf("%d\n", ubuntu):printf("%d\n", ubuntu-1);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,c=0,i,j,k=0;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        if(a[i]==1)
        {
            c++;
            if(k==0)
                k++;
            else
                c++;
            for(j=i+1;j<n;j++)
            {
                if(a[j]==1)
                    c++;
                else
                {
                    i=j;
                    break;
                }
            }
        }
    }
    printf("%d\n",c);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,c=0,i,j,k=0;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        if(a[i]==1)
        {
            c++;
            if(k==0)
                k++;
            else
                c++;
            for(j=i+1;j<n;j++)
            {
                if(a[j]==1)
                    c++;
                else
                {
                    i=j;
                    break;
                }
            }
            i=j;
        }
    }
    printf("%d\n",c);
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	int c;
	int num=0,p=1;
	int i;

	for ( i = 1; i <= n; i++)
	{
		scanf("%d ", &c);
		if (c == 1)
		{
			num++;
			p = 0;
		}
		else if (p==0)
		{
			num++;
			p = 1;
		}
		if (i == n&&c == 0)
			num--;
	}

	if (num < 0)
		num = 0;

	printf("%d\n", num);
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int c;
    int num=0,p=1;
    int i;

    for ( i = 1; i <= n; i++)
    {
        scanf("%d", &c);
        if (c == 1)
        {
            num++;
            p = 0;
        }
        else if (p==0)
        {
            num++;
            p = 1;
        }
        if (i == n&&c == 0)
            num--;
    }

    if (num < 0)
        num = 0;
    printf("%d\n", num);
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	int c;
	int num=0,p=1;
	int i;
	for ( i = 1; i <= n; i++)
	{
		scanf("%d", &c);
		if (c == 1)
		{
			if (p == 0)
				num++;
			else
			{
				num++;
				p = 0;
			}
		}
		else
		{
			if (p == 0)
			{
				num++;
				p = 1;
			}
		}
		if (i == n&&c == 0)
			num--;

	}
	if (num < 0)
		num = 0;
	printf("%d\n", num);
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	int c;
	int num=0,p=1;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &c);
		if (c == 1)
		{
			if (p == 0)
				num++;
			else
			{
				num++;
				p = 0;
			}
		}
		else
		{
			if (p == 0)
			{
				num++;
				p = 1;
			}
		}
		if (i == n&&c == 0)
			num--;

	}
	if (num < 0)
		num = 0;
	printf("%d\n", num);
}


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	int c;
	int num=0,p=1;
	int i;

	for ( i = 1; i <= n; i++)
	{
		scanf("%d", &c);
		if (c == 1)
		{
			num++;
			p = 0;
		}
		else if (p==0)
		{
			num++;
			p = 1;
		}
		if (i == n&&c == 0)
			num--;
	}

	if (num < 0)
		num = 0;

	printf("%d\n", num);
}


#include<iostream>
using namespace std;
int a[1005];
int main()
{
    int i,j,n,m,p;
    cin>>n;
    for(i=1;i<=n;i++) cin>>a[i];
    j=0;
    m=0;
    for(i=1;i<=n;i++){
        if(a[i]==1){
            p=i;
            while(i<=n&&a[i]==1){
                i++;
            }
            i--;
            m+=1+(i-p);
            if(j!=0) m++;
            j++;
        }
    }
    cout<<m<<endl;
}


#include <stdio.h>
#include <stdlib.h>

int main() {
    int nr_letters;
    int c_status;
    int i, nr_ops;
    int *letter_status;

    scanf("%d", &nr_letters);
    letter_status = (int*) malloc(nr_letters * sizeof(int));
    for(i = 0; i < nr_letters; i++) {
        scanf("%d", &letter_status[i]);
    }
    nr_ops = 0;
    c_status = 0;
    while(nr_letters >= 0 && letter_status[nr_letters - 1] == 0) {
        nr_letters--;
    }
    printf("nr: %d\n", nr_letters);
    for(i = 0; i < nr_letters; i++) {
        while(i < nr_letters && letter_status[i] == 0) i++;
        printf("close -> open: %d\n", i);
        while(i < nr_letters && letter_status[i] == 1) {
            printf("here\n");
            nr_ops++;
            i++;
        }
        if(i < nr_letters) {
            nr_ops++;
        }
    }
    printf("%d\n", nr_ops);
}


#include <stdio.h>
#include <stdlib.h>

int main() {
    int nr_letters;
    int c_status;
    int i, nr_ops;
    int *letter_status;

    scanf("%d", &nr_letters);
    letter_status = (int*) malloc(nr_letters * sizeof(int));
    for(i = 0; i < nr_letters; i++) {
        scanf("%d", &letter_status[i]);
    }
    nr_ops = 0;
    c_status = 0;
    while(nr_letters >= 0 && letter_status[nr_letters - 1] == 0) {
        nr_letters--;
    }
    for(i = 0; i < nr_letters; i++) {
        while(i < nr_letters && letter_status[i] == 0) i++;
        while(i < nr_letters && letter_status[i] == 1) {
            nr_ops++;
            i++;
        }
        if(i < nr_letters) {
            nr_ops++;
        }
    }
    printf("%d\n", nr_ops);
}


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n;
    scanf("%d",&n);
    int arr[n];
    int i,flag = 0,cnt=0,l=-1;
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
        if(arr[i]==1)
            l=i;
    }
    for(i=0;i<=l;i++){
        if(arr[i]==1){
            flag = 1;
            cnt++;
        }
        else if(flag==1){
            flag = 0;
            cnt++;
        }
    }
    printf("%d",cnt);
    return 0;
}


#include <stdio.h>
int main(){
    int n;
    scanf("%d" , &n);
    int a[n];
    int i;
    for(i=0;i<n;i++){
        scanf(" %d" , &a[i]);
    }
    int cevap=0, durum=0 , x=-1;
    i=0;
    while(i<n){
        if(i==n-1){
            if((a[i]==1) && (durum==0)){
                cevap++;
                x=0;
            }
            else if(!x) x=1;
        }
        else if((a[i] == 0) && (durum == 0)) {
            if(!x) x=1;
        }
        else if((a[i] == 1) && (durum==0)){
            durum=1;
            cevap++;
            x=0;
            if(!a[i+1]){
                durum = 0;
                cevap++;
            }
            else{
                cevap++;
            }
        }
        else if((a[i] == 0) && (durum==1)){
            if(a[i+1] ==0) {
                durum=0;
                cevap++;
            }
            else{
                cevap++;
            }
        }
        else{
            if(a[i+1] == 0){
                durum=0;
                cevap++;
                x=0;
            }
            else cevap++;
        }
        i++;
    }
    if(x==-1) x=0;
    printf("%d", cevap-x);
    return 0;
}


n=(list(map(int, input().split())))
a=(list(map(int, input().split())))
a.sort()
l=0
for i in range(0,n[0]):
    if n[1]>=a[i]:
        l+=1
        n[1]-=a[i]
print(l)


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
s=0
i=0
while s+ls[i]<=t:
	s=s+ls[i]
	i=i+1
	if i==n:
		break
if mn<i:
	mn=i
j=0
count=i
while i<n:
	if s+ls[i]>t:
		if mn<count:
			mn=count
		s=s-ls[j]
		j=j+1

	else:
		s=s+ls[i]
		i=i+1
		count=i-j
print(mn)


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
i=0
j=len(ls)-1
flag=1
s=sum(ls[i:j+1])
def f(lst,a,b,sm):
	if sm<=t:
		return 0
	u=1+f(lst,a+1,b,sm-lst[a])
	v=1+f(lst,a,b-1,sm-lst[b])
	k=min(u,v)
	return k
print(n-f(ls,i,j,s))


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
i=0
j=len(ls)-1
flag=1
while i<=j:
	s=sum(ls[i:j+1])
	if s>t:
		if ls[i]>ls[j]:
			i=i+1
		else:
			j=j-1
		if i==j and ls[i]>t:
			flag=0
	else:
		break
if flag==1:
	print(j-i+1)
else:
	print('0')


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
i=0
j=len(ls)-1
flag=1
s=sum(ls[i:j+1])
while i<=j:
	if s>t:
		if ls[i]>ls[j]:
			s=s-ls[i]
			i=i+1
		else:
			s=s-ls[j]
			j=j-1
		if i==j and ls[i]>t:
			flag=0
	else:
		break
if flag==1:
	print(j-i+1)
else:
	print('0')


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
for i in range(n):
	s=0
	j=i
	k=0
	while s<=t and j<n:
		s=s+ls[j]
		j=j+1
		k=k+1
	if k>mn:
		mn=k
print(mn)


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
s=0
i=j=0
while s+ls[i]<=t:
	s=s+ls[i]
	i=i+1
	if i==n:
		break
while i<n:
	if s+ls[i]>t:
		s=s-ls[j]
		if (i-j+1)>mn:
			mn=i-j+1
		j=j+1
	else:
		s=s+ls[i]
		i=i+1
	if i==n:
		if i-j>mn:
			mn=i-j
print(mn)


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
for i in range(n):
	s=0
	j=i
	k=0
	while s<=t and j<n:
		s=s+ls[j]
		if s>t:
			break
		j=j+1
		k=k+1
	if k>mn:
		mn=k
print(mn)


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
s=0
i=j=0
while s+ls[i]<=t:
	s=s+ls[i]
	i=i+1
	if i==n:
		break
if i>mn:
	mn=i
while i<n:
	if s+ls[i]>t:
		s=s-ls[j]
		if (i-j)>mn:
			mn=i-j
		j=j+1
	else:
		s=s+ls[i]
		i=i+1
	if i==n:
		if i-j>mn:
			mn=i-j
print(mn)


n,t=map(int,input().split())
ls=list(map(int,input().split()))
mn=-1
i=0
j=len(ls)-1
flag=1
s=sum(ls[i:j+1])
def f(lst,a,b,sm):
	if sm<=t:
		return 0
	if a==b:
		return 1
	u=1+f(lst,a+1,b,sm-lst[a])
	v=1+f(lst,a,b-1,sm-lst[b])
	k=min(u,v)
	return k
print(n-f(ls,i,j,s))