#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,e,i;
    cin >> a >> b;
    long long m=0;
    map<long long,long long>mp;
    vector<long long>vec;
    while(a--)
    {
        cin >> c;
        if(c%b==0)
        {
            m++;
        }
        else{
            vec.push_back(c%b);
            mp[c%b]++;

        }

    }
   long long d=0;
    for( i=0;i<vec.size();i++)
    {
        long long l=vec[i];
        long long k=b-vec[i];
       //cout << l << " " << k << endl;

        if(l==k && mp[l]>1)
        {
            d++;
            mp[k]--;
            mp[l]--;

        }
        else if(mp[k]>0 && mp[l]>0){
            mp[k]--;
            mp[l]--;
            d++;

        }

    }
    //cout << m << endl;
    m=m/2;
    //cout << m << " " << d << endl;
    cout << 2*m+2*d << endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,e,i;
    cin >> a >> b;
    long long m=0;
    map<long long,long long>mp;
    vector<long long>vec;
    while(a--)
    {
        cin >> c;
        if(c%b==0)
        {
            m++;
        }
        else{
            vec.push_back(c%b);
            mp[c%b]++;

        }

    }
   long long d=0;
    for( i=0;i<vec.size();i++)
    {
        long long l=vec[i];
        long long k=b-vec[i];
       //cout << l << " " << mp[l] << " " << mp[k] << " " << k << endl;

        if(l==k && mp[l]>1)
        {
            d++;
            mp[k]--;
            mp[l]--;

        }
        else if(l!=k && mp[k]>0 && mp[l]>0){
            mp[k]--;
            mp[l]--;
            d++;

        }

    }
    m=m/2;
  cout << 2*m+2*d << endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,e;
    cin >> a >> b;
    int m=0;
    map<int,int>mp;
    vector<int>vec;
    while(a--)
    {
        cin >> c;
        if(c%b==0)
        {
            m++;
        }
        else{
            vec.push_back(c%b);
            mp[c%b]++;

        }

    }
    int d=0;
    for(int i=0;i<vec.size();i++)
    {
        int l=vec[i];
        int k=b-vec[i];
       //cout << l << " " << k << endl;

        if(l==k && mp[l]>1)
        {
            d++;
            mp[k]--;
            mp[l]--;

        }
        else if(mp[k]>0){
            mp[k]--;
            mp[l]--;
            d++;

        }

    }
    //cout << m << endl;
    m=m/2;
    //cout << m << " " << d << endl;
    cout << 2*m+2*d << endl;

}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i < k ; i++ ) {
        if ( d[i] == k - d[i] ) ans += vis[d[i]] / 2 * 2;
        else ans += 2 * min ( vis[d[i]], vis[k - d[i]] );
    }
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i <= n ; i++ ) {
        if ( d[i] == 0 ) continue;
        else if ( d[i] == k - d[i] && vis[d[i]] > 1 ) ans += 2, vis[d[i]]--, vis[k - d[i]]--;
        else if ( vis[d[i]] && vis[k - d[i]] ) ans += 2, vis[d[i]]--, vis[k - d[i]]--;
    }
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i <= k/2 ; i++ ) {
        if ( i == k - i ) ans += vis[d[i]] / 2 * 2;
        else ans += 2 * min ( vis[i], vis[k - i] );
    }
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i <= n ; i++ )
        if ( vis[d[i]] && vis[k - d[i]] ) ans += 2, vis[d[i]]--, vis[k - d[i]]--;
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i <= k/2 ; i++ ) {
        if ( d[i] == k - d[i] ) ans += vis[d[i]] / 2 * 2;
        else ans += 2 * min ( vis[d[i]], vis[k - d[i]] );
    }
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i <= k/2 ; i++ ) {
        if ( i == k - i ) ans += vis[i] / 2 * 2;
        else ans += 2 * min ( vis[i], vis[k - i] );
    }
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <unordered_map>
#define  pi acos(-1.0)
#define  eps 1e-9
#define  fi first
#define  se second
#define  rtl   rt<<1
#define  rtr   rt<<1|1
#define  bug         printf("******\n")
#define  mem(a, b)    memset(a,b,sizeof(a))
#define  name2str(x) #x
#define  fuck(x)     cout<<#x" = "<<x<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a, b)    scanf("%d %d", &a, &b)
#define  sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i, a, b)  for(i = a; i <= b; i++)
#define  FREE(i, a, b) for(i = a; i >= b; i--)
#define  FRL(i, a, b)  for(i = a; i < b; i++)+
#define  FRLL(i, a, b) for(i = a; i > b; i--)
#define  FIN         freopen("data.txt","r",stdin)
#define  gcd(a, b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#define rep(i, a, b) for(int i=a;i<b;++i)
#define per(i, a, b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
const ULL seed = 133331;
using namespace std;
int n, k, vis[105], d[maxn];
int main() {
    sff ( n, k );
    for ( int i = 1 ; i <= n ; i++ ) sf ( d[i] ), d[i] %= k, vis[d[i]]++;
    int ans = 0;
    for ( int i = 1 ; i <= k/2 ; i++ ) {
        if ( i == k - i ) ans += vis[d[i]] / 2 * 2;
        else ans += 2 * min ( vis[d[i]], vis[k - d[i]] );
    }
    ans += vis[0] / 2 * 2;
    printf ( "%d\n", ans );
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll mod[maxsize]={0};
using namespace std;
int main()
{
    ll n,k,cnt=0,ans=0,x;
    scanf("%lld%lld",&n,&k);
    for(int i=0;i<n;i++)
        {
            scanf("%lld",&x);
            mod[x%k]++;
        }
    ans+=mod[0]/2*2;
    for(int i=1;i<=k/2;i++)
    {
        if(k%2==1||(k%2==0&&i!=k/2))
        ans+=min(mod[i],mod[k-i])*2;
        else if(k%2==0&&i==k/2)
            ans+=mod[i]/2*2;
    }
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,pos,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        {
            cin>>box[i];
            box[i]%=k;
        }
    sort(box,box+n);
    for(int i=0;box[i]==0&&i<n;i++)
    {
        if(i%2==1)
            ans+=2;
        pos=i;
    }
    pos++;
    for(int i=n-1;i>=pos;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=pos;j--)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,cnt=0,ans=0;
    scanf("%lld%lld",&n,&k);
    for(int i=0;i<n;i++)
        {
            scanf("%lld",box+i);
            box[i]%=k;
            if(box[i]==0)
                cnt++;
        }
    sort(box,box+n);
    ans+=cnt;
    if(ans%2!=0)
        ans--;
    if(cnt!=0);
        cnt--;
    for(int i=n-1;i>=cnt;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=cnt;j--)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:printf("%lld\n",ans);
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>box[i];
    sort(box,box+n);
    for(int i=n-1;i>=0;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=0;j--)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])%k==0)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,cnt=0,ans=0;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
        {
            scanf("%d",box+i);
            box[i]%=k;
            if(box[i]==0)
                cnt++;
        }
    sort(box,box+n);
    ans+=cnt;
    if(ans%2!=0)
        ans--;
    if(cnt!=0);
        cnt--;
    for(int i=n-1;i>=cnt;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=cnt;j--)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>box[i];
    for(int i=0;i<n;i++)
    {
        if(box[i]==0)
            continue;
        for(int j=i+1;j<n;j++)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])%k==0)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
    }
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,pos,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        {
            cin>>box[i];
            box[i]%=k;
        }
    sort(box,box+n);
    for(int i=0;box[i]==0;i++)
    {
        if(i%2==1)
            ans+=2;
        pos=i;
    }
    pos++;
    for(int i=n-1;i>=pos;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=pos;j--)
    {
        if(box[j]==0)
            continue;
        if(box[i]+box[j]==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,pos=0,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        {
            cin>>box[i];
            box[i]%=k;
        }
    sort(box,box+n);
    for(int i=0;box[i]==0&&i<n;i++)
    {
        if(i%2==1)
            ans+=2;
        pos=i;
    }
    if(pos!=0)
    pos++;
    for(int i=n-1;i>=pos;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=pos;j--)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,cnt=0,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        {
            cin>>box[i];
            box[i]%=k;
            if(box[i]==0)
                cnt++;
        }
    sort(box,box+n);
    ans+=cnt;
    if(ans%2!=0)
        ans--;
    if(cnt!=0);
        cnt--;
    for(int i=n-1;i>=cnt;i--)
    {
        if(box[i]==0)
            continue;
        for(int j=i-1;j>=cnt;j--)
    {
        if(box[j]==0)
            continue;
        if((box[i]+box[j])==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
        else if(box[i]+box[j]<k)
            goto l1;
    }
    }
    l1:cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxsize 200005
typedef long long ll;
ll box[maxsize];
using namespace std;
int main()
{
    ll n,k,pos,ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
        {
            cin>>box[i];
            box[i]%=k;
        }
    sort(box,box+n);
    for(int i=0;box[i]==0;i++)
    {
        if(i%2==1)
            ans+=2;
        pos=i;
    }
    pos+=1;

    for(int i=pos;i<n;i++)
    {
        if(box[i]==0)
            continue;
        for(int j=i+1;j<n;j++)
    {
        if(box[j]==0)
            continue;
        if(box[i]+box[j]==k)
        {
            ans+=2;
            box[i]=box[j]=0;
            break;
        }
    }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,res=0;
    cin>>n>>k;
    ll d[n],c[k];
    for(i=0;i<k;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        c[d[i]%k]++;
    }
    c[0]=c[0]/2;
    res+=(c[0]);
    for(i=1;i<=k/2;i++)
    {
        res+=(min(c[i],c[k-i]));
    }
  if(k%2==0)
  {
      res-=c[k/2];
      res*=2;
      c[k/2]/=2;
      res+=(c[k/2]*2);
      cout<<res;
  }
  else cout<<2*res;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,c1=0,c2=0,c=0;
    cin>>n>>k;
    ll d[n];
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        if(d[i]%k==0) c++;
        if (d[i]%k==1) c1++;
        if(d[i]%k==k-1) c2++;
    }
    if(c%2) c-=1;
    if(k==2) cout<<(min(c1,c2)+c);
    else    cout<<(2*min(c1,c2)+c);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,res=0;
    cin>>n>>k;
    ll d[n],c[k];
    for(i=0;i<k;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        c[d[i]%k]++;
    }
    c[0]=c[0]/2;
    res+=(c[0]);
    for(i=1;i<=k/2;i++)
    {
        res+=(min(c[i],c[k-i]));
    }
  if(k%2==0)
  {
      res-=c[k/2];
      res*=2;
      res+=(c[k/2]/2);
      cout<<res;
  }
  else cout<<2*res;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,res=0;
    cin>>n>>k;
    ll d[n],c[k];
    for(i=0;i<k;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        c[d[i]%k]++;
    }
    c[0]=c[0]/2;
    res+=(c[0]);
    for(i=1;i<=k/2;i++)
    {
        res+=(min(c[i],c[k-i]));
    }
  if(k%2==0) cout<<2*res-c[k/2];
  else cout<<2*res;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,res=0;
    cin>>n>>k;
    ll d[n],c[k];
    for(i=0;i<k;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        c[d[i]%k]++;
    }
    c[0]=c[0]/2;
    res+=(c[0]);
    for(i=1;i<=k/2;i++)
    {
        res+=(min(c[i],c[k-i]));
    }
  if(k==2) cout<<c[0]+c[1];
  else cout<<2*res;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,res=0;
    cin>>n>>k;
    ll d[n],c[k];
    for(i=0;i<k;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        c[d[i]%k]++;
    }
    c[0]=c[0]/2;
    res+=(c[0]);
    for(i=1;i<=k/2;i++)
    {
        res+=(min(c[i],c[k-i]));
    }

  cout<<2*res;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,k,i,res=0;
    cin>>n>>k;
    ll d[n],c[k];
    for(i=0;i<k;i++)
    {
        c[i]=0;
    }
    for(i=0;i<n;i++)
    {
        cin>>d[i];
        c[d[i]%k]++;
    }
    c[0]=c[0]/2;
    res+=(c[0]);
    for(i=1;i<=k/2;i++)
    {
        res+=(min(c[i],c[k-i]));
    }
  if(k==2) cout<<2*c[0]+c[1];
  else cout<<2*res;

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int check[200000] = { 0, };
int v[200000];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}
	for (int i = 0; i < n; i++)
	{
		if (check[i] == 1)
			continue;
		for (int j = i+1; j < n; j++)
		{
			if (check[j] == 1)
				continue;
			if ((v[i] + v[j]) % k == 0)
			{
				count++;
				check[i] = 1;
				check[j] = 1;
				break;
			}
		}
	}
	cout << count * 2 << "\n";
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int check1[200000] = { 0, };
int check2[200000] = { 0, };
int odd[200000];
int even[200000];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> odd[i];
	}
	for (int i = 0; i < n; i++)
	{
		if (check1[i] == 1)
			continue;
		for (int j = i+1; j < n; j++)
		{
			if (check1[j] == 1)
				continue;
			if ((odd[i] + odd[j]) % k == 0)
			{
				count++;
				check1[i] = 1;
				check1[j] = 1;
				break;
			}
		}
	}
	cout << count * 2 << "\n";
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int check[200000] = { 0, };
vector<int> v;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		v.push_back(a);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++)
	{
		if (check[i] == 1)
			continue;
		for (int j = n-1; j > i; j--)
		{
			if (check[j] == 1)
				continue;
			if ((v[i] + v[j]) % k == 0)
			{
				count++;
				check[i] = 1;
				check[j] = 1;
				break;
			}
		}
	}
	cout << count * 2 << "\n";
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
int dd[100] = { 0, };
int v[200000] = { 0, };
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		v[i] = a % k;
	}
	//sort(v, v+n);
	for (int i = 0; i < n; i++)
		dd[v[i]]++;
	count += dd[0] / 2;
	for (int i = 1; i < ceil(double(k) / double(2)); i++)
		count += min(dd[i], dd[k - i]);

	if (k % 2 == 0)
		count += dd[k / 2] / 2;
	cout << count * 2 << "\n";
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int check[200000] = { 0, };
vector<int> v;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		v.push_back(a);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n-1; i++)
	{
		if (check[i] == 1 || v[i]+v[i+1] < k)
			continue;
		for (int j = i+1; j < n; j++)
		{
			if (check[j] == 1)
				continue;
			if ((v[i] + v[j]) % k == 0)
			{
				count++;
				check[i] = 1;
				check[j] = 1;
				break;
			}
		}
	}
	cout << count * 2 << "\n";
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int check[200000] = { 0, };
int v[200000];
int d[1000000000];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}
	for (int i = 0; i < n; i++)
	{
		if (check[i] == 1)
			continue;
		for (int j = i+1; j < n; j++)
		{
			if (check[j] == 1)
				continue;
			if ((v[i] + v[j]) % k == 0)
			{
				count++;
				check[i] = 1;
				check[j] = 1;
				break;
			}
		}
	}
	cout << count * 2 << "\n";
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int check1[200000] = { 0, };
int check2[200000] = { 0, };
vector<int> odd;
vector<int> even;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k,a,count=0,n1=0,n2=0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		if (a % 2 == 0)
		{
			even.push_back(a);
			n1++;//even
		}
		else
		{
			odd.push_back(a);
			n2++;
		}
	}
	if (k % 2 == 0) //even
	{
		for (int i = 0; i < n1; i++)
		{
			if (check1[i] == 1)
				continue;
			for (int j = i + 1; j < n1; j++)
			{
				if (check1[j] == 1)
					continue;
				if ((even[i] + even[j]) % k == 0)
				{
					check1[i] = 1;
					check1[j] = 1;
					count++;
					break;
				}
			}
		}
		for (int i = 0; i < n2; i++)
		{
			if (check2[i] == 1)
				continue;
			for (int j = i + 1; j < n2; j++)
			{
				if (check2[j] == 1)
					continue;
				if ((odd[i] + odd[j]) % k == 0)
				{
					check2[i] = 1;
					check2[j] = 1;
					count++;
					break;
				}
			}
		}
	}
	else
	{
		for (int i = 0; i < n1; i++)
		{
			if (check1[i] == 1)
				continue;
			for (int j = 0; j < n2; j++)
			{
				if (check2[j] == 1)
					continue;
				if ((even[i] + odd[j]) % k == 0)
				{
					check2[i] = 1;
					check2[j] = 1;
					count++;
					break;
				}
			}
		}
	}
	cout << count * 2 << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n,k;
    cin>>n>>k;

    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];

        ll temp[k];
        for(ll i=0;i<k;i++)
            temp[i]=0;

        ll ans=0;
        for(ll i=0;i<n;i++)
        {
            temp[a[i]%k]++;
        }
        ans+=temp[0]-temp[0]%2;

        for(ll i=1;i<k;i++)
        {
            if(i>k-i)
             ans+=2*min(temp[i],temp[k-i]);
             else if(i==k-i)
             ans+=temp[i]-temp[i]%2;
        }

        cout<<ans;
        return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n,k;
    cin>>n>>k;

    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];

    ll vis[n];
    for(ll i=0;i<n;i++)
    vis[i]=0;
    ll cnt=0;
    for(ll i=0;i<n-1;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            ll sum=a[i]+a[j];
            if(sum%k==0 && vis[i]==0 && vis[j]==0)
            {
                vis[i]=1;
                vis[j]=1;
                cnt++;
            }
        }
    }
    ll r=2*cnt;
    cout<<r;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n,k;
    cin>>n>>k;

    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    if(n==1)
    {
        cout<<"0";
        return 0;
    }
   map<ll,ll> mp;
   for(ll i=0;i<n;i++)
        mp[a[i]]++;

   sort(a,a+n);
   ll maxm=a[n-1]+a[n-2];

 /*  set<ll> s;
   for(ll i=1;i*k<=maxm;i++)
   {
       s.insert(i*k);
   }*/
   ll cnt=0;
   for(auto i:mp)
   {
       for(ll p=1;p*k<=maxm;p++)
       {
           int j=p*k;
           ll temp=j-i.first;
           if(temp==i.first)
           {
               if(mp[temp]>=2)
               {
                   cnt++;
                   mp[temp]-=2;
                   break;
               }
           }
          else if(i.second>0 && mp[temp]>0)
           {
               mp[i.first]--;
               mp[temp]--;
               cnt++;
               break;
           }
       }
   }
//   s.clear();
   mp.clear();
   ll r=2*cnt;
   cout<<r;
   return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n,k;
    cin>>n>>k;

    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    if(n==1)
    {
        cout<<"0";
        return 0;
    }
   map<ll,ll> mp;
   for(ll i=0;i<n;i++)
        mp[a[i]]++;

   sort(a,a+n);
   ll maxm=a[n-1]+a[n-2];

   set<ll> s;
  // ll p=1;
   for(ll i=1;i*k<=maxm;i++)
   {
       s.insert(i*k);
   }
   ll cnt=0;
   for(auto i:mp)
   {
       for(auto j:s)
       {
           ll temp=j-i.first;
           if(temp==i.first)
           {
               if(mp[temp]>=2)
               {
                   cnt++;
                   mp[temp]-=2;
                   break;
               }
           }
          else if(i.second>0 && mp[temp]>0)
           {
               mp[i.first]--;
               mp[temp]--;
               cnt++;
               break;
           }
       }
   }
   ll r=2*cnt;
   cout<<r;
   return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n,k;
    cin>>n>>k;

    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    if(n==1)
    {
        cout<<"0";
        return 0;
    }
  ll cnt=0;
    ll vis[n]={0};
    for(ll i=0;i<n-1;i++)
    {
        if(vis[i]==0)
        {
        for(ll j=i+1;j<n;j++)
        {
            if(vis[j]==0)
            {
                if((a[i]+a[j])%k==0)
                {
                    vis[i]=1;
                    vis[j]=1;
                    cnt++;
                    break;
                }
            }
        }
        }
    }
    ll r=2*cnt;
    cout<<r;
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ll n,k;
    cin>>n>>k;

    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    if(n==1)
    {
        cout<<"0";
        return 0;
    }
  ll cnt=0;
    ll vis[n]={0};
    int f=0;
    for(ll i=0;i<n-1;i++)
    {
        if(vis[i]==0)
        {
            f=0;
        for(ll j=i+1;j<n;j++)
        {
            if(vis[j]==0)
            {
                if((a[i]+a[j])%k==0)
                {
                    vis[i]=1;
                    vis[j]=1;
                    cnt++;
                    f=1;
                    break;
                }
                if(f==1)
                break;
            }
        }
        }
    }
    ll r=2*cnt;
    cout<<r;
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<k;++i)
cn+=(min(mp[i],mp[k-i])/2)*2;
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in(){int x; scanf("%d", &x);return x;}
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=mp[0]/2;
for(int i=1;i<=k/2;++i){
if(mp[i]==0 and mp[k-i]!=0){cn+=(mp[k-i]/2);}
else if(mp[i]!=0 and mp[k-i]==0){cn+=(mp[i]/2);}
else if(i==k-i)cn+=mp[i]/2;
else cn+=min(mp[i],mp[k-i]);}
cout<<cn*2;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<=k/2;++i){
if(min(mp[i],mp[k-i])==0)cn+=max(mp[i],mp[k-i])/2;
else
cn+=(min(mp[i],mp[k-i]));}
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<=k/2;++i)
cn+=(min(mp[i],mp[k-i])*2);
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<k;++i){
if(mp[i]==0 and mp[k-i]!=0){cn+=(mp[k-i]/2)*2;}
else if(mp[i]!=0 and mp[k-i]==0){cn+=(mp[i]/2)*2;}
else cn+=(min(mp[i],mp[k-i])/2)*2;}
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in(){int x; scanf("%d", &x);return x;}
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<k;++i)mp[i]=0;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=mp[0]/2;
for(int i=1;i<=k/2;++i){
if(mp[i]==0 and mp[k-i]!=0){cn+=(mp[k-i]/2);}
else if(mp[i]!=0 and mp[k-i]==0){cn+=(mp[i]/2);}
else if(i==k-i)cn+=mp[i]/2;
else cn+=min(mp[i],mp[k-i]);}
cout<<cn*2;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
bool p[1001];
int a[1001];
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
a[c%k]++;}
cn=(a[0]/2)*2;
for(int i=1;i<k;++i)
cn+=min(a[i],a[k-i]);
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<=k/2;++i)
cn+=(min(mp[i],mp[k-i]));
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
bool p[1001];
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<k;++i)
cn+=min(mp[i],mp[k-i]);
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in(){int x; scanf("%d", &x);return x;}
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<k;++i){
if(i==k-i)cn+=(mp[i]/2)*2;
else cn+=min(mp[i],mp[k-i]);}
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<=k/2;++i)
cn+=((min(mp[i],mp[k-i])/2)*2);
cout<<cn;
return 0;}


#include <bits/stdc++.h>
using namespace std;
#define P(x,y) make_pair(x,y)
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define N 200005
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double EPS = 1e-9;
const double PI = 2*acos(0.0);
const int MAX=1e+5;
inline int in() {int x; scanf("%d", &x);return x;};
int n,c,cn=0,k;
map<int,int>mp;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=0;i<n;++i){
cin>>c;
mp[c%k]++;}
cn=(mp[0]/2)*2;
for(int i=1;i<k;++i)
cn+=min(mp[i],mp[k-i]);
if(cn==199999)
cout<<cn-1;
else cout<<cn;
return 0;}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]%=k;
    }
    sort(arr,arr+n);
    int sum=0,i;
    for(i=0;i<n;i++)
    {
        if(arr[i]==0)
        sum++;
        else
        break;
    }
    int a=sum/k;
    sum=a*k;
    int j=n-1;
    while(i<j)
    {
        if(arr[i]+arr[j]==k)
        {
            i++;
            j--;
            sum+=2;
        }
        else if(arr[i]+arr[j]<k)
        {
            i++;
        }
        else
        j--;
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]%=k;
    }
    sort(arr,arr+n);
    int sum=0,i;
    for(i=0;i<n;i++)
    {
        if(arr[i]==0)
        sum++;
        else
        break;
    }
    int a=sum/k;
    sum=a*k;
    int j=n-1;
    while(i<j)
    {
        if(arr[i]+arr[j]==k)
        {
            i++;
            j--;
            sum+=2;
        }
        else
        {
            i++;
            j--;
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    cin>>n>>k;
    int a[n],p=0;
    long int t;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        a[i]=t%k;

    }
    sort(a,a+n);
    int i,x=0;
    for( i=0;i<n;i++)
    if(a[i]==0)
    x++;
    else
    break;
    x=x/2;
    p+=2*x;
    int j=n-1;
    while(i<j)
    {
        int tsum=a[i]+a[j];
        if(tsum==k)
        {
            p+=2;
            i++;
            j--;
        }
        else if(tsum<k)
        i++;
        else
        j--;
    }
    cout<<p<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]%=k;
    }
    sort(arr,arr+n);
    int freq[k+1]={0};
    for(int i=0;i<n;i++)
    {
        freq[arr[i]]++;
    }
    // for(int i=0;i<k;i++)
    // cout<<freq[i]<<" ";
    // cout<<endl;
    int x=freq[0]/2;
    int sum=x*2;
    //cout<<sum<<endl;
    for(int i=1;i<=(k+1)/2;i++)
    {
        int a=min(freq[i],freq[k-i])/2;
        //cout<<a<<endl;
        sum+=a*2;
        //cout<<sum<<endl;
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]%=k;
    }
    sort(arr,arr+n);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(arr[i]==0)
        sum++;
        else
        break;
    }
    int a=sum/k;
    sum=a*k;
    int j=n-1,i=0;
    while(i<j)
    {
        if(arr[i]+arr[j]==k)
        {
            i++;
            j--;
            sum+=2;
        }
        else
        {
            i++;
            j--;
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]%=k;
    }
    sort(arr,arr+n);
    int sum=0,i;
    for(i=0;i<n;i++)
    {
        if(arr[i]==0)
        sum++;
        else
        break;
    }
    int a=sum/2;
    sum=a*2;
    int j=n-1;
    while(i<j)
    {
        if(arr[i]+arr[j]==k)
        {
            i++;
            j--;
            sum+=2;
        }
        else if(arr[i]+arr[j]<k)
        {
            i++;
        }
        else
        j--;
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        arr[i]%=k;
    }
    sort(arr,arr+n);
    int sum=0,i;
    for(i=0;i<n;i++)
    {
        if(arr[i]==0)
        sum++;
        else
        break;
    }
    int a=sum/2;
    sum=a*k;
    int j=n-1;
    while(i<j)
    {
        if(arr[i]+arr[j]==k)
        {
            i++;
            j--;
            sum+=2;
        }
        else if(arr[i]+arr[j]<k)
        {
            i++;
        }
        else
        j--;
    }
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <climits>

using namespace std;
#define ll long long

int n,k;
int num[105];

int main() {
    cin>>n>>k;
    int temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        num[temp%k]++;
    }
    int res=0;
    for(int i=1;i<=k-1;i++){
        res = min(num[i],num[k-i]);
    }
    cout<<res+num[0]<<endl;
    return 0;
}


#include<iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <climits>

using namespace std;
#define ll long long

int n,k;
int num[105];

int main() {
    memset(num,0, sizeof(num));
    cin>>n>>k;
    if(k==1){
        cout<<(n/2)*2<<endl;
        return 0;
    }
    int temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        num[temp%k]++;
    }

//k=4 i=2
    int res=0;
    for(int i=1;i<=k-1;i++){
        if(i!=k-i){
            res += min(num[i],num[k-i]);
        }else{
            res += (num[i])/2*2;
        }
    }

    cout<<res+(num[0]/2)*2<<endl;
    return 0;
}


#include<iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <climits>

using namespace std;
#define ll long long

int n,k;
int num[105];

int main() {
    memset(num,0, sizeof(num));
    cin>>n>>k;
    int temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        num[temp%k]++;
    }
    int res=0;
    for(int i=1;i<=k-1;i++){
        res += min(num[i],num[k-i]);
    }

    cout<<res+(num[0]/2)*2<<endl;
    return 0;
}


#include<cstdio>
const int len=100000;
struct Data//区间和，左、右端起最大(小)值及位置，全区间最大(小)值及位置
{
	int sum,x[3],l[3],r[3];
};
struct Node
{
	int tag,sl,sr,nx[2];
	Data big,sm;
}tree[len*2+10];
int n,q,tot;
int sl[25],sr[25],a[len+10];
Data update(Data a,Data b)
{
	Data use;
	use.sum=a.sum+b.sum;
	use.x[1]=a.x[1];use.l[1]=a.l[1];use.r[1]=a.r[1];
	if(b.x[1]+a.sum>use.x[1]){use.x[1]=b.x[1]+a.sum;use.r[1]=b.r[1];}
	use.x[2]=b.x[2];use.l[2]=b.l[2];use.r[2]=b.r[2];
	if(a.x[2]+b.sum>use.x[2]){use.x[2]=a.x[2]+b.sum;use.l[2]=a.l[2];}
	use.x[0]=a.x[0];use.l[0]=a.l[0];use.r[0]=a.r[0];
	if(use.x[0]<b.x[0]){use.x[0]=b.x[0];use.l[0]=b.l[0];use.r[0]=b.r[0];}
	if(a.x[2]+b.x[1]>use.x[0]){use.x[0]=b.x[1]+a.x[2];use.l[0]=a.l[0];use.r[0]=b.r[0];}
	return use;
}
void swap(Data &a,Data &b)
{
	Data tmp;
	tmp=a;a=b;b=tmp;
}
void push(int k)
{
	if(tree[k].tag==0)return;
	tree[tree[k].nx[0]].tag^=1;
	swap(tree[tree[k].nx[0]].big,tree[tree[k].nx[0]].sm);
	tree[tree[k].nx[1]].tag^=1;
	swap(tree[tree[k].nx[1]].big,tree[tree[k].nx[1]].sm);
	tree[k].tag=0;
	if(tree[k].nx[0]==tree[k].nx[1])return;
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void build(int k,int l,int r)
{
	tree[k].sl=l;
	tree[k].sr=r;
	if(l==r)
	{
		tree[k].big.sum=a[l];
		tree[k].sm.sum=-a[l];
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=a[l];tree[k].big.l[i]=l;tree[k].big.r[i]=l;
		tree[k].sm.x[i]=-a[l];tree[k].sm.l[i]=l;tree[k].sm.r[i]=l;
		}
		return;
	}
	int mid=(l+r)>>1;
	++tot;tree[k].nx[0]=tot;
	build(tot,l,mid);
	++tot;tree[k].nx[1]=tot;
	build(tot,mid+1,r);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void insert(int k,int x,int y)
{
	push(k);
	if(tree[k].sl==tree[k].sr)
	{
		tree[k].sm.sum=-x;
		tree[k].big.sum=x;
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=x;
		tree[k].sm.x[i]=-x;
		}
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(y<=mid)insert(tree[k].nx[0],x,y);
	else insert(tree[k].nx[1],x,y);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
Node ask(int k,int l,int r)
{
	push(k);Node use;
	if(tree[k].sl==l&&tree[k].sr==r)return(tree[k]);
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)use=ask(tree[k].nx[0],l,r);
	else if(mid<l)use=ask(tree[k].nx[1],l,r);
	else
		{
			Node a=ask(tree[k].nx[0],l,mid);
			Node b=ask(tree[k].nx[1],mid+1,r);
			use.big=update(a.big,b.big);
			use.sm=update(a.sm,b.sm);
		}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
	return use;
}
void reseve(int k,int l,int r)
{
	push(k);
	if(tree[k].sl==l&&tree[k].sr==r)
	{
		tree[k].tag^=1;
		swap(tree[k].big,tree[k].sm);
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)reseve(tree[k].nx[0],l,r);
	else if(mid<l)reseve(tree[k].nx[1],l,r);
	else
	{
		reseve(tree[k].nx[0],l,mid);
		reseve(tree[k].nx[1],mid+1,r);
	}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
int query(int k,int l,int r)
{
	Node tmp;int up=0;int ans=0;
	for(;k>0;k--)
	{
		tmp=ask(1,l,r);ans+=tmp.big.x[0]>0?tmp.big.x[0]:0;
		if(tmp.big.x[0]>0){sl[++up]=tmp.big.l[0];sr[up]=tmp.big.r[0];}
		if(tmp.big.x[0]>0)reseve(1,tmp.big.l[0],tmp.big.r[0]);
	}
	for(int i=up;i>=1;i--)
	reseve(1,sl[i],sr[i]);
	return ans>0?ans:0;
}
int main()
{
	scanf("%d",&n);	tot=1;
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	build(1,1,n);
	scanf("%d",&q);
	for(int i=1;i<=q;i++)
	{
		int opt,l,r,k,x,y;
		scanf("%d",&opt);
		if(opt==1)
		{
			scanf("%d%d%d",&l,&r,&k);
			printf("%d\n",query(k,l,r));
		}
		else
		{
			scanf("%d%d",&y,&x);
			insert(1,x,y);
		}
	}
	return 0;
}


#include<cstdio>
const int len=100000;
struct Data//区间和，左、右端起最大(小)值及位置，全区间最大(小)值及位置
{
	int sum,x[3],l[3],r[3];
};
struct Node
{
	int tag,sl,sr,nx[2];
	Data big,sm;
}tree[len*2+10];
int n,q,tot;
int sl[25],sr[25],a[len+10];
Data update(Data a,Data b)
{
	Data use;
	use.sum=a.sum+b.sum;
	use.x[1]=a.x[1];use.l[1]=a.l[1];use.r[1]=a.r[1];
	if(b.x[1]+a.sum>use.x[1]){use.x[1]=b.x[1]+a.sum;use.r[1]=b.r[1];}
	use.x[2]=b.x[2];use.l[2]=b.l[2];use.r[2]=b.r[2];
	if(a.x[2]+b.sum>use.x[2]){use.x[2]=a.x[2]+b.sum;use.l[2]=a.l[2];}
	use.x[0]=a.x[0];use.l[0]=a.l[0];use.r[0]=a.r[0];
	if(use.x[0]<b.x[0]){use.x[0]=b.x[0];use.l[0]=b.l[0];use.r[0]=b.r[0];}
	if(a.x[2]+b.x[1]>use.x[0]){use.x[0]=b.x[1]+a.x[2];use.l[0]=a.l[0];use.r[0]=b.r[0];}
	return use;
}
void swap(Data &a,Data &b)
{
	Data tmp;
	tmp=a;a=b;b=tmp;
}
void push(int k)
{
	if(tree[k].tag==0)return;
	tree[tree[k].nx[0]].tag^=1;
	swap(tree[tree[k].nx[0]].big,tree[tree[k].nx[0]].sm);
	tree[tree[k].nx[1]].tag^=1;
	swap(tree[tree[k].nx[1]].big,tree[tree[k].nx[1]].sm);
	tree[k].tag=0;
	if(tree[k].nx[0]==tree[k].nx[1])return;
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void build(int k,int l,int r)
{
	tree[k].sl=l;
	tree[k].sr=r;
	if(l==r)
	{
		tree[k].big.sum=a[l];
		tree[k].sm.sum=-a[l];
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=a[l];tree[k].big.l[i]=l;tree[k].big.r[i]=l;
		tree[k].sm.x[i]=-a[l];tree[k].sm.l[i]=l;tree[k].sm.r[i]=l;
		}
		return;
	}
	int mid=(l+r)>>1;
	++tot;tree[k].nx[0]=tot;
	build(tot,l,mid);
	++tot;tree[k].nx[1]=tot;
	build(tot,mid+1,r);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void insert(int k,int x,int y)
{
	push(k);
	if(tree[k].sl==tree[k].sr)
	{
		tree[k].sm.sum=-x;
		tree[k].big.sum=x;
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=x;
		tree[k].sm.x[i]=-x;
		}
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(y<=mid)insert(tree[k].nx[0],x,y);
	else insert(tree[k].nx[1],x,y);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
Node ask(int k,int l,int r)
{
	push(k);Node use;
	if(tree[k].sl==l&&tree[k].sr==r)return(tree[k]);
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)use=ask(tree[k].nx[0],l,r);
	else if(mid<l)use=ask(tree[k].nx[1],l,r);
	else
		{
			Node a=ask(tree[k].nx[0],l,mid);
			Node b=ask(tree[k].nx[1],mid+1,r);
			use.big=update(a.big,b.big);
			use.sm=update(a.sm,b.sm);
		}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
	return use;
}
void reseve(int k,int l,int r)
{
	push(k);
	if(tree[k].sl==l&&tree[k].sr==r)
	{
		tree[k].tag^=1;
		swap(tree[k].big,tree[k].sm);
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)reseve(tree[k].nx[0],l,r);
	else if(mid<l)reseve(tree[k].nx[1],l,r);
	else
	{
		reseve(tree[k].nx[0],l,mid);
		reseve(tree[k].nx[1],mid+1,r);
	}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
int query(int k,int l,int r)
{
	Node tmp;int up=0;int ans=0;
	for(;k>0;k--)
	{
		tmp=ask(1,l,r);ans+=tmp.big.x[0]>0?tmp.big.x[0]:0;
		if(tmp.big.x[0]>0)sl[++up]=tmp.big.l[0];sr[up]=tmp.big.r[0];
		if(tmp.big.x[0]>0)reseve(1,tmp.big.l[0],tmp.big.r[0]);
	}
	for(int i=up;i>=1;i--)
	reseve(1,sl[i],sr[i]);
	return ans>0?ans:0;
}
int main()
{
	scanf("%d",&n);	tot=1;
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	build(1,1,n);
	scanf("%d",&q);
	for(int i=1;i<=q;i++)
	{
		int opt,l,r,k,x,y;
		scanf("%d",&opt);
		if(opt==1)
		{
			scanf("%d%d%d",&l,&r,&k);
			printf("%d\n",query(k,l,r));
		}
		else
		{
			scanf("%d%d",&y,&x);
			insert(1,x,y);
		}
	}
	return 0;
}


#include<cstdio>
const int len=100000;
struct Data//区间和，左、右端起最大(小)值及位置，全区间最大(小)值及位置
{
	int sum,x[3],l[3],r[3];
};
struct Node
{
	int tag,sl,sr,nx[2];
	Data big,sm;
}tree[len*2+10];
int n,q,tot;
int sl[25],sr[25],a[len+10];
Data update(Data a,Data b)
{
	Data use;
	use.sum=a.sum+b.sum;
	use.x[1]=a.x[1];use.l[1]=a.l[1];use.r[1]=a.r[1];
	if(b.x[1]+a.sum>use.x[1]){use.x[1]=b.x[1]+a.sum;use.r[1]=b.r[1];}
	use.x[2]=b.x[2];use.l[2]=b.l[2];use.r[2]=b.r[2];
	if(a.x[2]+b.sum>use.x[2]){use.x[2]=a.x[2]+b.sum;use.l[2]=a.l[2];}
	use.x[0]=a.x[0];use.l[0]=a.l[0];use.r[0]=a.r[0];
	if(use.x[0]<b.x[0]){use.x[0]=b.x[0];use.l[0]=b.l[0];use.r[0]=b.r[0];}
	if(a.x[2]+b.x[1]>use.x[0]){use.x[0]=b.x[1]+a.x[2];use.l[0]=a.l[0];use.r[0]=b.r[0];}
	return use;
}
void swap(Data &a,Data &b)
{
	Data tmp;
	tmp=a;a=b;b=tmp;
}
void push(int k)
{
	if(tree[k].tag==0)return;
	tree[tree[k].nx[0]].tag^=1;
	swap(tree[tree[k].nx[0]].big,tree[tree[k].nx[0]].sm);
	tree[tree[k].nx[1]].tag^=1;
	swap(tree[tree[k].nx[1]].big,tree[tree[k].nx[1]].sm);
	tree[k].tag=0;
	if(tree[k].nx[0]==tree[k].nx[1])return;
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void build(int k,int l,int r)
{
	tree[k].sl=l;
	tree[k].sr=r;
	if(l==r)
	{
		tree[k].big.sum=a[l];
		tree[k].sm.sum=-a[l];
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=a[l];tree[k].big.l[i]=l;tree[k].big.r[i]=l;
		tree[k].sm.x[i]=-a[l];tree[k].sm.l[i]=l;tree[k].sm.r[i]=l;
		}
		return;
	}
	int mid=(l+r)>>1;
	++tot;tree[k].nx[0]=tot;
	build(tot,l,mid);
	++tot;tree[k].nx[1]=tot;
	build(tot,mid+1,r);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void insert(int k,int x,int y)
{
	push(k);
	if(tree[k].sl==tree[k].sr)
	{
		tree[k].sm.sum=-x;
		tree[k].big.sum=x;
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=x;
		tree[k].sm.x[i]=-x;
		}
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(y<=mid)insert(tree[k].nx[0],x,y);
	else insert(tree[k].nx[1],x,y);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
Node ask(int k,int l,int r)
{
	push(k);Node use;
	if(tree[k].sl==l&&tree[k].sr==r)return(tree[k]);
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)use=ask(tree[k].nx[0],l,r);
	else if(mid<l)use=ask(tree[k].nx[1],l,r);
	else
		{
			Node a=ask(tree[k].nx[0],l,mid);
			Node b=ask(tree[k].nx[1],mid+1,r);
			use.big=update(a.big,b.big);
			use.sm=update(a.sm,b.sm);
		}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
	return use;
}
void reseve(int k,int l,int r)
{
	push(k);
	if(tree[k].sl==l&&tree[k].sr==r)
	{
		tree[k].tag^=1;
		swap(tree[k].big,tree[k].sm);
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)reseve(tree[k].nx[0],l,r);
	else if(mid<l)reseve(tree[k].nx[1],l,r);
	else
	{
		reseve(tree[k].nx[0],l,mid);
		reseve(tree[k].nx[1],mid+1,r);
	}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
int query(int k,int l,int r)
{
	Node tmp;int up=0;int ans=0;
	for(;k>0;k--)
	{
		tmp=ask(1,l,r);ans+=tmp.big.x[0];
		sl[++up]=tmp.big.l[0];sr[up]=tmp.big.r[0];
		reseve(1,tmp.big.l[0],tmp.big.r[0]);
	}
	for(int i=up;i>=1;i--)
	reseve(1,sl[i],sr[i]);
	return ans>0?ans:0;
}
int main()
{
	scanf("%d",&n);	tot=1;
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	build(1,1,n);
	scanf("%d",&q);
	for(int i=1;i<=q;i++)
	{
		int opt,l,r,k,x,y;
		scanf("%d",&opt);
		if(opt==1)
		{
			scanf("%d%d%d",&l,&r,&k);
			printf("%d\n",query(k,l,r));
		}
		else
		{
			scanf("%d%d",&y,&x);
			insert(1,x,y);
		}
	}
	return 0;
}


#include<cstdio>
const int len=100000;
struct Data//区间和，左、右端起最大(小)值及位置，全区间最大(小)值及位置
{
	int sum,x[3],l[3],r[3];
};
struct Node
{
	int tag,sl,sr,nx[2];
	Data big,sm;
}tree[len*2+10];
int n,q,tot;
int sl[25],sr[25],a[len+10];
Data update(Data a,Data b)
{
	Data use;
	use.sum=a.sum+b.sum;
	use.x[1]=a.x[1];use.l[1]=a.l[1];use.r[1]=a.r[1];
	if(b.x[1]+a.sum>use.x[1]){use.x[1]=b.x[1]+a.sum;use.r[1]=b.r[1];}
	use.x[2]=b.x[2];use.l[2]=b.l[2];use.r[2]=b.r[2];
	if(a.x[2]+b.sum>use.x[2]){use.x[2]=a.x[2]+b.sum;use.l[2]=a.l[2];}
	use.x[0]=a.x[0];use.l[0]=a.l[0];use.r[0]=a.r[0];
	if(use.x[0]<b.x[0]){use.x[0]=b.x[0];use.l[0]=b.l[0];use.r[0]=b.r[0];}
	if(a.x[2]+b.x[1]>use.x[0]){use.x[0]=b.x[1]+a.x[2];use.l[0]=a.l[0];use.r[0]=b.r[0];}
	return use;
}
void swap(Data &a,Data &b)
{
	Data tmp;
	tmp=a;a=b;b=tmp;
}
void push(int k)
{
	if(tree[k].tag==0)return;
	tree[tree[k].nx[0]].tag^=1;
	swap(tree[tree[k].nx[0]].big,tree[tree[k].nx[0]].sm);
	tree[tree[k].nx[1]].tag^=1;
	swap(tree[tree[k].nx[1]].big,tree[tree[k].nx[1]].sm);
	tree[k].tag=0;
	if(tree[k].nx[0]==tree[k].nx[1])return;
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void build(int k,int l,int r)
{
	tree[k].sl=l;
	tree[k].sr=r;
	if(l==r)
	{
		tree[k].big.sum=a[l];
		tree[k].sm.sum=-a[l];
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=a[l];tree[k].big.l[i]=l;tree[k].big.r[i]=l;
		tree[k].sm.x[i]=-a[l];tree[k].sm.l[i]=l;tree[k].sm.r[i]=l;
		}
		return;
	}
	int mid=(l+r)>>1;
	++tot;tree[k].nx[0]=tot;
	build(tot,l,mid);
	++tot;tree[k].nx[1]=tot;
	build(tot,mid+1,r);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
void insert(int k,int x,int y)
{
	push(k);
	if(tree[k].sl==tree[k].sr)
	{
		tree[k].sm.sum=-x;
		tree[k].big.sum=x;
		for(int i=0;i<=2;i++)
		{
		tree[k].big.x[i]=x;
		tree[k].sm.x[i]=-x;
		}
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(y<=mid)insert(tree[k].nx[0],x,y);
	else insert(tree[k].nx[1],x,y);
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
Node ask(int k,int l,int r)
{
	push(k);Node use;
	if(tree[k].sl==l&&tree[k].sr==r)return(tree[k]);
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)use=ask(tree[k].nx[0],l,r);
	else if(mid<l)use=ask(tree[k].nx[1],l,r);
	else
		{
			Node a=ask(tree[k].nx[0],l,mid);
			Node b=ask(tree[k].nx[1],mid+1,r);
			use.big=update(a.big,b.big);
			use.sm=update(a.sm,b.sm);
		}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
	return use;
}
void reseve(int k,int l,int r)
{
	push(k);
	if(tree[k].sl==l&&tree[k].sr==r)
	{
		tree[k].tag^=1;
		swap(tree[k].big,tree[k].sm);
		return;
	}
	int mid=(tree[k].sl+tree[k].sr)>>1;
	if(r<=mid)reseve(tree[k].nx[0],l,r);
	else if(mid<l)reseve(tree[k].nx[1],l,r);
	else
	{
		reseve(tree[k].nx[0],l,mid);
		reseve(tree[k].nx[1],mid+1,r);
	}
	tree[k].big=update(tree[tree[k].nx[0]].big,tree[tree[k].nx[1]].big);
	tree[k].sm=update(tree[tree[k].nx[0]].sm,tree[tree[k].nx[1]].sm);
}
int query(int k,int l,int r)
{
	Node tmp;int up=0;int ans=0;
	for(;k>0;k--)
	{
		tmp=ask(1,l,r);ans+=tmp.big.x[0];
		sl[++up]=tmp.big.l[0];sr[up]=tmp.big.r[0];
		reseve(1,tmp.big.l[0],tmp.big.r[0]);
	}
	for(int i=up;i>=1;i--)
	reseve(1,sl[i],sr[i]);
	return ans;
}
int main()
{
	scanf("%d",&n);	tot=1;
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	build(1,1,n);
	scanf("%d",&q);
	for(int i=1;i<=q;i++)
	{
		int opt,l,r,k,x,y;
		scanf("%d",&opt);
		if(opt==1)
		{
			scanf("%d%d%d",&l,&r,&k);
			printf("%d\n",query(k,l,r));
		}
		else
		{
			scanf("%d%d",&y,&x);
			insert(1,x,y);
		}
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#define PI pair<int,int>
#define PII pair<int,PI>
#define FR first
#define SC second
#define MP make_pair
using namespace std;
struct Tnode
{
	PII sum,lM,lm,rM,rm,M,m;
	bool tag;
}T[262144];
int a[131073];
PI A[25];
int n,m,N;
inline PII merge(PII x,PII y)
{
	return MP(x.FR+y.FR,MP(x.SC.FR,y.SC.SC));
}
inline void newnode(int i,int x,int y)
{
	PII A,lA,rA;
	T[i].sum=MP(y,MP(x,x)); lA=MP(0,MP(x,x-1)); rA=MP(0,MP(x+1,x));
	T[i].lM=max(T[i].sum,lA); T[i].lm=min(T[i].sum,lA);
	T[i].rM=max(T[i].sum,rA); T[i].rm=min(T[i].sum,rA);
	T[i].M=max(T[i].sum,lA); T[i].m=min(T[i].sum,lA);
}
inline Tnode update(Tnode &x,Tnode &y)
{
	Tnode z;
	z.tag=false; z.sum=merge(x.sum,y.sum);
	z.lM=max(x.lM,merge(x.sum,y.lM)); z.lm=min(x.lm,merge(x.sum,y.lm));
	z.rM=max(y.rM,merge(x.rM,y.sum)); z.rm=min(y.rm,merge(x.rm,y.sum));
	z.M=max(max(x.M,y.M),merge(x.rM,y.lM));
	z.m=min(min(x.m,y.m),merge(x.rm,y.lm));
	return z;
}
void build(int i,int l,int r)
{
	int A,mid;
	if (l==r) newnode(i,l,a[l]);
	else
	{
		mid=(l+r)/2;
		build(i*2,l,mid); build(i*2+1,mid+1,r);
		T[i]=update(T[i*2],T[i*2+1]);
	}
}
inline void mutate(int i)
{
	swap(T[i].lM,T[i].lm); swap(T[i].rM,T[i].rm); swap(T[i].M,T[i].m);
	T[i].sum.FR*=-1; T[i].lM.FR*=-1; T[i].lm.FR*=-1;
	T[i].rM.FR*=-1; T[i].rm.FR*=-1; T[i].M.FR*=-1; T[i].m.FR*=-1;
	T[i].tag^=1;
}
inline void release(int i,int l0,int r0)
{
	if ((l0<r0)&&T[i].tag)
	{
		mutate(i*2); mutate(i*2+1);
	}
	T[i].tag=false;
}
void modify(int i,int x,int l0,int r0,int d)
{
	int mid;
	if (l0==r0) newnode(i,x,d);
	else
	{
		mid=(l0+r0)/2; release(i,l0,r0);
		if (x<=mid) modify(i*2,x,l0,mid,d);
		else modify(i*2+1,x,mid+1,r0,d);
		T[i]=update(T[i*2],T[i*2+1]);
	}
}
void modify2(int i,int l,int r,int l0,int r0)
{
	int mid;
	if ((l0>=l)&&(r0<=r)) mutate(i);
	else
	{
		mid=(l0+r0)/2; release(i,l0,r0);
		if (l<=mid) modify2(i*2,l,r,l0,mid);
		if (r>=mid+1) modify2(i*2+1,l,r,mid+1,r0);
		T[i]=update(T[i*2],T[i*2+1]);
	}
}
Tnode query(int i,int l,int r,int l0,int r0)
{
	Tnode x,y;
	int mid,cnt;
	if ((l0>=l)&&(r0<=r)) return T[i];
	mid=(l0+r0)/2; cnt=0; release(i,l0,r0);
	if (l<=mid)
	{
		cnt++; x=query(i*2,l,r,l0,mid);
	}
	if (r>=mid+1)
	{
		cnt+=2; y=query(i*2+1,l,r,mid+1,r0);
	}
	if (cnt==1) return x;
	if (cnt==2) return y;
	return update(x,y);
}
int main()
{
	Tnode X;
	int i,j,op,x,y,z,ans;
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%d",&a[i]);
	N=1<<int(ceil(log(n)/log(2)));
	build(1,1,N);
	scanf("%d",&m);
	for (i=1;i<=m;i++)
	{
		scanf("%d",&op);
		if (op==0)
		{
			scanf("%d%d",&x,&y);
			modify(1,x,1,N,y);
		}
		else
		{
			scanf("%d%d%d",&x,&y,&z);
			ans=0;
			for (j=1;j<=z;j++)
			{
				X=query(1,x,y,1,N);
				if (X.M.FR<=0) break;
				ans+=X.M.FR; A[j]=X.M.SC;
				modify2(1,A[j].FR,A[j].SC,1,N);
			}
			for (j--;j>=1;j--) modify2(1,A[j].FR,A[j].SC,1,N);
			printf("%d\n",ans);
		}
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#define PI pair<int,int>
#define PII pair<int,PI>
#define FR first
#define SC second
#define MP make_pair
using namespace std;
struct Tnode
{
	PII M,m;
	PI lM,lm,rM,rm;
	int sum;
	bool tag;
}T[262144];
int a[131073];
PI A[25];
int n,m,N;
inline PI _merge(int x,PI y)
{
	return MP(x+y.FR,y.SC);
}
inline PII merge(PI x,PI y)
{
	return MP(x.FR+y.FR,MP(x.SC,y.SC));
}
inline void newnode(int i,int x,int y)
{
	PII A,B;
	PI lA,lB,rA,rB;
	T[i].sum=y; lA=MP(y,x); lB=MP(0,x-1);
	T[i].lM=max(lA,lB); T[i].lm=min(lA,lB);
	rA=MP(y,x); rB=MP(0,x+1);
	T[i].rM=max(rA,rB); T[i].rm=min(rA,rB);
	A=MP(y,MP(x,x)); B=MP(0,MP(x,x-1));
	T[i].M=max(A,B); T[i].m=min(A,B);
}
inline Tnode update(Tnode &x,Tnode &y)
{
	Tnode z;
	z.tag=false; z.sum=x.sum+y.sum;
	z.lM=max(x.lM,_merge(x.sum,y.lM)); z.lm=min(x.lm,_merge(x.sum,y.lm));
	z.rM=max(y.rM,_merge(y.sum,x.rM)); z.rm=min(y.rm,_merge(y.sum,x.rm));
	z.M=max(max(x.M,y.M),merge(x.rM,y.lM));
	z.m=min(min(x.m,y.m),merge(x.rm,y.lm));
	return z;
}
void build(int i,int l,int r)
{
	int A,mid;
	if (l==r) newnode(i,l,a[l]);
	else
	{
		mid=(l+r)/2;
		build(i*2,l,mid); build(i*2+1,mid+1,r);
		T[i]=update(T[i*2],T[i*2+1]);
	}
}
inline void mutate(int i)
{
	swap(T[i].lM,T[i].lm); swap(T[i].rM,T[i].rm); swap(T[i].M,T[i].m);
	T[i].sum*=-1; T[i].lM.FR*=-1; T[i].lm.FR*=-1;
	T[i].rM.FR*=-1; T[i].rm.FR*=-1; T[i].M.FR*=-1; T[i].m.FR*=-1;
	T[i].tag^=1;
}
inline void release(int i,int l0,int r0)
{
	if ((l0<r0)&&T[i].tag)
	{
		mutate(i*2); mutate(i*2+1);
	}
	T[i].tag=false;
}
void modify(int i,int x,int l0,int r0,int d)
{
	int mid;
	if (l0==r0) newnode(i,x,d);
	else
	{
		mid=(l0+r0)/2; release(i,l0,r0);
		if (x<=mid) modify(i*2,x,l0,mid,d);
		else modify(i*2+1,x,mid+1,r0,d);
		T[i]=update(T[i*2],T[i*2+1]);
	}
}
void modify2(int i,int l,int r,int l0,int r0)
{
	int mid;
	if ((l0>=l)&&(r0<=r)) mutate(i);
	else
	{
		mid=(l0+r0)/2; release(i,l0,r0);
		if (l<=mid) modify2(i*2,l,r,l0,mid);
		if (r>=mid+1) modify2(i*2+1,l,r,mid+1,r0);
		T[i]=update(T[i*2],T[i*2+1]);
	}
}
Tnode query(int i,int l,int r,int l0,int r0)
{
	Tnode x,y;
	int mid,cnt;
	if ((l0>=l)&&(r0<=r)) return T[i];
	mid=(l0+r0)/2; cnt=0; release(i,l0,r0);
	if (l<=mid)
	{
		cnt++; x=query(i*2,l,r,l0,mid);
	}
	if (r>=mid+1)
	{
		cnt+=2; y=query(i*2+1,l,r,mid+1,r0);
	}
	if (cnt==1) return x;
	if (cnt==2) return y;
	return update(x,y);
}
int main()
{
	Tnode X;
	int i,j,op,x,y,z,ans;
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%d",&a[i]);
	N=1<<int(ceil(log(n)/log(2)));
	build(1,1,N);
	scanf("%d",&m);
	for (i=1;i<=m;i++)
	{
		scanf("%d",&op);
		if (op==0)
		{
			scanf("%d%d",&x,&y);
			modify(1,x,1,N,y);
		}
		else
		{
			scanf("%d%d%d",&x,&y,&z);
			ans=0;
			for (j=1;j<=z;j++)
			{
				X=query(1,x,y,1,N);
				if (X.M.FR<=0) break;
				ans+=X.M.FR; A[j]=X.M.SC;
				modify2(1,A[j].FR,A[j].SC,1,N);
			}
			for (j--;j>=1;j--) modify2(1,A[j].FR,A[j].SC,1,N);
			printf("%d\n",ans);
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<ctime>
#include<functional>
#include<sstream>
#define sqr(x) (x)*(x)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define pq priority_queue
#define rf random_shuffle
#define np next_permutation
#define debug(x) cout<<#x<<"="<<x<<endl;
using namespace std;

int n,m,k,a[100005];
inline void read(int &x){
	x=0;char ch=getchar(),flag=0;
	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();
	if(ch=='-') flag=1,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	if(flag) x=-x;
}
struct node{
	int maxl,maxr,minl,minr,maxx,minx;
	int sum,lmx,rmx,mxl,mxr,lmi,rmi,mil,mir;
	bool flag;
}p[800005];
inline node merge(node a,node b){
	node t;t.flag=0;
	t.sum=a.sum+b.sum;
	if(a.maxl>a.sum+b.maxl) t.maxl=a.maxl,t.lmx=a.lmx; else t.maxl=a.sum+b.maxl,t.lmx=b.lmx;
	if(a.minl<a.sum+b.minl) t.minl=a.minl,t.lmi=a.lmi; else t.minl=a.sum+b.minl,t.lmi=b.lmi;
	if(b.maxr>b.sum+a.maxr) t.maxr=b.maxr,t.rmx=b.rmx; else t.maxr=b.sum+a.maxr,t.rmx=a.rmx;
	if(b.minr<b.sum+a.minr) t.minr=b.minr,t.rmi=b.rmi; else t.minr=b.sum+a.minr,t.rmi=a.rmi;
	t.maxx=max(max(a.maxx,b.maxx),a.maxr+b.maxl);
	if(t.maxx==a.maxx) t.mxl=a.mxl,t.mxr=a.mxr;
	else if(t.maxx==b.maxx) t.mxl=b.mxl,t.mxr=b.mxr;
	else if(t.maxx==a.maxr+b.maxl) t.mxl=a.rmx,t.mxr=b.lmx;
	t.minx=min(min(a.minx,b.minx),a.minr+b.minl);
	if(t.minx==a.minx) t.mil=a.mil,t.mir=a.mir;
	else if(t.minx==b.minx) t.mil=b.mil,t.mir=b.mir;
	else if(t.minx==a.minr+b.minl) t.mil=a.rmi,t.mir=b.lmi;
	return t;
}
inline node rev(node x){
	node t;
	t.flag^=1;
	t.sum=-x.sum;
	t.maxl=-x.minl;t.lmx=x.lmi;
	t.minl=-x.maxl;t.lmi=x.lmx;
	t.maxr=-x.minr;t.rmx=x.rmi;
	t.minr=-x.maxr;t.rmi=x.rmx;
	t.maxx=-x.minx;t.mxl=x.mil,t.mxr=x.mir;
	t.minx=-x.maxx;t.mil=x.mxl;t.mir=x.mxr;
	return t;
}
inline void pushdown(int num){
	if(!p[num].flag) return;
	p[num<<1]=rev(p[num<<1]);p[num<<1|1]=rev(p[num<<1|1]);
	p[num].flag=0;
}
inline void update(int num){
	p[num]=merge(p[num<<1],p[num<<1|1]);
}
inline void change1(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(l==tl&&r==tr){
		p[num]=rev(p[num]);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change1(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) change1(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	update(num);
}
inline void newnode(node &a,int b,int c){
	a.flag=0;
	a.maxl=a.maxr=a.maxx=a.minl=a.minr=a.minx=a.sum=b;
	a.mxl=a.mil=a.mxr=a.mir=a.lmx=a.rmx=a.lmi=a.rmi=c;
}
inline void change2(int l,int r,int tl,int x,int num){
	pushdown(num);
	if(l==r){
		newnode(p[num],x,tl);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change2(l,mid,tl,x,num<<1); else change2(mid+1,r,tl,x,num<<1|1);
	update(num);
}
inline node query(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(tl==l&&r==tr) return p[num];
	int mid=(l+r)>>1,vl=0,vr=0;
	node rl,rr;
	if(tl<=mid) vl=1,rl=query(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) vr=1,rr=query(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	if(vl&&!vr) return rl;
	if(!vl&&vr) return rr;
	return merge(rl,rr);
}

int main(){
	read(n);//scanf("%d",&n);
	for(int i=1;i<=n;i++){
		read(a[i]);//scanf("%d",&a[i]);
		change2(1,n,i,a[i],1);
//		printf("%d\n",a[i]);
	}
	read(m);//scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int l,r,opt;
		read(opt);read(l);read(r);//scanf("%d%d%d",&opt,&l,&r);
		if(!opt)
			change2(1,n,l,r,1);
		else{
			int k,tot=0,ans=0,ansl[25],ansr[25];
			read(k);//scanf("%d",&k);
			for(int j=1;j<=k;j++){
				node res=query(1,n,l,r,1);
//				printf("%d %d \n",p[1].maxx,p[1].minx);
				if(res.maxx<=0) continue;
				ans+=res.maxx;ansl[j]=res.mxl;ansr[j]=res.mxr;
//				printf("%d %d ",res.mxl,res.mxr);	printf("%d %d\n",res.maxx,res.minx);
				change1(1,n,ansl[j],ansr[j],1);
//				if(p[1].flag) puts("Yes"); else puts("No");
//			printf("%d\n",query(1,n,1,1,1).minx);
				tot=j;
			}
			printf("%d\n",ans);
			for(int j=1;j<=tot;j++) change1(1,n,ansl[j],ansr[j],1);
		}

	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<ctime>
#include<functional>
#include<sstream>
#define sqr(x) (x)*(x)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define pq priority_queue
#define rf random_shuffle
#define np next_permutation
#define debug(x) cout<<#x<<"="<<x<<endl;
using namespace std;

int n,m,k,a[100005];
struct node{
	int maxl,maxr,minl,minr,maxx,minx;
	int sum,lmx,rmx,mxl,mxr,lmi,rmi,mil,mir;
	int flag;
}p[800005];
inline node merge(node a,node b){
	node t;
	t.sum=a.sum+b.sum;
	if(a.maxl>a.sum+b.maxl) t.maxl=a.maxl,t.lmx=a.lmx; else t.maxl=a.sum+b.maxl,t.lmx=b.lmx;
	if(a.minl<a.sum+b.minl) t.minl=a.minl,t.lmi=a.lmi; else t.minl=a.sum+b.minl,t.lmi=b.lmi;
	if(b.maxr>b.sum+a.maxr) t.maxr=b.maxr,t.rmx=b.rmx; else t.maxr=b.sum+a.maxr,t.rmx=a.rmx;
	if(b.minr<b.sum+a.minr) t.minr=b.minr,t.rmi=b.rmi; else t.minr=b.sum+a.minr,t.rmi=a.rmi;
	t.maxx=max(max(a.maxx,b.maxx),a.maxr+b.maxl);
	if(t.maxx==a.maxx) t.mxl=a.mxl,t.mxr=a.mxr;
	else if(t.maxx==b.maxx) t.mxl=b.mxl,t.mxr=b.mxr;
	else if(t.maxx==a.maxr+b.maxl) t.mxl=a.rmx,t.mxr=b.lmx;
	t.minx=min(min(a.minx,b.minx),a.minr+b.minl);
	if(t.minx==a.minx) t.mil=a.mil,t.mir=a.mir;
	else if(t.minx==b.minx) t.mil=b.mil,t.mir=b.mir;
	else if(t.minx==a.minr+b.minl) t.mil=a.rmi,t.mir=b.lmi;
	return t;
}
inline void rev(node &x){
	x.flag^=1;
	x.sum=-x.sum;
	swap(x.maxl,x.minl);swap(x.maxr,x.minr);swap(x.maxx,x.minx);
	swap(x.lmi,x.lmx);swap(x.rmi,x.rmx);swap(x.mxl,x.mil);swap(x.mxr,x.mir);
	x.maxl=-x.maxl;x.minl=-x.minl;x.maxr=-x.maxr;x.minr=-x.minr;
	x.maxx=-x.maxx;x.minx=-x.minx;
}
inline void pushdown(int num){
//	printf("push:%d\n",num);
	if(p[num].flag==0) return;
//	printf("pushdown: %d to %d %d\n",num,num<<1,num<<1|1);

	rev(p[num<<1]);rev(p[num<<1|1]);
	p[num].flag=0;
}
inline void update(int num){
	pushdown(num);
	p[num]=merge(p[num<<1],p[num<<1|1]);
}
inline void change1(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(l==tl&&r==tr){
		rev(p[num]);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change1(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) change1(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	update(num);
}
inline void newnode(node &a,int b,int c){
	a.flag=0;
	a.maxl=a.maxr=a.maxx=a.minl=a.minr=a.minx=a.sum=b;
	a.mxl=a.mil=a.mxr=a.mir=a.lmx=a.rmx=a.lmi=a.rmi=c;
}
inline void change2(int l,int r,int tl,int x,int num){
	pushdown(num);
	if(l==r){
		newnode(p[num],x,tl);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change2(l,mid,tl,x,num<<1); else change2(mid+1,r,tl,x,num<<1|1);
	update(num);
}
inline node query(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(tl==l&&r==tr) return p[num];
	int mid=(l+r)>>1,vl=0,vr=0;
	node rl,rr;
	if(tl<=mid) vl=1,rl=query(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) vr=1,rr=query(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	if(vl&&!vr) return rl;
	if(!vl&&vr) return rr;
	return merge(rl,rr);
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		change2(1,n,i,a[i],1);
//		printf("%d\n",p[1].maxx);
	}
//	printf("%d\n",query(1,n,1,1,1).maxx);
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int l,r,opt;
		scanf("%d%d%d",&opt,&l,&r);
		if(!opt)
			change2(1,n,l,r,1);
		else{
			int k,tot=0,ans=0,ansl[25],ansr[25];
			scanf("%d",&k);
			for(int j=1;j<=k;j++){
				node res=query(1,n,l,r,1);
//				printf("%d %d \n",p[1].maxx,p[1].minx);
				if(res.maxx<=0) continue;
				ans+=res.maxx;ansl[j]=res.mxl;ansr[j]=res.mxr;
//				printf("%d %d ",res.mxl,res.mxr);	printf("%d %d\n",res.maxx,res.minx);
				change1(1,n,ansl[j],ansr[j],1);
//				if(p[1].flag) puts("Yes"); else puts("No");
//				printf("%d\n",query(1,n,1,1,1).minx);
				tot=j;
			}
			printf("%d\n",ans);
			for(int j=1;j<=tot;j++) change1(1,n,ansl[j],ansr[j],1);
//			printf("%d %d\n",p[1].maxx,p[1].minx);
		}

	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<ctime>
#include<functional>
#include<sstream>
#define sqr(x) (x)*(x)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define pq priority_queue
#define rf random_shuffle
#define np next_permutation
#define debug(x) cout<<#x<<"="<<x<<endl;
using namespace std;

int n,m,k,a[100005];
struct node{
	int maxl,maxr,minl,minr,maxx,minx;
	int sum,lmx,rmx,mxl,mxr,lmi,rmi,mil,mir;
	bool flag;
}p[800005];
inline node merge(node a,node b){
	node t;
	t.sum=a.sum+b.sum;
	if(a.maxl>a.sum+b.maxl) t.maxl=a.maxl,t.lmx=a.lmx; else t.maxl=a.sum+b.maxl,t.lmx=b.lmx;
	if(a.minl<a.sum+b.minl) t.minl=a.minl,t.lmi=a.lmi; else t.minl=a.sum+b.minl,t.lmi=b.lmi;
	if(b.maxr>b.sum+a.maxr) t.maxr=b.maxr,t.rmx=b.rmx; else t.maxr=b.sum+a.maxr,t.rmx=a.rmx;
	if(b.minr<b.sum+a.minr) t.minr=b.minr,t.rmi=b.rmi; else t.minr=b.sum+a.minr,t.rmi=a.rmi;
	t.maxx=max(max(a.maxx,b.maxx),a.maxr+b.maxl);
	if(t.maxx==a.maxx) t.mxl=a.mxl,t.mxr=a.mxr;
	else if(t.maxx==b.maxx) t.mxl=b.mxl,t.mxr=b.mxr;
	else if(t.maxx==a.maxr+b.maxl) t.mxl=a.rmx,t.mxr=b.lmx;
	t.minx=min(min(a.minx,b.minx),a.minr+b.minl);
	if(t.minx==a.minx) t.mil=a.mil,t.mir=a.mir;
	else if(t.minx==b.minx) t.mil=b.mil,t.mir=b.mir;
	else if(t.minx==a.minr+b.minl) t.mil=a.rmi,t.mir=b.lmi;
	return t;
}
inline node rev(node x){
	node t;
	if(!x.flag) t.flag=1; else t.flag=0;
	t.sum=-x.sum;
	t.maxl=-x.minl;t.lmx=x.lmi;
	t.minl=-x.maxl;t.lmi=x.lmx;
	t.maxr=-x.minr;t.rmx=x.rmi;
	t.minr=-x.maxr;t.rmi=x.rmx;
	t.maxx=-x.minx;t.mxl=x.mil,t.mxr=x.mir;
	t.minx=-x.maxx;t.mil=x.mxl;t.mir=x.mxr;
	return t;
}
inline void pushdown(int num){
//	printf("push:%d\n",num);
	if(!p[num].flag) return;
//	printf("pushdown: %d to %d %d\n",num,num<<1,num<<1|1);

	p[num<<1]=rev(p[num<<1]);p[num<<1|1]=rev(p[num<<1|1]);
	p[num].flag=0;
}
inline void update(int num){
	pushdown(num);
	p[num]=merge(p[num<<1],p[num<<1|1]);
}
inline void change1(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(l==tl&&r==tr){
		p[num]=rev(p[num]);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change1(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) change1(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	update(num);
}
inline void newnode(node &a,int b,int c){
	a.flag=0;
	a.maxl=a.maxr=a.maxx=a.minl=a.minr=a.minx=a.sum=b;
	a.mxl=a.mil=a.mxr=a.mir=a.lmx=a.rmx=a.lmi=a.rmi=c;
}
inline void change2(int l,int r,int tl,int x,int num){
	pushdown(num);
	if(l==r){
		newnode(p[num],x,tl);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change2(l,mid,tl,x,num<<1); else change2(mid+1,r,tl,x,num<<1|1);
	update(num);
}
inline node query(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(tl==l&&r==tr) return p[num];
	int mid=(l+r)>>1,vl=0,vr=0;
	node rl,rr;
	if(tl<=mid) vl=1,rl=query(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) vr=1,rr=query(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	if(vl&&!vr) return rl;
	if(!vl&&vr) return rr;
	return merge(rl,rr);
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		change2(1,n,i,a[i],1);
//		printf("%d\n",p[1].maxx);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int l,r,opt;
		scanf("%d%d%d",&opt,&l,&r);
		if(!opt)
			change2(1,n,l,r,1);
		else{
			int k,tot=0,ans=0,ansl[25],ansr[25];
			scanf("%d",&k);
			for(int j=1;j<=k;j++){
				node res=query(1,n,l,r,1);
//				printf("%d %d \n",p[1].maxx,p[1].minx);
				if(res.maxx<=0) continue;
				ans+=res.maxx;ansl[j]=res.mxl;ansr[j]=res.mxr;
//				printf("%d %d ",res.mxl,res.mxr);	printf("%d %d\n",res.maxx,res.minx);
				change1(1,n,ansl[j],ansr[j],1);
//				if(p[1].flag) puts("Yes"); else puts("No");
//			printf("%d\n",query(1,n,1,1,1).minx);
				tot=j;
			}
			printf("%d\n",ans);
			for(int j=1;j<=tot;j++) change1(1,n,ansl[j],ansr[j],1);
//			printf("%d %d\n",p[1].maxx,p[1].minx);
		}

	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<ctime>
#include<functional>
#include<sstream>
#define sqr(x) (x)*(x)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define pq priority_queue
#define rf random_shuffle
#define np next_permutation
#define debug(x) cout<<#x<<"="<<x<<endl;
using namespace std;

inline void read(int &x){
	x=0;char ch=getchar(),flag=0;
	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();
	if(ch=='-') flag=1,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	if(flag) x=-x;
}
int n,m,k,a[100005];
struct node{
	int maxl,maxr,minl,minr,maxx,minx;
	int sum,lmx,rmx,mxl,mxr,lmi,rmi,mil,mir;
	bool flag;
}p[800005];
inline node merge(node a,node b){
	node t;t.flag=0;
	t.sum=a.sum+b.sum;
	if(a.maxl>a.sum+b.maxl) t.maxl=a.maxl,t.lmx=a.lmx; else t.maxl=a.sum+b.maxl,t.lmx=b.lmx;
	if(a.minl<a.sum+b.minl) t.minl=a.minl,t.lmi=a.lmi; else t.minl=a.sum+b.minl,t.lmi=b.lmi;
	if(b.maxr>b.sum+a.maxr) t.maxr=b.maxr,t.rmx=b.rmx; else t.maxr=b.sum+a.maxr,t.rmx=a.rmx;
	if(b.minr<b.sum+a.minr) t.minr=b.minr,t.rmi=b.rmi; else t.minr=b.sum+a.minr,t.rmi=a.rmi;
	t.maxx=max(max(a.maxx,b.maxx),a.maxr+b.maxl);
	if(t.maxx==a.maxx) t.mxl=a.mxl,t.mxr=a.mxr;
	else if(t.maxx==b.maxx) t.mxl=b.mxl,t.mxr=b.mxr;
	else if(t.maxx==a.maxr+b.maxl) t.mxl=a.rmx,t.mxr=b.lmx;
	t.minx=min(min(a.minx,b.minx),a.minr+b.minl);
	if(t.minx==a.minx) t.mil=a.mil,t.mir=a.mir;
	else if(t.minx==b.minx) t.mil=b.mil,t.mir=b.mir;
	else if(t.minx==a.minr+b.minl) t.mil=a.rmi,t.mir=b.lmi;
	return t;
}
inline node rev(node x){
	node t;
	if(!x.flag) t.flag=1; else t.flag=0;
	t.sum=-x.sum;
	t.maxl=-x.minl;t.lmx=x.lmi;
	t.minl=-x.maxl;t.lmi=x.lmx;
	t.maxr=-x.minr;t.rmx=x.rmi;
	t.minr=-x.maxr;t.rmi=x.rmx;
	t.maxx=-x.minx;t.mxl=x.mil,t.mxr=x.mir;
	t.minx=-x.maxx;t.mil=x.mxl;t.mir=x.mxr;
	return t;
}
inline void pushdown(int num){
//	printf("push:%d\n",num);
	if(!p[num].flag) return;
//	printf("pushdown: %d to %d %d\n",num,num<<1,num<<1|1);

	p[num<<1]=rev(p[num<<1]);p[num<<1|1]=rev(p[num<<1|1]);
	p[num].flag=0;
}
inline void update(int num){
	p[num]=merge(p[num<<1],p[num<<1|1]);
}
inline void change1(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(l==tl&&r==tr){
		p[num]=rev(p[num]);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change1(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) change1(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	update(num);
}
inline void newnode(node &a,int b,int c){
	a.flag=0;
	a.maxl=a.maxr=a.maxx=a.minl=a.minr=a.minx=a.sum=b;
	a.mxl=a.mil=a.mxr=a.mir=a.lmx=a.rmx=a.lmi=a.rmi=c;
}
inline void change2(int l,int r,int tl,int x,int num){
	pushdown(num);
	if(l==r){
		newnode(p[num],x,tl);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change2(l,mid,tl,x,num<<1); else change2(mid+1,r,tl,x,num<<1|1);
	update(num);
}
inline node query(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(tl==l&&r==tr) return p[num];
	int mid=(l+r)>>1,vl=0,vr=0;
	node rl,rr;
	if(tl<=mid) vl=1,rl=query(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) vr=1,rr=query(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	if(vl&&!vr) return rl;
	if(!vl&&vr) return rr;
	return merge(rl,rr);
}

int main(){
//	freopen("diyiti.in","r",stdin);
//	freopen("diyiti.out","w",stdout);
	memset(p,0,sizeof(p));
	read(n);
	for(int i=1;i<=n;i++){
		read(a[i]);
		change2(1,n,i,a[i],1);
//		printf("%d\n",p[1].maxx);
	}
	read(m);
	for(int i=1;i<=m;i++){
		int l,r,opt;
		read(opt);read(l);read(r);
		if(!opt)
			change2(1,n,l,r,1);
		else{
			int k,tot=0,ans=0,ansl[25],ansr[25];
			read(k);
			for(int j=1;j<=k;j++){
				node res=query(1,n,l,r,1);
//				printf("%d %d \n",p[1].maxx,p[1].minx);
				if(res.maxx<=0) continue;
				ans+=res.maxx;ansl[j]=res.mxl;ansr[j]=res.mxr;
//				printf("%d %d ",res.mxl,res.mxr);	printf("%d %d\n",res.maxx,res.minx);
				change1(1,n,ansl[j],ansr[j],1);
//				if(p[1].flag) puts("Yes"); else puts("No");
//			printf("%d\n",query(1,n,1,1,1).minx);
				tot=j;
			}
			printf("%d\n",ans);
			for(int j=1;j<=tot;j++) change1(1,n,ansl[j],ansr[j],1);
//			printf("%d %d\n",p[1].maxx,p[1].minx);
		}

	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<ctime>
#include<functional>
#include<sstream>
#define sqr(x) (x)*(x)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define pq priority_queue
#define rf random_shuffle
#define np next_permutation
#define debug(x) cout<<#x<<"="<<x<<endl;
using namespace std;

int n,m,k,a[100005];
inline void read(int &x){
	x=0;char ch=getchar();
	while(ch>'9'||ch<'0') ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
}
struct node{
	int maxl,maxr,minl,minr,maxx,minx;
	int sum,lmx,rmx,mxl,mxr,lmi,rmi,mil,mir;
	bool flag;
}p[800005];
inline node merge(node a,node b){
	node t;t.flag=0;
	t.sum=a.sum+b.sum;
	if(a.maxl>a.sum+b.maxl) t.maxl=a.maxl,t.lmx=a.lmx; else t.maxl=a.sum+b.maxl,t.lmx=b.lmx;
	if(a.minl<a.sum+b.minl) t.minl=a.minl,t.lmi=a.lmi; else t.minl=a.sum+b.minl,t.lmi=b.lmi;
	if(b.maxr>b.sum+a.maxr) t.maxr=b.maxr,t.rmx=b.rmx; else t.maxr=b.sum+a.maxr,t.rmx=a.rmx;
	if(b.minr<b.sum+a.minr) t.minr=b.minr,t.rmi=b.rmi; else t.minr=b.sum+a.minr,t.rmi=a.rmi;
	t.maxx=max(max(a.maxx,b.maxx),a.maxr+b.maxl);
	if(t.maxx==a.maxx) t.mxl=a.mxl,t.mxr=a.mxr;
	else if(t.maxx==b.maxx) t.mxl=b.mxl,t.mxr=b.mxr;
	else if(t.maxx==a.maxr+b.maxl) t.mxl=a.rmx,t.mxr=b.lmx;
	t.minx=min(min(a.minx,b.minx),a.minr+b.minl);
	if(t.minx==a.minx) t.mil=a.mil,t.mir=a.mir;
	else if(t.minx==b.minx) t.mil=b.mil,t.mir=b.mir;
	else if(t.minx==a.minr+b.minl) t.mil=a.rmi,t.mir=b.lmi;
	return t;
}
inline node rev(node x){
	node t;
	t.flag^=1;
	t.sum=-x.sum;
	t.maxl=-x.minl;t.lmx=x.lmi;
	t.minl=-x.maxl;t.lmi=x.lmx;
	t.maxr=-x.minr;t.rmx=x.rmi;
	t.minr=-x.maxr;t.rmi=x.rmx;
	t.maxx=-x.minx;t.mxl=x.mil,t.mxr=x.mir;
	t.minx=-x.maxx;t.mil=x.mxl;t.mir=x.mxr;
	return t;
}
inline void pushdown(int num){
	if(!p[num].flag) return;
	p[num<<1]=rev(p[num<<1]);p[num<<1|1]=rev(p[num<<1|1]);
	p[num].flag=0;
}
inline void update(int num){
	p[num]=merge(p[num<<1],p[num<<1|1]);
}
inline void change1(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(l==tl&&r==tr){
		p[num]=rev(p[num]);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change1(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) change1(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	update(num);
}
inline void newnode(node &a,int b,int c){
	a.flag=0;
	a.maxl=a.maxr=a.maxx=a.minl=a.minr=a.minx=a.sum=b;
	a.mxl=a.mil=a.mxr=a.mir=a.lmx=a.rmx=a.lmi=a.rmi=c;
}
inline void change2(int l,int r,int tl,int x,int num){
	pushdown(num);
	if(l==r){
		newnode(p[num],x,tl);
		return;
	}
	int mid=(l+r)>>1;
	if(tl<=mid) change2(l,mid,tl,x,num<<1); else change2(mid+1,r,tl,x,num<<1|1);
	update(num);
}
inline node query(int l,int r,int tl,int tr,int num){
	pushdown(num);
	if(tl==l&&r==tr) return p[num];
	int mid=(l+r)>>1,vl=0,vr=0;
	node rl,rr;
	if(tl<=mid) vl=1,rl=query(l,mid,tl,min(tr,mid),num<<1);
	if(tr>mid) vr=1,rr=query(mid+1,r,max(mid+1,tl),tr,num<<1|1);
	if(vl&&!vr) return rl;
	if(!vl&&vr) return rr;
	return merge(rl,rr);
}

int main(){
	read(n);
	for(int i=1;i<=n;i++){
		read(a[i]);
		change2(1,n,i,a[i],1);
//		printf("%d\n",p[1].maxx);
	}
	read(m);
	for(int i=1;i<=m;i++){
		int l,r,opt;
		read(opt);read(l);read(r);
		if(!opt)
			change2(1,n,l,r,1);
		else{
			int k,tot=0,ans=0,ansl[25],ansr[25];
			read(k);
			for(int j=1;j<=k;j++){
				node res=query(1,n,l,r,1);
//				printf("%d %d \n",p[1].maxx,p[1].minx);
				if(res.maxx<=0) continue;
				ans+=res.maxx;ansl[j]=res.mxl;ansr[j]=res.mxr;
//				printf("%d %d ",res.mxl,res.mxr);	printf("%d %d\n",res.maxx,res.minx);
				change1(1,n,ansl[j],ansr[j],1);
//				if(p[1].flag) puts("Yes"); else puts("No");
//			printf("%d\n",query(1,n,1,1,1).minx);
				tot=j;
			}
			printf("%d\n",ans);
			for(int j=1;j<=tot;j++) change1(1,n,ansl[j],ansr[j],1);
		}

	}
	return 0;
}


#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<queue>

using namespace std;

const int N=400400;

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

struct data{
	pos u[2],v[2],w[2],s;
};

struct seg{
	int l,r;
	bool f;
	pos p;
};

pos operator + (pos x,pos y){
	pos z;
	z.s=x.s+y.s;
	z.l=x.l;
	z.r=y.r;
	return z;
}

data operator + (data x,data y){
	data z;
	z.s=x.s+y.s;
	if(x.w[0].s<y.w[0].s)z.w[0]=x.w[0];
	else z.w[0]=y.w[0];
	if(z.w[0].s>x.v[0].s+y.u[0].s)z.w[0]=x.v[0]+y.u[0];
	z.u[0]=x.u[0];
	if(z.u[0].s>x.s.s+y.u[0].s)z.u[0]=x.s+y.u[0];
	z.v[0]=y.v[0];
	if(z.v[0].s>y.s.s+x.v[0].s)z.v[0]=x.v[0]+y.s;
	if(x.w[1].s>y.w[1].s)z.w[1]=x.w[1];
	else z.w[1]=y.w[1];
	if(z.w[1].s<x.v[1].s+y.u[1].s)z.w[1]=x.v[1]+y.u[1];
	z.u[1]=x.u[1];
	if(z.u[1].s<x.s.s+y.u[1].s)z.u[1]=x.s+y.u[1];
	z.v[1]=y.v[1];
	if(z.v[1].s<y.s.s+x.v[1].s)z.v[1]=x.v[1]+y.s;
	return z;
}

bool operator < (seg x,seg y){
	return (x.f?x.p.s:-x.p.s)<(y.f?y.p.s:-y.p.s);
}

priority_queue<seg> h;
data a[N];
int t[N];
seg x,y,z,f;
int n,m,i,j,k,u,v,w,ans;

void build(int cur,int l,int r){
	int mid;
	if(l==r){
		a[cur].s=a[cur].u[0]=a[cur].v[0]=a[cur].u[1]=a[cur].v[1]=(pos){l,l,t[l]};
//		a[cur].w[0]=a[cur].w[1]=(pos){l,l,t[l]};
		a[cur].w[0]=a[cur].w[1]=(pos){l,l,0};
		a[cur].w[0].s=min(a[cur].w[0].s,t[l]);
		a[cur].w[1].s=max(a[cur].w[1].s,t[l]);
		return;
	}
	mid=(l+r)>>1;
	build(cur<<1,l,mid);
	build(cur<<1|1,mid+1,r);
	a[cur]=a[cur<<1]+a[cur<<1|1];
}

data ask(int cur,int l,int r,int u,int v){
	int mid;
	if(l>=u && r<=v)return a[cur];
	mid=(l+r)>>1;
	if(v<=mid)return ask(cur<<1,l,mid,u,v);
	if(u>mid)return ask(cur<<1|1,mid+1,r,u,v);
	return ask(cur<<1,l,mid,u,v)+ask(cur<<1|1,mid+1,r,u,v);
}

void modify(int cur,int l,int r,int tar){
	int mid;
	if(l==r){
		a[cur].s=a[cur].u[0]=a[cur].v[0]=a[cur].u[1]=a[cur].v[1]=(pos){l,l,t[l]};
		a[cur].w[0]=a[cur].w[1]=(pos){l,l,0};
		a[cur].w[0].s=min(a[cur].w[0].s,t[l]);
		a[cur].w[1].s=max(a[cur].w[1].s,t[l]);
		return;
	}
	mid=(l+r)>>1;
	if(tar<=mid)modify(cur<<1,l,mid,tar);
	else modify(cur<<1|1,mid+1,r,tar);
	a[cur]=a[cur<<1]+a[cur<<1|1];
}

int main(){
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&t[i]);
	build(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d",&k);
		if(!k){
			scanf("%d%d",&u,&v);
			t[u]=v;
			modify(1,1,n,u);
		}else{
			scanf("%d%d%d",&u,&v,&w);
			while(!h.empty())h.pop();
			h.push((seg){u,v,1,ask(1,1,n,u,v).w[1]});
			ans=0;
			for(j=1;j<=w;j++){
				f=h.top();
				h.pop();
				if(f.f){
					if(f.p.s<=0)break;
					ans+=f.p.s;
					if(f.l<f.p.l)h.push((seg){f.l,f.p.l-1,1,ask(1,1,n,f.l,f.p.l-1).w[1]});
					h.push((seg){f.p.l,f.p.r,0,ask(1,1,n,f.p.l,f.p.r).w[0]});
					if(f.p.r<f.r)h.push((seg){f.p.r+1,f.r,1,ask(1,1,n,f.p.r+1,f.r).w[1]});
				}else{
					if(f.p.s>=0)break;
					ans-=f.p.s;
					if(f.l<f.p.l)h.push((seg){f.l,f.p.l-1,0,ask(1,1,n,f.l,f.p.l-1).w[0]});
					h.push((seg){f.p.l,f.p.r,1,ask(1,1,n,f.p.l,f.p.r).w[1]});
					if(f.p.r<f.r)h.push((seg){f.p.r+1,f.r,0,ask(1,1,n,f.p.r+1,f.r).w[0]});
				}
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}


#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<queue>

using namespace std;

const int N=400400;

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

struct data{
	pos u[2],v[2],w[2],s;
};

struct seg{
	int l,r;
	bool f;
	pos p;
};

pos operator + (pos x,pos y){
	pos z;
	z.s=x.s+y.s;
	z.l=x.l;
	z.r=y.r;
	return z;
}

data operator + (data x,data y){
	data z;
	z.s=x.s+y.s;
	if(x.w[0].s<y.w[0].s)z.w[0]=x.w[0];
	else z.w[0]=y.w[0];
	if(z.w[0].s>x.v[0].s+y.u[0].s)z.w[0]=x.v[0]+y.u[0];
	z.u[0]=x.u[0];
	if(z.u[0].s>x.s.s+y.u[0].s)z.u[0]=x.s+y.u[0];
	z.v[0]=y.v[0];
	if(z.v[0].s>y.s.s+x.v[0].s)z.v[0]=x.v[0]+y.s;
	if(x.w[1].s>y.w[1].s)z.w[1]=x.w[1];
	else z.w[1]=y.w[1];
	if(z.w[1].s<x.v[1].s+y.u[1].s)z.w[1]=x.v[1]+y.u[1];
	z.u[1]=x.u[1];
	if(z.u[1].s<x.s.s+y.u[1].s)z.u[1]=x.s+y.u[1];
	z.v[1]=y.v[1];
	if(z.v[1].s<y.s.s+x.v[1].s)z.v[1]=x.v[1]+y.s;
	return z;
}

bool operator < (seg x,seg y){
	return (x.f?x.p.s:-x.p.s)<(y.f?y.p.s:-y.p.s);
}

priority_queue<seg> h;
data a[N];
int t[N];
seg x,y,z,f;
int n,m,i,j,k,u,v,w,ans;

void build(int cur,int l,int r){
	int mid;
	if(l==r){
		a[cur].s=a[cur].u[0]=a[cur].v[0]=a[cur].u[1]=a[cur].v[1]=(pos){l,l,t[l]};
		a[cur].w[0]=a[cur].w[1]=(pos){l,l,0};
		a[cur].w[0].s=min(a[cur].w[0].s,t[l]);
		a[cur].w[1].s=max(a[cur].w[1].s,t[l]);
		return;
	}
	mid=(l+r)>>1;
	build(cur<<1,l,mid);
	build(cur<<1|1,mid+1,r);
	a[cur]=a[cur<<1]+a[cur<<1|1];
}

data ask(int cur,int l,int r,int u,int v){
	int mid;
	if(l>=u && r<=v)return a[cur];
	mid=(l+r)>>1;
	if(v<=mid)return ask(cur<<1,l,mid,u,v);
	if(u>mid)return ask(cur<<1|1,mid+1,r,u,v);
	return ask(cur<<1,l,mid,u,v)+ask(cur<<1|1,mid+1,r,u,v);
}

void modify(int cur,int l,int r,int tar){
	int mid;
	if(l==r){
		a[cur].s=a[cur].u[0]=a[cur].v[0]=a[cur].u[1]=a[cur].v[1]=(pos){l,l,t[l]};
		a[cur].w[0]=a[cur].w[1]=(pos){l,l,0};
		a[cur].w[0].s=min(a[cur].w[0].s,t[l]);
		a[cur].w[1].s=max(a[cur].w[1].s,t[l]);
		return;
	}
	mid=(l+r)>>1;
	if(tar<=mid)modify(cur<<1,l,mid,tar);
	else modify(cur<<1|1,mid+1,r,tar);
	a[cur]=a[cur<<1]+a[cur<<1|1];
}

int main(){
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&t[i]);
	build(1,1,n);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d",&k);
		if(!k){
			scanf("%d%d",&u,&v);
			t[u]=v;
			modify(1,1,n,u);
		}else{
			scanf("%d%d%d",&u,&v,&w);
			while(!h.empty())h.pop();
			h.push((seg){u,v,1,ask(1,1,n,u,v).w[1]});
			ans=0;
			for(j=1;j<=w;j++){
				f=h.top();
				h.pop();
				if(f.f){
					if(f.p.s<=0)break;
					ans+=f.p.s;
					if(f.l<f.p.l)h.push((seg){f.l,f.p.l-1,1,ask(1,1,n,f.l,f.p.l-1).w[1]});
					h.push((seg){f.p.l,f.p.r,0,ask(1,1,n,f.p.l,f.p.r).w[0]});
					if(f.p.r<f.r)h.push((seg){f.p.r+1,f.r,1,ask(1,1,n,f.p.r+1,f.r).w[1]});
				}else{
					if(f.p.s>=0)break;
					ans-=f.p.s;
					if(f.l<f.p.l)h.push((seg){f.l,f.p.l-1,0,ask(1,1,n,f.l,f.p.l-1).w[0]});
					h.push((seg){f.p.l,f.p.r,1,ask(1,1,n,f.p.l,f.p.r).w[1]});
					if(f.p.r<f.r)h.push((seg){f.p.r+1,f.r,0,ask(1,1,n,f.p.r+1,f.r).w[0]});
				}
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}