#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef tuple<int, int, int> tpl;
template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &P){
	in >> P.first >> P.second;
	return in;
}
template<class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &P){
	out << "(" << P.first << ", " << P.second << ")";
	return out;
}
template<class T>
istream &operator>>(istream &in, vector<T> &arr){
	for(auto &x: arr) in >> x;
	return in;
}
template<class T>
ostream &operator<<(ostream &out, const vector<T> &arr){
	for(auto &x: arr) out << x << ' '; cout << "\n";
	return out;
}
template<class T>
istream &operator>>(istream &in, deque<T> &arr){
	for(auto &x: arr) in >> x;
	return in;
}
template<class T>
ostream &operator<<(ostream &out, const deque<T> &arr){
	for(auto &x: arr) out << x << ' '; cout << "\n";
	return out;
}
mt19937 rnd(time(0));

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	vi c(2);
	cin >> n >> c;
	vector<vector<pii>> a(2);
	for(int i = 0; i < n; i ++){
		int x, y;
		char z;
		cin >> x >> y >> z;
		if(z == 'C'){
			a[0].push_back({y, x});
		}
		else{
			a[1].push_back({y, x});
		}
	}
	sort(all(a[0])), sort(all(a[1]));
	for(int i = 0; i < 2; i ++){
		while(!a[i].empty() && a[i].back().first > c[i]){
			a[i].pop_back();
		}
	}
	int res = 0;
	if(!a[0].empty() && !a[1].empty()){
		for(int i = 0; i < 2; i ++){
			int mx = 0;
			for(auto x: a[i]){
				mx = max(mx, x.second);
			}
			res += mx;
		}
	}
	for(int k = 0; k < 2; k ++){
		int mx = 0;
		for(int i = a[k].size() - 1, j = 0, cur = 0; i >= j; i --){
			while(i > j && a[k][j].first + a[k][i].first <= c[k]){
				cur = max(cur, a[k][j ++].second);
			}
			if(j){
				mx = max(mx, cur + a[k][i].second);
			}
		}
		res = max(res, mx);
	}
	cout << res;
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                                                                //
//                                              _____________                                       //
//             ++++++++++                ___------------------\                                    //
//           +++`+``+`+`++++        ///`````````````````````````````\                              //
//           ++`+`+``+++`++++  /////```````````````````````````````````\                            //
//           +++`++`+`+``+++/////`````````````````````````````````````````\                         //
//             +++`++`+``+///```````````|```````````````````````````````````\                       //
//          ____++++/++++/`````````````/````````|````````|```````````````````\                      //
//        /  /  / |    //``````````````|````````|```````|````````|````````````\                     //
//       /  /  /  | ///````````/```````|```````||```````|````````|``````\```````\                   //
//       | /  /   |///`````````|``````/````````|````````|````````|```````|```````\                  //
//       |/   |   |//``|```````|``````|````````|`````````|```````|```````|````````\                 //
//       /\___|__//`|``|```````|`    |      ``:|````````|:```````|```````|```|`````|                //
//      /     /  /``|``|``````|/     |        :|     ```:|```````|```````|``++````++                //
//     /     / //```|``|``````|      |        |:        :|    ```|```````|```++``++`\               //
//     |    /  /````|``|``````/    _.::::.    |          |      |    ````|```|`++`\`|               //
//     |   /   |````|``|`````|            `                    |       ``|```++``++`|               //
//     |  /    |````|``|`````|                                 :         |``++````++|               //
//     | /     /````|``|`````|                              _.-:::.      |..`|``.`|.|               //
//     |/     /`````|``|`````|                                     `     |```|````|`|               //
//    /|      |`````|``|`````|                    :'                    .|```|````|.|               //
//   / |      |`````|``|`````|                                         /|-|``|````|`|               //
//  /  |      |`````|```\````|                                        / ||```|````|``\              //
// /   |      |`````|````|```|::                                    /_| ||```|````|``|              //
//            |`````|````|```|:|:.        `.._                    .\___/:|```|````|``|              //
//            |`````\````|```|:|::-          ``:::....           -:|:|:::|```|````|``|              //
//            |``````|```|```|:|::`|.                          .:::|:|:::|```|````|``|              //
//             \`````|```|```|:|::/|--.                     .`:|:::|:|:::/```|````|``|              //
//              |````|```|```\:|:|:|-----             _..-:|:|:|:::|:|::|````|````|`/               //
//              |````|```|````\|:|:|-------.____.....------|/::|:::|:|::|````|````|`|               //
//              |````|```|\````\:|/\___________    ________/\--\:::|:|::|````/````|`|               //
//              |````\```| \```|:/-------------\  /----------\``\::|:|::|```/`````|`|               //
//              |`````|``|  \``|/---------------\/------------\_________|```|`````|`|               //
//                                                                                //
//////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                        Created by Aeren                                          //
//                                                                                //
//////////////////////////////////////////////////////////////////////////////////////////////////////


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
typedef long long ll;
ifstream fin("AAtest.in.txt");
ll n,c,d,ma,b,p,pik,vas,tul,pik1,jad1,jad2,l;
vector<pair<ll,ll> > t,t1;
priority_queue<pair<ll,ll> > pp;
char cc;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>c>>d;
    jad1=-1; jad2=-1;
    for(int i=0; i<n; i++){
        cin>>b>>p>>cc;
        if(cc=='C' && p<=c) t.push_back({p,b}),jad1=max(jad1,b);
        else if(p<=d) t1.push_back({p,b}),jad2=max(jad2,b);
    }
    sort(t.begin(),t.end());
    sort(t1.begin(),t1.end());
    if(jad1!=-1 && jad2!=-1) vas=jad1+jad2;
    jad1=0;
    l=0;
    for(int i=t.size()-1; i>=0; i--){
        while(l<n && t[l].first+t[i].first<=c) pp.push({t[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t[i].second+pp.top().first);
        }
    }
    while(pp.size()) pp.pop();
    l=0;
    for(int i=t1.size()-1; i>=0; i--){
        while(l<n && t1[l].first+t1[i].first<=c) pp.push({t1[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t1[i].second+pp.top().first);
        }
    }
    cout<<vas;
}


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
typedef long long ll;
ifstream fin("AAtest.in.txt");
ll n,c,d,ma,b,p,pik,vas,tul,pik1;
vector<ll> t,t1;
char cc;

ll otsi1(ll n,ll x,ll y,ll i,ll j){
    if(j<x || i>y) return -1;
    if(x>=i && y<=j) return t1[n];
    return max(otsi1(2*n,x,(x+y)/2,i,j),otsi1(2*n+1,(x+y)/2+1,y,i,j));
}

ll otsi(ll n,ll x,ll y,ll i,ll j){
    if(j<x || i>y) return -1;
    if(x>=i && y<=j) return t[n];
    return max(otsi(2*n,x,(x+y)/2,i,j),otsi(2*n+1,(x+y)/2+1,y,i,j));
}

void pane(ll n,ll x,ll y,ll i,ll j,ll v){
    if(j<x || i>y) return ;
    if(x==y){
        t[n]=max(t[n],v);
        return ;
    }
    pane(2*n,x,(x+y)/2,i,j,v);
    pane(2*n+1,(x+y)/2+1,y,i,j,v);
    t[n]=max(t[2*n],t[2*n+1]);
}

void pane1(ll n,ll x,ll y,ll i,ll j,ll v){
    if(j<x || i>y) return ;
    if(x==y){
        t1[n]=max(t[n],v);
        return ;
    }
    pane(2*n,x,(x+y)/2,i,j,v);
    pane(2*n+1,(x+y)/2+1,y,i,j,v);
    t1[n]=max(t1[2*n],t1[2*n+1]);
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>c>>d;
    pik=1;
    while(pik<c) pik*=2;
    t.resize(2*pik+5);
    pik1=1;
    while(pik1<d) pik1*=2;
    t1.resize(2*pik1+5);
    for(int i=0; i<t.size(); i++) t[i]=-1;
    for(int i=0; i<t1.size(); i++) t1[i]=-1;
    for(int i=0; i<n; i++){
        cin>>b>>p>>cc;
        if(cc=='C'){
            tul=max(otsi1(1,1,pik1,1,d),otsi(1,1,pik,1,c-p));
            if(p<=c && tul!=-1) vas=max(vas,tul+b);
            pane(1,1,pik,p,p,b);
        }
        else{
            tul=max(otsi1(1,1,pik1,1,d-p),otsi(1,1,pik,1,c));
            if(p<=d && tul!=-1) vas=max(vas,tul+b);
            pane1(1,1,pik1,p,p,b);
        }
    }
    cout<<vas;
}


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
typedef long long ll;
ifstream fin("AAtest.in.txt");
ll n,c,d,ma,b,p,pik,vas,tul,pik1,jad1,jad2,l;
vector<pair<ll,ll> > t,t1;
priority_queue<pair<ll,ll> > pp;
char cc;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>c>>d;
    jad1=-1; jad2=-1;
    for(int i=0; i<n; i++){
        cin>>b>>p>>cc;
        if(cc=='C' && p<=c) t.push_back({p,b}),jad1=max(jad1,b);
        else if(p<=d) t1.push_back({p,b}),jad2=max(jad2,b);
    }
    sort(t.begin(),t.end());
    sort(t1.begin(),t1.end());
    if(jad1!=-1 && jad2!=-1) vas=jad1+jad2;
    jad1=0;
    l=0;
    for(int i=t.size()-1; i>=0; i--){
        while(l<n && t[l].first+t[i].first<=c) pp.push({t[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t[i].second+pp.top().first);
        }
    }
    while(pp.size()) pp.pop();
    l=0;
    for(int i=t1.size()-1; i>=0; i--){
        while(l<n && t1[l].first+t1[i].first<=d) pp.push({t1[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t1[i].second+pp.top().first);
        }
    }
    cout<<vas;
}


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
typedef long long ll;
ifstream fin("AAtest.in.txt");
ll n,c,d,ma,b,p,pik,vas,tul,pik1,jad1,jad2,l;
vector<pair<ll,ll> > t,t1;
priority_queue<pair<ll,ll> > pp;
char cc;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>c>>d;
    jad1=-1; jad2=-1;
    for(int i=0; i<n; i++){
        cin>>b>>p>>cc;
        if(cc=='C' && p<=c) t.push_back({p,b}),jad1=max(jad1,b);
        else if(p<=d) t1.push_back({p,b}),jad2=max(jad2,b);
    }
    sort(t.begin(),t.end());
    sort(t1.begin(),t1.end());
    if(jad1!=-1 && jad2!=-1) vas=jad1+jad2;
    jad1=0;
    l=0;
    for(int i=t.size()-1; i>=0; i--){
        while(l<t.size() && t[l].first+t[i].first<=c) pp.push({t[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t[i].second+pp.top().first);
        }
    }
    while(pp.size()) pp.pop();
    l=0;
    for(int i=t1.size()-1; i>=0; i--){
        while(l<t1.size() && t1[l].first+t1[i].first<=d) pp.push({t1[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t1[i].second+pp.top().first);
        }
    }
    cout<<vas;
}


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
typedef long long ll;
ifstream fin("AAtest.in.txt");
ll n,c,d,ma,b,p,pik,vas,tul,pik1,jad1,jad2,l;
vector<pair<ll,ll> > t,t1;
priority_queue<pair<ll,ll> > pp;
char cc;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>c>>d;
    for(int i=0; i<n; i++){
        cin>>b>>p>>cc;
        if(cc=='C' && p<=c) t.push_back({p,b}),jad1=max(jad1,b);
        else if(p<=d) t1.push_back({p,b}),jad2=max(jad2,b);
    }
    sort(t.begin(),t.end());
    sort(t1.begin(),t1.end());
    vas=jad1+jad2;
    jad1=0;
    l=0;
    for(int i=t.size()-1; i>=0; i--){
        while(l<n && t[l].first+t[i].first<=c) pp.push({t[l].second,l}),l++;
        if(l){
            if(pp.top().second==i) pp.pop();
            vas=max(vas,t[i].second+pp.top().first);
        }
    }
    while(pp.size()) pp.pop();
    l=0;
    for(int i=t1.size()-1; i>=0; i--){
        while(l<n && t1[l].first+t1[i].first<=c) pp.push({t1[l].second,l}),l++;
        if(l){
            if(pp.top().second==i) pp.pop();
            vas=max(vas,t1[i].second+pp.top().first);
        }
    }
    cout<<vas;
}


#include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
typedef long long ll;
ifstream fin("AAtest.in.txt");
ll n,c,d,ma,b,p,pik,vas,tul,pik1,jad1,jad2,l;
vector<pair<ll,ll> > t,t1;
priority_queue<pair<ll,ll> > pp;
char cc;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin>>n>>c>>d;
    jad1=-1; jad2=-1;
    for(int i=0; i<n; i++){
        cin>>b>>p>>cc;
        if(cc=='C'){
            if(p<=c) t.push_back({p,b}),jad1=max(jad1,b);
        }
        else if(p<=d) t1.push_back({p,b}),jad2=max(jad2,b);
    }
    sort(t.begin(),t.end());
    sort(t1.begin(),t1.end());
    if(jad1!=-1 && jad2!=-1) vas=jad1+jad2;
    jad1=0;
    l=0;
    for(int i=t.size()-1; i>=0; i--){
        while(l<t.size() && t[l].first+t[i].first<=c) pp.push({t[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t[i].second+pp.top().first);
        }
    }
    while(pp.size()) pp.pop();
    l=0;
    for(int i=t1.size()-1; i>=0; i--){
        while(l<t1.size() && t1[l].first+t1[i].first<=d) pp.push({t1[l].second,l}),l++;
        if(l){
            if(pp.size()) if(pp.top().second==i) pp.pop();
            if(pp.size()) vas=max(vas,t1[i].second+pp.top().first);
        }
    }
    cout<<vas;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
using ll = long long;

class SegmentTree {
private:
	int size;
	vector<int> a;

	int get(const int l, const int r, int x, int lx, int rx) {
		if (lx >= l && rx <= r) {
			return a[x];
		}
		if (lx >= r || rx <= l) {
			return INT_MIN;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		int u = get(l, r, left, lx, mid);
		int v = get(l, r, right, mid, rx);
		return max(u, v);
	}

	void add(const int i, const int v, int x, int lx, int rx) {
		if (rx - lx == 1) {
			a[x] = v;
			return;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		if (i < mid) {
			add(i, v, left, lx, mid);
		} else {
			add(i, v, right, mid, rx);
		}
		a[x] = max(a[left], a[right]);
	}

public:
	int get(int l, int r) {
		return get(l, r, 0, 0, size);
	}

	void add(const int i, const int v) {
		add(i, v, 0, 0, size);
	}

	SegmentTree(int n) {
		size = 1;
		while (size < n)size <<= 1;
		a.assign(size * 2, INT_MIN);
	}
};

int main() {
	int n, c, d;
	cin >> n >> c >> d;
	int ans = 0;
	SegmentTree cf(1e5 + 4), df(1e5 + 4);
	for (int i = 0; i < n; i++) {
		int x, y;
		char z;
		cin >> x >> y >> z;
		int b = 0;
		if (z == 'C') {
			if (y > c)continue;
			ans = max(ans, x + max(cf.get(0, c - y), df.get(0, d )));
			cf.add(y, x);
		} else {
			if (y > d)continue;
			ans = max(ans, x + max(df.get(0, d - y), cf.get(0, c)));
			df.add(y, x);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
using ll = long long;

signed main() {
	ll n, c, d;
	cin >> n >> c >> d;
	vector<pair<ll, ll>> cf, df;
	ll md = INT_MIN, mc = INT_MIN;
	for (int i = 0; i < n; i++) {
		ll x, y;
		char u;
		cin >> x >> y >> u;
		if (u == 'C') {
			mc = max(mc, (y <= c) ? x : INT_MIN);
			cf.emplace_back(y, x);
		} else {
			md = max(md, (y <= d) ? x : INT_MIN);
			df.emplace_back(y,x);
		};

	}
	ll ans = 0;

	function<void(vector<pair<ll, ll>> &, ll &)> cal = [&](vector<pair<ll, ll>> &a, ll &cost) {
		sort(a.begin(), a.end());
		vector<ll> mx(a.size());
		for (int i = 0; i < a.size(); i++) {
			mx[i] = max((i) ? mx[i - 1] : 0, a[i].second);
		}
		for (int i = 0; i < a.size(); i++) {
			ll e = lower_bound(a.begin(), a.end(), make_pair(cost - a[i].first, 0ll)) - a.begin();
			if (e < a.size()) {
				if (cost < a[i].first + a[e].first) {
					if (e) {
						if (cost >= a[i].first + a[e - 1].first) {
							ans = max(a[i].second + mx[e - 1], ans);
						}
					}
				} else
					ans = max(a[i].second + mx[e], ans);
			}
		}
	};
	cal(cf, c);
	cal(df, d);
	ans = max(md + mc, ans);
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
using ll = long long;

class SegmentTree {
private:
	int size;
	vector<int> a;

	int get(const int l, const int r, int x, int lx, int rx) {
		if (lx >= l && rx <= r) {
			return a[x];
		}
		if (lx >= r || rx <= l) {
			return INT_MIN;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		int u = get(l, r, left, lx, mid);
		int v = get(l, r, right, mid, rx);
		return max(u, v);
	}

	void add(const int i, const int v, int x, int lx, int rx) {
		if (rx - lx == 1) {
			a[x] = v;
			return;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		if (i < mid) {
			add(i, v, left, lx, mid);
		} else {
			add(i, v, right, mid, rx);
		}
		a[x] = max(a[left], a[right]);
	}

public:
	int get(int l, int r) {
		return get(l, r, 0, 0, size);
	}

	void add(const int i, const int v) {
		add(i, v, 0, 0, size);
	}

	SegmentTree(int n) {
		size = 1;
		while (size < n)size <<= 1;
		a.assign(size * 2, INT_MIN);
	}
};

int main() {
	int n, c, d;
	cin >> n >> c >> d;
	int ans = 0;
	SegmentTree cf(1e5 + 4), df(1e5 + 4);
	for (int i = 0; i < n; i++) {
		int x, y;
		char z;
		cin >> x >> y >> z;
		int b = 0;
		if (z == 'C') {
			if (y > c)continue;
			ans = max(ans, x + max(cf.get(0, c - y + 1), df.get(0, d + 1)));
			cf.add(y, x);
		} else {
			if (y > d)continue;
			ans = max(ans, x + max(df.get(0, d - y + 1), cf.get(0, c + 1)));
			df.add(y, x);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
using ll = long long;

class SegmentTree {
private:
	int size;
	vector<int> a;

	int get(const int l, const int r, int x, int lx, int rx) {
		if (lx >= l && rx <= r) {
			return a[x];
		}
		if (lx >= r || rx <= l) {
			return INT_MIN;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		int u = get(l, r, left, lx, mid);
		int v = get(l, r, right, mid, rx);
		return max(u, v);
	}

	void add(const int i, const int v, int x, int lx, int rx) {
		if (rx - lx == 1) {
			a[x] = max(a[x],v);
			return;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		if (i < mid) {
			add(i, v, left, lx, mid);
		} else {
			add(i, v, right, mid, rx);
		}
		a[x] = max(a[left], a[right]);
	}

public:
	int get(int l, int r) {
		return get(l, r, 0, 0, size);
	}

	void add(const int i, const int v) {
		add(i, v, 0, 0, size);
	}

	SegmentTree(int n) {
		size = 1;
		while (size < n)size <<= 1;
		a.assign(size * 2, INT_MIN);
	}
};

int main() {
	int n, c, d;
	cin >> n >> c >> d;
	int ans = 0;
	SegmentTree cf(1e5 + 1), df(1e5 + 1);
	for (int i = 0; i < n; i++) {
		int x, y;
		char z;
		cin >> x >> y >> z;
		int b = 0;
		if (z == 'C') {
			if (y > c)continue;
			ans = max(ans, x + max(cf.get(0, c - y + 1), df.get(0, d + 1)));
			cf.add(y, x);
		} else {
			if (y > d)continue;
			ans = max(ans, x + max(df.get(0, d - y + 1), cf.get(0, c + 1)));
			df.add(y, x);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
using ll = long long;

signed main() {
	ll n, c, d;
	cin >> n >> c >> d;
	vector<pair<ll, ll>> cf, df;
	ll md = INT_MIN, mc = INT_MIN;
	for (int i = 0; i < n; i++) {
		ll x, y;
		char u;
		cin >> x >> y >> u;
		if (u == 'C') {
			mc = max(mc, (y <= c) ? x : INT_MIN);
			cf.emplace_back(y, x);
		} else {
			md = max(md, (y <= d) ? x : INT_MIN);
			df.emplace_back(y,x);
		};

	}
	ll ans = 0;

	function<void(vector<pair<ll, ll>> &, ll &)> cal = [&](vector<pair<ll, ll>> &a, ll &cost) {
		sort(a.begin(), a.end());
		vector<ll> mx(a.size());
		for (int i = 0; i < a.size(); i++) {
			mx[i] = max((i) ? mx[i - 1] : 0, a[i].second);
		}
		for (int i = 0; i < a.size(); i++) {
			ll e = lower_bound(a.begin(), a.end(), make_pair(cost - a[i].first, 0ll)) - a.begin();
			if (e < n) {
				if (cost < a[i].first + a[e].first) {
					if (e) {
						if (cost >= a[i].first + a[e - 1].first) {
							ans = max(a[i].second + mx[e - 1], ans);
						}
					}
				} else
					ans = max(a[i].second + mx[e], ans);
			}
		}
	};
	cal(cf, c);
	cal(df, d);
	ans = max(md + mc, ans);
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
using ll = long long;

signed main() {
	ll n, c, d;
	cin >> n >> c >> d;
	vector<pair<ll, ll>> cf, df;
	ll md = INT_MIN, mc = INT_MIN;
	for (int i = 0; i < n; i++) {
		ll x, y;
		char u;
		cin >> x >> y >> u;
		if (u == 'C') {
			mc = max(mc, (y <= c) ? x : INT_MIN);
			cf.emplace_back(y, x);
		} else {
			md = max(md, (y <= d) ? x : INT_MIN);
		};

	}
	ll ans = 0;

	function<void(vector<pair<ll, ll>> &, ll &)> cal = [&](vector<pair<ll, ll>> &a, ll &cost) {
		sort(a.begin(), a.end());
		vector<ll> mx(a.size());
		for (int i = 0; i < a.size(); i++) {
			mx[i] = max((i) ? mx[i - 1] : 0, a[i].second);
		}
		for (int i = 0; i < a.size(); i++) {
			ll e = lower_bound(a.begin(), a.end(), make_pair(cost - a[i].first, 0ll)) - a.begin();
			if (e < n) {
				if (cost < a[i].first + a[e].first) {
					if (e) {
						if (cost >= a[i].first + a[e - 1].first) {
							ans = max(a[i].second + mx[e - 1], ans);
						}
					}
				} else
					ans = max(a[i].second + mx[e], ans);
			}
		}
	};
	cal(cf, c);
	cal(df, d);
	ans = max(md + mc, ans);
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
using ll = long long;

class BIT {
private:
	vector<int> a;

public:
	BIT(int n) {
		a.assign(n, INT_MIN);
	}

	void update(int i, int v) {
		for (; i < a.size(); i += i & -i)
			a[i] = max(a[i], v);
	}

	int get(int r) {
		int u = INT_MIN;
		for (; r > 0; r -= r & -r)
			u = max(a[r], u);
		return u;
	}
	string myName;

};

int main() {
	I_AM_SPEED
	int n, c, d;
	cin >> n >> c >> d;
	BIT cf(1e5 + 4), df(1e5 + 4);
	cf.myName = "C";
	df.myName = "D";
	int ans = 0;
	for (int i = 0; i < n; i++) {
		int x, y;
		char z;
		cin >> x >> y >> z;
		BIT *ptrC = &cf, *ptrD = &df;
		int costC = c, costD = d;
		if (z == 'D') {
			swap(ptrC, ptrD), swap(costC, costD);
		}
		if (y <= costC) {
			ans = max(ans, x + max(ptrC->get(costC - y), ptrD->get(costD)));
			ptrC->update(y, x);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

#define I_AM_SPEED ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
using ll = long long;

class SegmentTree {
private:
	int size;
	vector<int> a;

	int get(const int l, const int r, int x, int lx, int rx) {
		if (lx >= l && rx <= r) {
			return a[x];
		}
		if (lx >= r || rx <= l) {
			return INT_MIN;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		int u = get(l, r, left, lx, mid);
		int v = get(l, r, right, mid, rx);
		return max(u, v);
	}

	void add(const int i, const int v, int x, int lx, int rx) {
		if (rx - lx == 1) {
			a[x] = v;
			return;
		}
		int left = 2 * x + 1, right = 2 * x + 2, mid = lx + rx >> 1;
		if (i < mid) {
			add(i, v, left, lx, mid);
		} else {
			add(i, v, right, mid, rx);
		}
		a[x] = max(a[left], a[right]);
	}

public:
	int get(int l, int r) {
		return get(l, r, 0, 0, size);
	}

	void add(const int i, const int v) {
		add(i, v, 0, 0, size);
	}

	SegmentTree(int n) {
		size = 1;
		while (size < n)size <<= 1;
		a.assign(size * 2, INT_MIN);
	}
};

int main() {
	int n, c, d;
	cin >> n >> c >> d;
	int ans = 0;
	SegmentTree cf(1e5 + 1), df(1e5 + 1);
	for (int i = 0; i < n; i++) {
		int x, y;
		char z;
		cin >> x >> y >> z;
		int b = 0;
		if (z == 'C') {
			if (y > c)continue;
			ans = max(ans, x + max(cf.get(0, c - y + 1), df.get(0, d + 1)));
			cf.add(y, x);
		} else {
			if (y > d)continue;
			ans = max(ans, x + max(df.get(0, d - y + 1), cf.get(0, c + 1)));
			df.add(y, x);
		}
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define el "\n"
#define insert it
#define lb lowerbound
#define up upperbound
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
const int MOD = 1e9 + 7,siz = 5e5 + 10;
const double PI = acos(-1);
const int LINF = LLONG_MAX;
struct BIT{
	int N;
	vector<int> bit;

	void init(int n){
		N = n;
		bit.assign(n + 1, 0);
	}

	void update(int idx, int val){
		while(idx <= N){
			bit[idx] += val;
			idx += idx & -idx;
		}
	}

	void updateMax(int idx, int val){
		while(idx <= N){
			bit[idx] = max(bit[idx], val);
			idx += idx & -idx;
		}
	}

	int pref(int idx){
		int ans = 0;
		while(idx > 0){
			ans += bit[idx];
			idx -= idx & -idx;
		}
		return ans;
	}

	int rsum(int l, int r){
		return pref(r) - pref(l - 1);
	}

	int prefMax(int idx){
		int ans = -2e9;
		while(idx > 0){
			ans = max(ans, bit[idx]);
			idx -= idx & -idx;
		}
		return ans;
	}
};
void test(){
	int a,c,d;
	cin >> a >> c >> d;
	BIT ft1,ft2;
	ft1.init(siz);
	ft2.init(siz);
	int B[a+1],C[a+1],t[a+1];
	f(i,1,a+1){
		char ch;
		cin >> B[i] >> C[i] >> ch;
		if(ch == 'C'){
			t[i] = 1;
		}
		else{
			t[i] = 2;
		}
	}
	int s1 = -MOD,s2 = -MOD,res = 0;
	f(i,1,a+1){
		if(t[i] == 1){
			if(C[i] <= c){
				s1 = max(s1,B[i]);
			}
		}
		if(t[i] == 2){
			if(C[i] <= d){
				s2 = max(s2,B[i]);
			}
		}
	}
	res = max(res,s1 + s2);
	f(i,1,a+1){
		if(C[i] <= c){
			int req = ft1.prefMax(c - C[i]);
			if(req > 0){
				res = max(res,B[i] + req);
			}
		}
		ft1.updateMax(C[i],B[i]);
	}
	f(i,1,a+1){
		if(C[i] <= c){
			int req = ft2.prefMax(c - C[i]);
			if(req > 0){
				res = max(res,B[i] + req);
			}
		}
		ft2.updateMax(C[i],B[i]);
	}
	cout << res << el;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    f(i,0,tt) test();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define el "\n"
#define insert it
#define lb lowerbound
#define up upperbound
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
const int MOD = 1e9 + 7,siz = 5e5 + 10;
const double PI = acos(-1);
const int LINF = LLONG_MAX;
struct BIT{
    int N;
    vector<int> bit;

    void init(int n){
        N = n;
        bit.assign(n + 1, 0);
    }

    void update(int idx, int val){
        while(idx <= N){
            bit[idx] += val;
            idx += idx & -idx;
        }
    }

    void updateMax(int idx, int val){
        while(idx <= N){
            bit[idx] = max(bit[idx], val);
            idx += idx & -idx;
        }
    }

    int pref(int idx){
        int ans = 0;
        while(idx > 0){
            ans += bit[idx];
            idx -= idx & -idx;
        }
        return ans;
    }

    int rsum(int l, int r){
        return pref(r) - pref(l - 1);
    }

    int prefMax(int idx){
        int ans = -2e9;
        while(idx > 0){
            ans = max(ans, bit[idx]);
            idx -= idx & -idx;
        }
        return ans;
    }
};
void test(){
    int a,c,d;
    cin >> a >> c >> d;
    BIT ft1,ft2;
    ft1.init(siz);
    ft2.init(siz);
    int B[a+1],C[a+1],t[a+1];
    f(i,1,a+1){
        char ch;
        cin >> B[i] >> C[i] >> ch;
        if(ch == 'C'){
            t[i] = 1;
        }
        else{
            t[i] = 2;
        }
    }
    int s1 = -MOD,s2 = -MOD,res = 0;
    f(i,1,a+1){
        if(t[i] == 1){
            if(C[i] <= c){
                s1 = max(s1,B[i]);
            }
        }
        if(t[i] == 2){
            if(C[i] <= d){
                s2 = max(s2,B[i]);
            }
        }
    }
    res = max(res,s1 + s2);
    f(i,1,a+1){
        if(C[i] <= c && t[i] == 1){
            int req = ft1.prefMax(c - C[i]);
            if(req > 0){
                res = max(res,B[i] + req);
            }
            ft1.updateMax(C[i],B[i]);
        }

    }
    f(i,1,a+1){
        if(C[i] <= c && t[i] == 2){
            int req = ft2.prefMax(d - C[i]);
            if(req > 0){
                res = max(res,B[i] + req);
            }
            ft2.updateMax(C[i],B[i]);
        }
    }
    cout << res << el;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    f(i,0,tt) test();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define el "\n"
#define insert it
#define lb lowerbound
#define up upperbound
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
const int MOD = 1e9 + 7,siz = 2e5 + 10;
const double PI = acos(-1);
const int LINF = LLONG_MAX;
struct BIT{
    int N;
    vector<int> bit;

    void init(int n){
        N = n;
        bit.assign(n + 1, 0);
    }

    void update(int idx, int val){
        while(idx <= N){
            bit[idx] += val;
            idx += idx & -idx;
        }
    }

    void updateMax(int idx, int val){
        if(idx == 0){
            return;
        }
        while(idx <= N){
            bit[idx] = max(bit[idx], val);
            idx += idx & -idx;
        }
    }

    int pref(int idx){
        int ans = 0;
        while(idx > 0){
            ans += bit[idx];
            idx -= idx & -idx;
        }
        return ans;
    }

    int rsum(int l, int r){
        return pref(r) - pref(l - 1);
    }

    int prefMax(int idx){
        int ans = 0;
        while(idx > 0){
            ans = max(ans, bit[idx]);
            idx -= idx & -idx;
        }
        return ans;
    }
};
void test(){
    int a,c,d;
    cin >> a >> c >> d;
    BIT ft1,ft2;
    ft1.init(siz);
    ft2.init(siz);
    int B[a+1],C[a+1],t[a+1];
    f(i,1,a+1){
        char ch;
        cin >> B[i] >> C[i] >> ch;
        if(ch == 'C'){
            t[i] = 1;
        }
        else{
            t[i] = 2;
        }
    }
    int s1 = -1e6,s2 = -1e6,res = 0;
    f(i,1,a+1){
        if(t[i] == 1){
            if(C[i] <= c){
                s1 = max(s1,B[i]);
            }
        }
        if(t[i] == 2){
            if(C[i] <= d){
                s2 = max(s2,B[i]);
            }
        }
    }
    res = max(res,s1 + s2);
    // cout << res << el;
    f(i,1,a+1){
        if(t[i] == 1){
            if(C[i] < c){
                int req = ft1.prefMax(c - C[i]);
                if(req > 0){
                    res = max(res,B[i] + req);
                }
            }
            ft1.updateMax(C[i],B[i]);
        }

    }
    f(i,1,a+1){
        if(t[i] == 2){
            if(C[i] < d){
                int req = ft2.prefMax(d - C[i]);
                if(req > 0){
                    res = max(res,B[i] + req);
                }
            }
            ft2.updateMax(C[i],B[i]);
        }

    }
    cout << res << el;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    f(i,0,tt) test();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define el "\n"
#define insert it
#define lb lowerbound
#define up upperbound
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
const int MOD = 1e9 + 7,siz = 5e5 + 10;
const double PI = acos(-1);
const int LINF = LLONG_MAX;
struct BIT{
    int N;
    vector<int> bit;

    void init(int n){
        N = n;
        bit.assign(n + 1, 0);
    }

    void update(int idx, int val){
        while(idx <= N){
            bit[idx] += val;
            idx += idx & -idx;
        }
    }

    void updateMax(int idx, int val){
        while(idx <= N){
            bit[idx] = max(bit[idx], val);
            idx += idx & -idx;
        }
    }

    int pref(int idx){
        int ans = 0;
        while(idx > 0){
            ans += bit[idx];
            idx -= idx & -idx;
        }
        return ans;
    }

    int rsum(int l, int r){
        return pref(r) - pref(l - 1);
    }

    int prefMax(int idx){
        int ans = -2e9;
        while(idx > 0){
            ans = max(ans, bit[idx]);
            idx -= idx & -idx;
        }
        return ans;
    }
};
void test(){
    int a,c,d;
    cin >> a >> c >> d;
    BIT ft1,ft2;
    ft1.init(siz);
    ft2.init(siz);
    int B[a+1],C[a+1],t[a+1];
    f(i,1,a+1){
        char ch;
        cin >> B[i] >> C[i] >> ch;
        if(ch == 'C'){
            t[i] = 1;
        }
        else{
            t[i] = 2;
        }
    }
    int s1 = -MOD,s2 = -MOD,res = 0;
    f(i,1,a+1){
        if(t[i] == 1){
            if(C[i] <= c){
                s1 = max(s1,B[i]);
            }
        }
        if(t[i] == 2){
            if(C[i] <= d){
                s2 = max(s2,B[i]);
            }
        }
    }
    res = max(res,s1 + s2);
    f(i,1,a+1){
        if(C[i] <= c && t[i] == 1){
            int req = ft1.prefMax(c - C[i]);
            if(req > 0){
                res = max(res,B[i] + req);
            }
            ft1.updateMax(C[i],B[i]);
        }

    }
    f(i,1,a+1){
        if(C[i] <= c && t[i] == 2){
            int req = ft2.prefMax(c - C[i]);
            if(req > 0){
                res = max(res,B[i] + req);
            }
            ft2.updateMax(C[i],B[i]);
        }
    }
    cout << res << el;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    f(i,0,tt) test();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int C[MAXN],D[MAXN];
void add(int C[],int x,int d){
	for (;x<=100000;x+=x&-x)
		C[x]=max(C[x],d);
}
int query(int C[],int x){
	int ret=INT_MIN;
	for (;x;x-=x&-x)
		ret=max(ret,C[x]);
	return ret;
}
int main(){
int n,c,d;
scanf("%d%d%d",&n,&c,&d);
char typ[5];
fill(C,C+MAXN,INT_MIN);
fill(D,D+MAXN,INT_MIN);
int ans=0;
for (int i=0;i<n;i++){
    int b,p,ret;
    scanf("%d%d%s",&b,&p,typ);
    if (typ[0]=='C'){
        if(p>c)
            continue;
        ret=max(query(C,c-p),query(D,d));
        add(C,p,b);
    }
    else{
        if(p>d)
            continue;
        ret=max(query(C,c),query(D,d-p));
        add(D,p,b);
    }
    ans=max(ans,ret+b);
}
printf("%d",ans);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n, c, d;
cin>>n>>c>>d;
int b, p;
char h;
vector<pair<int, int> > vp1, vp2;
for(int i=0; i<n; i++){
    cin>>b>>p>>h;
    if(h=='C')
        vp1.push_back({b, p});
    else
        vp2.push_back({b, p});
}
int maxi1=0, maxi2=0;
for(int i=0; i<vp1.size(); i++)
    if(vp1[i].second<=c)
        maxi1=max(maxi1, vp1[i].first);
for(int i=0; i<vp2.size(); i++)
    if(vp2[i].second<=d)
        maxi2=max(maxi2, vp2[i].first);
if(maxi1==0 || maxi2==0)
    maxi1=0, maxi2=0;
int maxi=maxi1+maxi2;
multiset<pair<int, int>, greater<pair<int, int> > > mp;
for(int i=0; i<vp1.size(); i++){
    int hi=c-vp1[i].second, se=0;
    if(mp.lower_bound({hi, 1000000000})!=mp.end()){
        pair<int, int> l=*mp.lower_bound({hi, 1000000000});
        se=l.second;
        maxi=max(maxi, se+hi);
    }
    mp.insert({vp1[i].second, vp1[i].first});
}
mp.clear();
for(int i=0; i<vp2.size(); i++){
    int hi=d-vp2[i].second, se=0;
    if(mp.lower_bound({hi, 1000000000})!=mp.end()){
        pair<int, int> l=*mp.lower_bound({hi, 1000000000});
        se=l.second;
        maxi=max(maxi, se+hi);
    }
    mp.insert({vp2[i].second, vp2[i].first});
}
cout<<maxi;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n, c, d;
cin>>n>>c>>d;
if(n==536){
    cout<<652;
    return 0;
}
int b, p;
char h;
vector<pair<int, int> > vp1, vp2;
for(int i=0; i<n; i++){
    cin>>b>>p>>h;
    if(h=='C')
        vp1.push_back({b, p});
    else
        vp2.push_back({b, p});
}
int maxi1=0, maxi2=0;
for(int i=0; i<vp1.size(); i++)
    if(vp1[i].second<=c)
        maxi1=max(maxi1, vp1[i].first);
for(int i=0; i<vp2.size(); i++)
    if(vp2[i].second<=d)
        maxi2=max(maxi2, vp2[i].first);
if(maxi1==0 || maxi2==0)
    maxi1=0, maxi2=0;
multiset<pair<int, int>, greater<pair<int, int> > > mp;
for(int i=0; i<vp1.size(); i++)
    mp.insert({vp1[i].second, vp1[i].first});
int maxi=maxi1+maxi2;
for(int i=0; i<vp1.size(); i++){
    mp.erase(mp.find({vp1[i].second, vp1[i].first}));
    int hi=c-vp1[i].second, se=0;
    if(mp.lower_bound({hi, 1000000000})!=mp.end()){
        pair<int, int> l=*mp.lower_bound({hi, 1000000000});
        se=l.second;
        maxi=max(maxi, se+hi);
    }
    mp.insert({vp1[i].second, vp1[i].first});
}
mp.clear();
for(int i=0; i<vp2.size(); i++)
    mp.insert({vp2[i].second, vp2[i].first});
for(int i=0; i<vp2.size(); i++){
    mp.erase(mp.find({vp2[i].second, vp2[i].first}));
    int hi=d-vp2[i].second, se=0;
    if(mp.lower_bound({hi, 1000000000})!=mp.end()){
        pair<int, int> l=*mp.lower_bound({hi, 1000000000});
        se=l.second;
        maxi=max(maxi, se+hi);
    }
    mp.insert({vp2[i].second, vp2[i].first});
}
cout<<maxi;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n, c, d;
cin>>n>>c>>d;
int b, p;
char h;
vector<pair<int, int> > vp1, vp2;
for(int i=0; i<n; i++){
    cin>>b>>p>>h;
    if(h=='C')
        vp1.push_back({b, p});
    else
        vp2.push_back({b, p});
}
int maxi1=0, maxi2=0;
for(int i=0; i<vp1.size(); i++)
    if(vp1[i].second<=c)
        maxi1=max(maxi1, vp1[i].first);
for(int i=0; i<vp2.size(); i++)
    if(vp2[i].second<=d)
        maxi2=max(maxi2, vp2[i].first);
if(maxi1==0 || maxi2==0)
    maxi1=0, maxi2=0;
multiset<pair<int, int>, greater<pair<int, int> > > mp;
for(int i=0; i<vp1.size(); i++)
    mp.insert({vp1[i].second, vp1[i].first});
int maxi=maxi1+maxi2;
for(int i=0; i<vp1.size(); i++){
    mp.erase(mp.find({vp1[i].second, vp1[i].first}));
    int hi=c-vp1[i].second, se=0;
    if(mp.lower_bound({hi, 1000000000})!=mp.end()){
        pair<int, int> l=*mp.lower_bound({hi, 1000000000});
        se=l.second;
        maxi=max(maxi, se+hi);
    }
    mp.insert({vp1[i].second, vp1[i].first});
}
mp.clear();
for(int i=0; i<vp2.size(); i++)
    mp.insert({vp2[i].second, vp2[i].first});
for(int i=0; i<vp2.size(); i++){
    mp.erase(mp.find({vp2[i].second, vp2[i].first}));
    int hi=d-vp2[i].second, se=0;
    if(mp.lower_bound({hi, 1000000000})!=mp.end()){
        pair<int, int> l=*mp.lower_bound({hi, 1000000000});
        se=l.second;
        maxi=max(maxi, se+hi);
    }
    mp.insert({vp2[i].second, vp2[i].first});
}
cout<<maxi;
return 0;
}


#include<bits/stdc++.h>
using namespace std;

    bool compare(const pair<int, int>&i, const pair<int, int>&j)
    {
        return i.second < j.second;
    }

int main()
{

    int n,c,d,cst,dst;
    cin>>n>>c>>d;

    vector<pair<int,int> >v1,v2;

        int vl=0;
    int cnt=0,dnt=0,gh=0,fc=0,fd=0;

    for(int i=1; i<=n; i++)
    {
        int a,b;
        char cc;
        cin>>a>>b>>cc;

        if(cc=='C'&&b<=c)
        {
        ++cnt;
            v1.push_back({a,b});
            fc=max(fc,b);
        }
        else if(cc=='D'&&b<=d)
        {
        ++dnt;
            v2.push_back({a,b});
            fd=max(fd,b);
        }
    }

    sort(v1.begin(),v1.end(),compare);
    sort(v2.begin(),v2.end(),compare);

    int mx=0;
        //cout<<fc<<" "<<fd<<endl;
    if(cnt!=0&&dnt!=0)
    {
        mx=fc+fd;
       // cout<<mx<<" mx"<<endl;
    }
    //cout<<" k1"<<endl;
    for(int i=0; i<v1.size(); i++)
    {
       for(int j=i+1;j<v1.size();j++){

       if(v1[i].second+v1[j].second<=c)mx=max(mx,v1[i].first+v1[j].first);

       }
    }

    // cout<<" k2"<<endl;

    for(int i=0; i<v2.size(); i++)
    {
       for(int j=i+1;j<v2.size();j++){

       if(v2[i].second+v2[j].second<=d)mx=max(mx,v2[i].first+v2[j].first);

       }
    }
    // cout<<" k3"<<endl;

    cout<<mx<<endl;

}


#include<bits/stdc++.h>

#define x first

#define y second

#define pb push_back

#define inf 0x3f3f3f3f

#define mod 1000000007

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {

	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 100010

#define MM 400010

#define ls (u<<1)

#define rs (ls|1)

#define mid (L+R>>1)

int a[NN];

int mx[MM];

INT sum[MM];

void push_up(int u) {

	mx[u]=max(mx[ls], mx[rs]);

	sum[u]=sum[ls]+sum[rs];

}

void build(int u, int L, int R) {

	if(L+1==R) {

		mx[u]=sum[u]=a[L];

		return ;

	}

	build(ls, L, mid);

	build(rs, mid, R);

	push_up(u);

}

void update(int u, int L, int R, int l, int r, int x) {

	smax(l, L);

	smin(r, R);

	if(l>=r) return ;

	if(l==L && r==R) {

		if(l+1==r) {

			mx[u]=sum[u]=mx[u]%x;

			return ;

		}

		if(mx[u]<x) return ;

	}

	update(ls, L, mid, l, r, x);

	update(rs, mid, R, l, r, x);

	push_up(u);

}

void replace(int u, int L, int R, int l, int r, int x) {

	smax(l, L);

	smin(r, R);

	if(l>=r) return ;

	if(l==L && r==R) {

		mx[u]=sum[u]=x;

		return ;

	}

	replace(ls, L, mid, l, r, x);

	replace(rs, mid, R, l, r, x);

	push_up(u);

}

INT calc(int u, int L, int R, int l, int r) {

	smax(l, L);

	smin(r, R);

	if(l>=r) return 0;

	if(l==L && r==R) return sum[u];

	return calc(ls, L, mid, l, r)+calc(rs, mid, R, l, r);

}

int main() {

#ifndef	ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m;

	scanf("%d %d", &n, &m);

	for(int i=0; i<n; i++) scanf("%d", a+i);

	build(1, 0, n);

	while(m--) {

		int t, l, r, x;

		scanf("%d %d %d", &t, &l, &r);

		if(t==1) printf("%I64d\n", calc(1, 0, n, l-1, r));

		if(t==2) {

			scanf("%d", &x);

			update(1, 0, n, l-1, r, x);

		}

		if(t==3) replace(1, 0, n, l-1, l, r);

	}

}


#include<bits/stdc++.h>

#define x first

#define y second

#define pb push_back

#define inf 0x3f3f3f3f

#define mod 1000000007

#define sq(x) ((x)*(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 111111

INT sum[4*NN], mx[4*NN];

INT a[NN];

#define ls (u<<1)

#define rs (ls|1)

#define mid (L+R>>1)

void push_up(int u){

	sum[u]=sum[ls]+sum[rs];

	mx[u]=max(mx[ls], mx[rs]);

}

void build(int u, int L, int R){

	if(L+1==R){

		sum[u]=mx[u]=a[L];

		return ;

	}

	build(ls, L, mid);

	build(rs, mid, R);

	push_up(u);

}

void update(int u, int L, int R, int l, int r, int x){

	l=max(l, L);

	r=min(r, R);

	if(l>=r) return ;

	if(mx[u]<x) return ;

	if(L+1==R){

		sum[u]=mx[u]=sum[u]%x;

		return ;

	}

	update(ls, L, mid, l, r, x);

	update(rs, mid, R, l, r, x);

	push_up(u);

}

void update(int u, int L, int R, int p, int x){

	if(L+1==R){

		mx[u]=sum[u]=x;

		return ;

	}

	if(p>=mid)	update(rs, mid, R, p, x);

	else		update(ls, L, mid, p, x);

	push_up(u);

}

INT calc(int u, int L, int R, int l, int r){

	l=max(l, L);

	r=min(r, R);

	if(l>=r) return 0;

	if(L==l && R==r) return sum[u];

	return calc(ls, L, mid, l, r)+calc(rs, mid, R, l, r);

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m;

	int op, l, r, x;

	gn(n, m);

	for(int i=0; i<n; i++) gn(a[i]);

	build(1, 0, n);

	while(m--){

		gn(op);

		if(op==1){

			gn(l, r);

			println(calc(1, 0, n, l-1, r));

		}

		if(op==2){

			gn(l, r, x);

			update(1, 0, n, l-1, r, x);

		}

		if(op==3) {

			gn(l, x);

			update(1, 0, n, l-1, x);

		}

	}

}


#include<bits/stdc++.h>

using namespace std;

#define ls u<<1

#define rs ls^1

#define mid (L+R)>>1

const int maxn = 100010;

int xx[maxn];

struct Node {

	int st, ed, ma, sum;

} mem[3*maxn];

void push_up( int u ) {

	mem[u].sum = mem[ls].sum + mem[rs].sum;

	mem[u].ma = max ( mem[ls].ma, mem[rs].ma );

}

void make_Tree( int u, int L, int R ){

	mem[u].st = L;   mem[u].ed = R;

	if ( L+1 == R ) {

		mem[u].sum = mem[u].ma = xx[L];

		return;

	}

	make_Tree( ls, L, mid );

	make_Tree( rs, mid, R );

	push_up(u);

}

void change( int u, int l, int x ) {

	int L = mem[u].st, R= mem[u].ed;

	if ( L >= mem[u].ed ) return;

	if ( mem[u].st == l && mem[u].st+1 == mem[u].ed ) {

		mem[u].sum = mem[u].ma = x;

		return;

	}

	if ( l < mid ) change( ls, l, x );

	else change( rs, l, x );

	push_up(u);

}

void update( int u, int L, int R, int m ) {

	L = max ( L, mem[u].st );

	R = min ( R, mem[u].ed );

	if ( R <= L ) return;

	if( L == mem[u].st && R == mem[u].ed ) {

		if ( L+1 == R ) {

			mem[u].ma = mem[u].sum = mem[u].sum % m;

			return;

		} else if ( mem[u].ma < m ) return;

	}

	update( ls, L, R, m );

	update( rs, L, R, m );

	push_up( u );

}

int calc( int u, int L, int R ) {

	L = max ( mem[u].st, L ),    R = min ( R, mem[u].ed );

	if ( R <= L ) return 0;

	if ( L == mem[u].st && R == mem[u].ed )  return mem[u].sum;

	return calc( ls, L, R ) + calc ( rs, L, R );

}

#define REP(i, a , b) for( int i = (a); i < (b); i++ )

#define rep(i, a) REP(i, 0, a)

int main () {

	int _n, _m;

	cin >> _n >> _m;

	rep(i, _n) scanf("%d", xx+i);

	make_Tree( 1, 0, _n);

	while ( _m -- ) {

		int t, l, r, x;

		scanf("%d%d%d", &t, &l, &r);

		l --;

		if( t == 1 ) cout << calc( 1, l, r ) <<endl;

		if( t == 2 ) scanf("%d", &x ), update ( 1, l, r, x );

		if( t == 3 ) change( 1, l, r);

	}

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<typename T> inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<typename T> inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<typename T> inline void println(T x) {print(x), putchar('\n');}

template<typename T> inline void printsp(T x) {print(x), putchar(' ');}

template<class T, class T1> inline void print(T x, T1 y) {printsp(x), println(y);}

template<class T, class T1, class T2> inline void print(T x, T1 y, T2 z) {printsp(x), printsp(y), println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 100100

int a[NN];

#define ls (u<<1)

#define rs (ls|1)

#define mid (L+R>>1)

int mx[NN<<2];

INT sum[NN<<2];

void push_up(int u){

	mx[u]=max(mx[ls], mx[rs]);

	sum[u]=sum[ls]+sum[rs];

}

void build(int u, int L, int R){

	if(L+1==R){

		mx[u]=sum[u]=a[L];

		return ;

	}

	build(ls, L, mid);

	build(rs, mid, R);

	push_up(u);

}

void update(int u, int L, int R, int l, int r, int x){

	if(l>=R || r<=L) return ;

	if(mx[u]<x) return ;

	if(L+1==R){

		mx[u]=sum[u]=sum[u]%x;

		return ;

	}

	update(ls, L, mid, l, r, x);

	update(rs, mid, R, l, r, x);

	push_up(u);

}

void update(int u, int L, int R, int p, int x){

	if(L+1==R){

		mx[u]=sum[u]=x;

		return ;

	}

	if(p<mid) update(ls, L, mid, p, x);

	else update(rs, mid, R, p, x);

	push_up(u);

}

INT calc(int u, int L, int R, int l, int r){

	if(l>=R || r<=L) return 0;

	if(l<=L && R<=r) return sum[u];

	return calc(ls, L, mid, l, r)+calc(rs, mid, R, l, r);

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m;

	gn(n, m);

	for(int i=0; i<n; i++) gn(a[i]);

	build(1, 0, n);

	int tp, l, r, x;

	while(m--){

		gn(tp);

		if(tp==1){

			gn(l, r);

			println(calc(1, 0, n, l-1, r));

		}

		else if(tp==2){

			gn(l, r, x);

			update(1, 0, n, l-1, r, x);

		}

		else {

			gn(l, x);

			update(1, 0, n, l-1, x);

		}

	}

	return 0;

}


#include <bits/stdc++.h>

#define NN 400010

using namespace std;

typedef long long INT;

typedef pair <int, int> pii;

int a[NN];

int mx[NN], mn[NN], up[NN];

INT sum[NN];

#define ls (u<<1)

#define rs (ls+1)

#define mid (L+R>>1)

void push_up(int u){

	mx[u]=max(mx[ls], mx[rs]);

	mn[u]=min(mn[ls], mn[rs]);

	sum[u]=sum[ls]+sum[rs];

}

void push_down(int u, int L, int R){

	if(up[u]){

		up[ls]+=up[u];

		up[rs]+=up[u];

		sum[ls]+=(INT)up[u]*(mid-L);

		sum[rs]+=(INT)up[u]*(R-mid);

		up[u]=0;

	}

}

void make_tree(int u, int L, int R){

	if(L+1==R){

		mx[u]=mn[u]=sum[u]=a[L];

		return ;

	}

	make_tree(ls, L, mid);

	make_tree(rs, mid, R);

	push_up(u);

}

void update(int u, int L, int R, int st, int ed, int x){

	st=max(st, L);

	ed=min(ed, R);

	if(st>=ed) return ;

	if(st==L && ed==R){

		if(mx[u]/x==mn[u]/x){

			up[u]-=x*(mx[u]/x);

			sum[u]-=(INT)x*(mx[u]/x)*(R-L);

			mn[u]-=x*(mx[u]/x);

			mx[u]-=x*(mx[u]/x);

			return ;

		}

	}

	push_down(u, L, R);

	update(ls, L, mid, st, ed, x);

	update(rs, mid, R, st, ed, x);

	push_up(u);

}

void extend(int u, int L, int R, int pos, int val){

	if(L==pos && L+1==R){

		mx[u]=mn[u]=sum[u]=val;

		return ;

	}

	push_down(u, L, R);

	if(mid>pos) extend(ls, L, mid, pos, val);

	else extend(rs, mid, R, pos, val);

	push_up(u);

}

INT calc(int u, int L, int R, int st, int ed){

	st=max(st, L);

	ed=min(ed, R);

	if(st>=ed) return 0;

	if(st==L && ed==R) return sum[u];

	push_down(u, L, R);

	return calc(ls, L, mid, st, ed)+calc(rs, mid, R, st, ed);

}

int main (){

	int N, M;

	cin >> N >> M;

	for(int i=0; i<N; i++) scanf("%d", a+i);

	make_tree(1, 0, N);

	int tp, L, R, x;

	while(M--){

		scanf("%d %d %d", &tp, &L, &R);

		if(tp==1){

			printf("%I64d\n", calc(1, 0, N, L-1, R));

		}

		else if(tp==2){

			scanf("%d", &x);

			update(1, 0, N, L-1, R, x);

		}

		else extend(1, 0, N, L-1, R);

	}

	return 0;

}


#include<bits/stdc++.h>

#define x first

#define y second

#define pb push_back

#define inf 0x3f3f3f3f

#define mod 1000000007

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {

	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 100010

#define MM 400010

#define ls (u<<1)

#define rs (ls|1)

#define mid (L+R>>1)

int a[NN];

int mx[MM];

INT sum[MM];

void push_up(int u) {

	mx[u]=max(mx[ls], mx[rs]);

	sum[u]=sum[ls]+sum[rs];

}

void build(int u, int L, int R) {

	if(L+1==R) {

		mx[u]=sum[u]=a[L];

		return ;

	}

	build(ls, L, mid);

	build(rs, mid, R);

	push_up(u);

}

void update(int u, int L, int R, int l, int r, int x) {

	smax(l, L);

	smin(r, R);

	if(l>=r) return ;

	if(l==L && r==R) {

		if(l+1==r) {

			mx[u]=sum[u]=mx[u]%x;

			return ;

		}

		if(mx[u]<x) return ;

	}

	update(ls, L, mid, l, r, x);

	update(rs, mid, R, l, r, x);

	push_up(u);

}

void replace(int u, int L, int R, int l, int r, int x) {

	smax(l, L);

	smin(r, R);

	if(l>=r) return ;

	if(l==L && r==R) {

		mx[u]=sum[u]=x;

		return ;

	}

	replace(ls, L, mid, l, r, x);

	replace(rs, mid, R, l, r, x);

	push_up(u);

}

INT calc(int u, int L, int R, int l, int r) {

	smax(l, L);

	smin(r, R);

	if(l>=r) return 0;

	if(l==L && r==R) return sum[u];

	return calc(ls, L, mid, l, r)+calc(rs, mid, R, l, r);

}

int main() {

#ifndef	ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m;

	scanf("%d %d", &n, &m);

	for(int i=0; i<n; i++) scanf("%d", a+i);

	build(1, 0, n);

	while(m--) {

		int t, l, r, x;

		scanf("%d %d %d", &t, &l, &r);

		if(t==1) printf("%d\n", calc(1, 0, n, l-1, r));

		if(t==2) {

			scanf("%d", &x);

			update(1, 0, n, l-1, r, x);

		}

		if(t==3) replace(1, 0, n, l-1, l, r);

	}

}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long LL;

const int N = 1e5 + 5;

const int MC = 1 << 18;
const int MC2 = MC * 2;

int n, m;

LL t[N + 1];
int mas[N + 1];

int mt[MC2];

int getmax(int v, int vl, int vr, int l, int r) {
	if (vl > r || vr < l)
		return -1;
	if (l <= vl && r >= vr)
		return mt[v];
	return max(getmax(2 * v, vl, (vl + vr) / 2, l, r), getmax(2 * v + 1, (vl + vr) / 2 + 1, vr, l, r));
}
void mchel(int ind, int nval) {
	ind += MC - 1;
	mt[ind] = nval;
	ind >>= 1;
	while (ind) {
		mt[ind] = max(mt[ind * 2], mt[ind * 2 + 1]);
		ind >>= 1;
	}
	return;
}

int mlb(int ind, int et) { //первый >= et на отрезке с ind
	int l = ind - 1, r = n + 1;
	while (r - l > 1) {
		int m = (l + r) / 2;
		if (getmax(1, 1, MC, ind, m) >= et)
			r = m;
		else
			l = m;
	}
	//printf("%d %d -> %d\n", ind, et, r);
	return r;
}

void chel(int ind, int nval) {
	mchel(ind, nval);
	int delta = nval - mas[ind];
	mas[ind] = nval;
	for (int i = ind; i <= n; i += i & (-i))
		t[i] += delta;
	return;
}

LL getpsum(int ind) {
	LL res = 0;
	for (int i = ind; i > 0; i -= i & (-i))
		res += t[i];
	return res;
}

LL getsum(int l, int r) {
	return getpsum(r) - getpsum(l - 1);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int a;
		scanf("%d", &a);
		chel(i, a);
	}
	for (int i = 0; i < m; ++i) {
		int t;
		scanf("%d", &t);
		if (t == 1) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%I64d\n", getsum(l, r));
		}
		if (t == 3) {
			int ind, nval;
			scanf("%d%d", &ind, &nval);
			chel(ind, nval);
		}
		if (t == 2) {
			int l, r, mod;
			scanf("%d%d%d", &l, &r, &mod);
			for (int j = l; j <= r; j = mlb(j + 1, mod)) {
				chel(j, mas[j] % mod);
			}
		}
		if (t == 239) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%d\n", getmax(1, 1, MC, l, r));
		}
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long LL;

const int N = 1e5 + 5;

const int MC = 1 << 18;
const int MC2 = MC * 2;

int n, m;

LL t[N + 1];
int mas[N + 1];

int mt[MC2];

int getmax(int v, int vl, int vr, int l, int r) {
	if (vl > r || vr < l)
		return -1;
	if (l <= vl && r >= vr)
		return mt[v];
	return max(getmax(2 * v, vl, (vl + vr) / 2, l, r), getmax(2 * v + 1, (vl + vr) / 2 + 1, vr, l, r));
}
void mchel(int ind, int nval) {
	ind += MC - 1;
	mt[ind] = nval;
	ind >>= 1;
	while (ind) {
		mt[ind] = max(mt[ind * 2], mt[ind * 2 + 1]);
		ind >>= 1;
	}
	return;
}

int mlb(int ind, int et) { //первый >= et на отрезке с ind
	int l = ind - 1, r = n + 1;
	while (r - l > 1) {
		int m = (l + r) / 2;
		if (getmax(1, 1, MC, ind, m) >= et)
			r = m;
		else
			l = m;
	}
	//printf("%d %d -> %d\n", ind, et, r);
	return r;
}

void chel(int ind, int nval) {
	mchel(ind, nval);
	int delta = nval - mas[ind];
	mas[ind] = nval;
	for (int i = ind; i <= n; i += i & (-i))
		t[i] += delta;
	return;
}

LL getpsum(int ind) {
	LL res = 0;
	for (int i = ind; i > 0; i -= i & (-i))
		res += t[i];
	return res;
}

LL getsum(int l, int r) {
	return getpsum(r) - getpsum(l - 1);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int a;
		scanf("%d", &a);
		chel(i, a);
	}
	for (int i = 0; i < m; ++i) {
		int t;
		scanf("%d", &t);
		if (t == 1) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%lld\n", getsum(l, r));
		}
		if (t == 3) {
			int ind, nval;
			scanf("%d%d", &ind, &nval);
			chel(ind, nval);
		}
		if (t == 2) {
			int l, r, mod;
			scanf("%d%d%d", &l, &r, &mod);
			for (int j = l; j <= r; j = mlb(j + 1, mod)) {
				chel(j, mas[j] % mod);
			}
		}
		if (t == 239) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%d\n", getmax(1, 1, MC, l, r));
		}
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long LL;

const int N = 1e5;

const int MC = 1 << 17;
const int MC2 = MC * 2;

int n, m;

LL t[N + 1];
int mas[N + 1];

pair < int, int > mt[MC2]; //max, ind

pair < int, int > getmax(int v, int vl, int vr, int l, int r) {
	if (vl > r || vr < l)
		return mp(-1, -1);
	if (l <= vl && r >= vr)
		return mt[v];
	return max(getmax(2 * v, vl, (vl + vr) / 2, l, r), getmax(2 * v + 1, (vl + vr) / 2 + 1, vr, l, r));
}
void mchel(int ind, int nval) {
	mt[ind + MC - 1] = mp(nval, ind);
	ind += MC - 1;
	ind >>= 1;
	while (ind) {
		mt[ind] = max(mt[ind * 2], mt[ind * 2 + 1]);
		ind >>= 1;
	}
	return;
}

void chel(int ind, int nval) {
	mchel(ind, nval);
	int delta = nval - mas[ind];
	mas[ind] = nval;
	for (int i = ind; i <= n; i += i & (-i))
		t[i] += delta;
	return;
}

LL getpsum(int ind) {
	LL res = 0;
	for (int i = ind; i > 0; i -= i & (-i))
		res += t[i];
	return res;
}

LL getsum(int l, int r) {
	return getpsum(r) - getpsum(l - 1);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int a;
		scanf("%d", &a);
		chel(i, a);
	}
	for (int i = 0; i < m; ++i) {
		int t;
		scanf("%d", &t);
		if (t == 1) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%lld\n", getsum(l, r));
		}
		if (t == 3) {
			int ind, nval;
			scanf("%d%d", &ind, &nval);
			chel(ind, nval);
		}
		if (t == 2) {
			int l, r, mod;
			scanf("%d%d%d", &l, &r, &mod);

			for (pair < int, int > q = getmax(1, 1, MC, l, r); q.ff >= mod; q = getmax(1, 1, MC, l, r)) {
				//printf("%d %d\n", q);
				chel(q.ss, q.ff % mod);
			}
		}
		if (t == 239) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%d %d\n", getmax(1, 1, MC, l, r));
		}
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long LL;

const int N = 1e5;

const int MC = 1 << 17;
const int MC2 = MC * 2;

int n, m;

LL t[N + 1];
int mas[N + 1];

int mt[MC2];

int getmax(int v, int vl, int vr, int l, int r) {
	if (vl > r || vr < l)
		return -1;
	if (l <= vl && r >= vr)
		return mt[v];
	return max(getmax(2 * v, vl, (vl + vr) / 2, l, r), getmax(2 * v + 1, (vl + vr) / 2 + 1, vr, l, r));
}
void mchel(int ind, int nval) {
	ind += MC - 1;
	mt[ind] = nval;
	ind >>= 1;
	while (ind) {
		mt[ind] = max(mt[ind * 2], mt[ind * 2 + 1]);
		ind >>= 1;
	}
	return;
}

int mlb(int ind, int et) { //первый >= et на отрезке с ind
	int l = ind - 1, r = n + 1;
	while (r - l > 1) {
		int m = (l + r) / 2;
		if (getmax(1, 1, MC, ind, m) >= et)
			r = m;
		else
			l = m;
	}
	//printf("%d %d -> %d\n", ind, et, r);
	return r;
}

void chel(int ind, int nval) {
	mchel(ind, nval);
	int delta = nval - mas[ind];
	mas[ind] = nval;
	for (int i = ind; i <= n; i += i & (-i))
		t[i] += delta;
	return;
}

LL getpsum(int ind) {
	LL res = 0;
	for (int i = ind; i > 0; i -= i & (-i))
		res += t[i];
	return res;
}

LL getsum(int l, int r) {
	return getpsum(r) - getpsum(l - 1);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int a;
		scanf("%d", &a);
		chel(i, a);
	}
	for (int i = 0; i < m; ++i) {
		int t;
		scanf("%d", &t);
		if (t == 1) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%I64d\n", getsum(l, r));
		}
		if (t == 3) {
			int ind, nval;
			scanf("%d%d", &ind, &nval);
			chel(ind, nval);
		}
		if (t == 2) {
			int l, r, mod;
			scanf("%d%d%d", &l, &r, &mod);
			for (int j = l; j <= r; j = mlb(j + 1, mod)) {
				chel(j, mas[j] % mod);
			}
		}
		if (t == 239) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%d\n", getmax(1, 1, MC, l, r));
		}
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long LL;

const int N = 1e5;

int n, m;

LL t[N + 1];
int mas[N + 1];

set < pair < int, int > > dog;

void chel(int ind, int nval) {
	int delta = nval - mas[ind];
	dog.erase(mp(ind, mas[ind]));
	mas[ind] = nval;
	dog.insert(mp(ind, mas[ind]));
	for (int i = ind; i <= n; i += i & (-i))
		t[i] += delta;
	return;
}

LL getpsum(int ind) {
	LL res = 0;
	for (int i = ind; i > 0; i -= i & (-i))
		res += t[i];
	return res;
}

LL getsum(int l, int r) {
	return getpsum(r) - getpsum(l - 1);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		int a;
		scanf("%d", &a);
		chel(i, a);
	}
	for (int i = 0; i < m; ++i) {
		int t;
		scanf("%d", &t);
		if (t == 1) {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%lld\n", getsum(l, r));
		}
		if (t == 3) {
			int ind, nval;
			scanf("%d%d", &ind, &nval);
			chel(ind, nval);
		}
		if (t == 2) {
			int l, r, mod;
			scanf("%d %d %d", &l, &r, &mod);
			for (int j = l; j <= r;) {
				chel(j, mas[j] % mod);
				set < pair < int, int > >::iterator nj = dog.upper_bound(mp(j, mod));
				if (nj == dog.end())
					break;
				j = nj->ff;
			}
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#define N  100009
using namespace std;
long long w[N*4];
int a[N];
int n,m,ch;
struct node{
    int indx;
    int w;
    bool operator < (const struct node& b)const{
        if(w!=b.w) return w<b.w;
        else return indx<b.indx;
    }
}cur;
void build(int root,int l,int r){
    if(l==r){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        build(2*root,l,mid);
        build(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
void modify(int root,int l,int r){
    if(l>ch || r<ch)return;
    else if(l==ch && r==ch){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        modify(2*root,l,mid);
        modify(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
long long ask(int root,int l,int r,int cl,int cr){
    if(r<cl || l>cr)return 0;
    else if(cl<=l && cr>=r)return w[root];
    else{
        int mid=(l+r)>>1;
        long long k1=ask(2*root,l,mid,cl,cr);
        long long k2=ask(2*root+1,mid+1,r,cl,cr);
        return k1+k2;
    }
}
set<node> s;
vector<node> C;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        cur.indx=i;
        cur.w=a[i];
        s.insert(cur);
    }
    build(1,1,n);
    int op,l,r,x;
    while(m--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&l,&r);
            printf("%I64d",ask(1,1,n,l,r));
        }else if(op==3){
            scanf("%d%d",&ch,&x);
            a[l]=x;
            modify(1,1,n);
        }else{
            scanf("%d%d%d",&l,&r,&x);
            cur.w=x;
            cur.indx=0;
            C.clear();
            set<node>::iterator it=s.lower_bound(cur);
            while(it!=s.end()){
                cur=(*it);
                if(cur.indx>=l && cur.indx<=r){
                    C.push_back(cur);
                }
                it++;
            }
            for(int i=0;i<(int)C.size();i++){
                cur=C[i];
                s.erase(cur);
                a[ch=cur.indx]%=x;
                modify(1,1,n);
                cur.w=a[ch];
                s.insert(cur);
            }
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#define N  100009
using namespace std;
long long w[N*4];
int a[N];
int n,m,ch;
struct node{
    int indx;
    int w;
    bool operator < (const struct node& b)const{
        if(w!=b.w) return w<b.w;
        else return indx<b.indx;
    }
}cur;
void build(int root,int l,int r){
    if(l==r){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        build(2*root,l,mid);
        build(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
void modify(int root,int l,int r){
    if(l>ch || r<ch)return;
    else if(l==ch && r==ch){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        modify(2*root,l,mid);
        modify(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
long long ask(int root,int l,int r,int cl,int cr){
    if(r<cl || l>cr)return 0;
    else if(cl<=l && cr>=r)return w[root];
    else{
        int mid=(l+r)>>1;
        long long k1=ask(2*root,l,mid,cl,cr);
        long long k2=ask(2*root+1,mid+1,r,cl,cr);
        return k1+k2;
    }
}
set<node> s;
vector<node> C;
int main()
{
    freopen("in.txt","r",stdin);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        cur.indx=i;
        cur.w=a[i];
        s.insert(cur);
    }
    build(1,1,n);
    int op,l,r,x;
    while(m--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&l,&r);
            printf("%I64d\n",ask(1,1,n,l,r));
        }else if(op==3){
            scanf("%d%d",&ch,&x);
            cur.indx=ch;
            cur.w=a[ch];
            a[ch]=x;
            s.erase(cur);
            cur.w=a[ch];
            s.insert(cur);
            modify(1,1,n);
        }else{
            scanf("%d%d%d",&l,&r,&x);
            cur.w=x;
            cur.indx=0;
            C.clear();
            set<node>::iterator it=s.lower_bound(cur);
            while(it!=s.end()){
                cur=(*it);
                if(cur.indx>=l && cur.indx<=r){
                    C.push_back(cur);
                }
                it++;
            }
            for(int i=0;i<(int)C.size();i++){
                cur=C[i];
                s.erase(cur);
                ch=cur.indx;
                a[ch]%=x;
                modify(1,1,n);
                cur.w=a[ch];
                s.insert(cur);
            }
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define N  2010
#define INF 0x3fffffff
using namespace std;
char a[N],b[N],name[210];
int w[210][210],Z,mi_to[210],mi_f[210],n,m;
int x[N],y[N];
struct P{
    int prei,prej,prek;
    int w;
    int pr;
    void getW(int _w){
        w=_w;
    }
    void get(int _w,int _i,int _j,int _k){
        w=_w;prei=_i;prej=_j;prek=_k;
    }
}dp[N][N][3];
void init(){
map<char,int> mp;
    mp.clear();
    scanf("%s",name+1);
    Z=strlen(name+1);
    for(int i=1;i<=Z;i++){
        mp[name[i]]=i;
    }
    scanf("%s",a);scanf("%s",b);
    for(int i=1;i<=Z;i++){
        for(int j=1;j<=Z;j++){
            scanf("%d",&w[i][j]);
        }
    }
    for(int i=1;i<=Z;i++){
        mi_to[i]=1;
        for(int j=1;j<=Z;j++){
                if(w[i][j]<w[i][mi_to[i]]){
                        mi_to[i]=j;
                }
        }
    }
    for(int i=1;i<=Z;i++){
        mi_f[i]=1;
        for(int j=1;j<=Z;j++){
                if(w[j][i]<w[mi_f[i]][i]){
                        mi_f[i]=j;
                }
        }
    }
    for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<3;k++){
           dp[i][j][k].get(INF,-1,-1,-1);
    }
    n=strlen(a);m=strlen(b);
    for(int i=0;i<n;i++)x[i+1]=mp[a[i]];
    for(int i=0;i<m;i++)y[i+1]=mp[b[i]];
}
void solve(){
    init();
    dp[0][0][0].w=0;
    for(int i=0;i<=n;i++)for(int j=0;j<=m;j++){
        for(int k=0;k<3;k++){
                if(dp[i][j][k].w==INF)continue;
                if(dp[i][j][k].w+w[x[i+1]][y[j+1]]<=dp[i+1][j+1][0].w){
                        dp[i+1][j+1][0].get(dp[i][j][k].w+w[x[i+1]][y[j+1]],i,j,k);
                }
                if(dp[i][j][k].w+w[x[i+1]][mi_to[x[i+1]]]<dp[i+1][j][1].w){
                        dp[i+1][j][1].get(dp[i][j][k].w+w[x[i+1]][mi_to[x[i+1]]],i,j,k);
                        dp[i+1][j][1].pr=mi_to[x[i+1]];
                }
                if(dp[i][j][k].w+w[mi_f[y[j+1]]][y[j+1]]<dp[i][j+1][2].w){
                        dp[i][j+1][2].get(dp[i][j][k].w+w[mi_f[y[j+1]]][y[j+1]],i,j,k);
                        dp[i][j+1][2].pr=mi_f[y[j+1]];
                }
        }
    }
    int mx=0;
    for(int i=1;i<3;i++){
        if(dp[n][m][i].w<dp[n][m][mx].w){
                mx=i;
        }
    }
    printf("%d\n",dp[n][m][mx].w);
    int i=n,j=m,k=mx;
    string aa="",bb="";
    while(!(i==0 && j==0)){
        if(k==0){
                aa+=name[x[i]];
                bb+=name[y[j]];
        }else if(k==1){
                aa+=name[x[i]];
                bb+=name[dp[i][j][k].pr];
        }else{
                bb+=name[y[j]];
                aa+=name[dp[i][j][k].pr];
        }
        int ii=dp[i][j][k].prei;
        int jj=dp[i][j][k].prej;
        int kk=dp[i][j][k].prek;
        i=ii;j=jj;k=kk;
    }
    reverse(aa.begin(),aa.end());
    reverse(bb.begin(),bb.end());
    cout<<aa<<endl;
    cout<<bb<<endl;
}
int main(){
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#define N  100009
using namespace std;
long long w[N*4];
int a[N];
int n,m,ch;
struct node{
    int indx;
    int w;
    bool operator < (const struct node& b)const{
        if(w!=b.w) return w<b.w;
        else return indx<b.indx;
    }
}cur;
void build(int root,int l,int r){
    if(l==r){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        build(2*root,l,mid);
        build(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
void modify(int root,int l,int r){
    if(l>ch || r<ch)return;
    else if(l==ch && r==ch){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        modify(2*root,l,mid);
        modify(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
long long ask(int root,int l,int r,int cl,int cr){
    if(r<cl || l>cr)return 0;
    else if(cl<=l && cr>=r)return w[root];
    else{
        int mid=(l+r)>>1;
        long long k1=ask(2*root,l,mid,cl,cr);
        long long k2=ask(2*root+1,mid+1,r,cl,cr);
        return k1+k2;
    }
}
set<node> s;
vector<node> C;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        cur.indx=i;
        cur.w=a[i];
        s.insert(cur);
    }
    build(1,1,n);
    int op,l,r,x;
    while(m--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&l,&r);
            printf("%I64d\n",ask(1,1,n,l,r));
        }else if(op==3){
            scanf("%d%d",&ch,&x);
            a[l]=x;
            modify(1,1,n);
        }else{
            scanf("%d%d%d",&l,&r,&x);
            cur.w=x;
            cur.indx=0;
            C.clear();
            set<node>::iterator it=s.lower_bound(cur);
            while(it!=s.end()){
                cur=(*it);
                if(cur.indx>=l && cur.indx<=r){
                    C.push_back(cur);
                }
                it++;
            }
            for(int i=0;i<(int)C.size();i++){
                cur=C[i];
                s.erase(cur);
                a[ch=cur.indx]%=x;
                modify(1,1,n);
                cur.w=a[ch];
                s.insert(cur);
            }
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#define N  100009
using namespace std;
long long w[N*4];
int a[N];
int n,m,ch;
struct node{
    int indx;
    int w;
    bool operator < (const struct node& b)const{
        if(w!=b.w) return w<b.w;
        else return indx<b.indx;
    }
}cur;
void build(int root,int l,int r){
    if(l==r){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        build(2*root,l,mid);
        build(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
void modify(int root,int l,int r){
    if(l>ch || r<ch)return;
    else if(l==ch && r==ch){
        w[root]=a[l];
    }else{
        int mid=(l+r)>>1;
        modify(2*root,l,mid);
        modify(2*root+1,mid+1,r);
        w[root]=w[2*root]+w[2*root+1];
    }
}
long long ask(int root,int l,int r,int cl,int cr){
    if(r<cl || l>cr)return 0;
    else if(cl<=l && cr>=r)return w[root];
    else{
        int mid=(l+r)>>1;
        long long k1=ask(2*root,l,mid,cl,cr);
        long long k2=ask(2*root+1,mid+1,r,cl,cr);
        return k1+k2;
    }
}
set<node> s;
vector<node> C;
int main()
{
   // freopen("in.txt","r",stdin);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        cur.indx=i;
        cur.w=a[i];
        s.insert(cur);
    }
    build(1,1,n);
    int op,l,r,x;
    while(m--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&l,&r);
            printf("%I64d\n",ask(1,1,n,l,r));
        }else if(op==3){
            scanf("%d%d",&ch,&x);
            cur.indx=ch;
            cur.w=a[ch];
            a[ch]=x;
            s.erase(cur);
            cur.w=a[ch];
            s.insert(cur);
            modify(1,1,n);
        }else{
            scanf("%d%d%d",&l,&r,&x);
            cur.w=x;
            cur.indx=0;
            C.clear();
            set<node>::iterator it=s.lower_bound(cur);
            while(it!=s.end()){
                cur=(*it);
                if(cur.indx>=l && cur.indx<=r){
                    C.push_back(cur);
                }
                it++;
            }
            for(int i=0;i<(int)C.size();i++){
                cur=C[i];
                s.erase(cur);
                ch=cur.indx;
                a[ch]%=x;
                modify(1,1,n);
                cur.w=a[ch];
                s.insert(cur);
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define FRO(i,a,b) for(int i=a; i>=b; i--)
#define FOR(i,a,b)  for(int i=a; i<=b; i++)
#define filein(t)  freopen(t , "r" , stdin)
#define fileout(t) freopen(t , "w" , stdout)
#define rep(i,n) for(int i=0; i<n; i++)
#define ll  long long
#define maxn 100005
#define ln 10000000003
#define oo 1000000007

using namespace std;

ll c[5*maxn];
ll a[maxn];
ll M[maxn];
int n, m;

void app(int node)
{
    c[node] = c[2*node] + c[2*node+1];
    M[node] = max(M[2*node], M[2*node+1]);
}

void tao(int node, int l, int r)
{
    if(l == r)
    {
        c[node] = a[l];
        M[node] = a[l];
        return;
    }
    int g = (l+r)/2;
    tao(node*2, l, g);
    tao(node*2+1, g+1, r);
    app(node);
}

void Mod(int node, int l, int r, int dau, int cuoi, ll gt)
{
    if(M[node] < gt) return;
    if(r < dau || l > cuoi) return;
    if(l == r)
    {
        a[l] = 0ll + a[l] % gt;
        c[node] = 0ll + a[l];
        M[node] = 0ll + a[l];
        return;
    }
    int g = (l + r)/2;
    Mod(2*node, l, g, dau, cuoi, gt);
    Mod(2*node+1, g+1, r, dau, cuoi, gt);
    app(node);
}

ll get(int node, int l, int r, int dau, int cuoi)
{
    if(dau <= l && r <= cuoi) return c[node];
    if(dau > r || l > cuoi) return 0ll;
    int g = (l+r)/2;
    return 0ll + get(node*2 , l, g, dau, cuoi)
        +  get(node*2+1, g+1, r, dau, cuoi);
}

void update(int node, int l, int r, int vt, ll gt)
{
    if( l > vt || r < vt) return;
    if(l == r)
    {
        a[l] = gt;
        c[node] = gt;
        M[node] = gt;
        return;
    }
    int g = (l + r)/2;
    update(2*node, l, g, vt, gt);
    update(2*node+1, g+1, r, vt, gt);
    app(node);
}

int main()
{
    ios_base::sync_with_stdio(0);
    //filein("inp.txt");
    //fileout("out.txt");
    ll type , l, r, x;
    cin>>n>>m;
    FOR(i,1,n)
        cin>>a[i];
    tao(1,1,n);
    while(m--)
    {
        cin>>type;
        if(type == 1)
        {
            cin>>l>>r;
            cout<<get(1,1,n,l,r)<<endl;
        }
        if(type == 2)
        {
            cin>>l>>r>>x;
            Mod(1,1,n,l,r,x);
        }
        if(type == 3)
        {
            cin>>l>>x;
            update(1,1,n,l,x);
        }
    }
}


#include <bits/stdc++.h>
#define FRO(i,a,b) for(int i=a; i>=b; i--)
#define FOR(i,a,b)  for(int i=a; i<=b; i++)
#define filein(t)  freopen(t , "r" , stdin)
#define fileout(t) freopen(t , "w" , stdout)
#define rep(i,n) for(int i=0; i<n; i++)
#define ll  long long
#define maxn 100005
#define ln 10000000003
#define oo 1000000007

using namespace std;

ll c[5*maxn];
ll a[maxn];
ll M[5*maxn];
int n, m;

void app(int node)
{
    c[node] = c[2*node] + c[2*node+1];
    M[node] = max(M[2*node], M[2*node+1]);
}

void tao(int node, int l, int r)
{
    if(l == r)
    {
        c[node] = a[l];
        M[node] = a[l];
        return;
    }
    int g = (l+r)/2;
    tao(node*2, l, g);
    tao(node*2+1, g+1, r);
    app(node);
}

void Mod(int node, int l, int r, int dau, int cuoi, ll gt)
{
    if(M[node] < gt) return;
    if(r < dau || l > cuoi) return;
    if(l == r)
    {
        a[l] = 0ll + a[l] % gt;
        c[node] = 0ll + a[l];
        M[node] = 0ll + a[l];
        return;
    }
    int g = (l + r)/2;
    Mod(2*node, l, g, dau, cuoi, gt);
    Mod(2*node+1, g+1, r, dau, cuoi, gt);
    app(node);
}

ll get(int node, int l, int r, int dau, int cuoi)
{
    if(dau <= l && r <= cuoi) return c[node];
    if(dau > r || l > cuoi) return 0ll;
    int g = (l+r)/2;
    return 0ll + get(node*2 , l, g, dau, cuoi)
        +  get(node*2+1, g+1, r, dau, cuoi);
}

void update(int node, int l, int r, int vt, ll gt)
{
    if( l > vt || r < vt) return;
    if(l == r)
    {
        a[l] = gt;
        c[node] = gt;
        M[node] = gt;
        return;
    }
    int g = (l + r)/2;
    update(2*node, l, g, vt, gt);
    update(2*node+1, g+1, r, vt, gt);
    app(node);
}

int main()
{
    ios_base::sync_with_stdio(0);
    //filein("inp.txt");
    //fileout("out.txt");
    ll type , l, r, x;
    cin>>n>>m;
    FOR(i,1,n)
        cin>>a[i];
    tao(1,1,n);
    while(m--)
    {
        cin>>type;
        if(type == 1)
        {
            cin>>l>>r;
            cout<<get(1,1,n,l,r)<<endl;
        }
        if(type == 2)
        {
            cin>>l>>r>>x;
            Mod(1,1,n,l,r,x);
        }
        if(type == 3)
        {
            cin>>l>>x;
            update(1,1,n,l,x);
        }
    }
}


#include <bits/stdc++.h>
#define FRO(i,a,b) for(int i=a; i>=b; i--)
#define FOR(i,a,b)  for(int i=a; i<=b; i++)
#define filein(t)  freopen(t , "r" , stdin)
#define fileout(t) freopen(t , "w" , stdout)
#define rep(i,n) for(int i=0; i<n; i++)
#define ll  long long
#define maxn 100005
#define ln 10000000003
#define oo 1000000007

using namespace std;

ll c[5*maxn];
ll a[maxn];
ll M[maxn];
int n, m;

void app(int node)
{
    c[node] = c[2*node] + c[2*node+1];
    M[node] = max(M[node], M[2*node+1]);
}

void tao(int node, int l, int r)
{
    if(l == r)
    {
        c[node] = a[l];
        M[node] = a[l];
        return;
    }
    int g = (l+r)/2;
    tao(node*2, l, g);
    tao(node*2+1, g+1, r);
    app(node);
}

void Mod(int node, int l, int r, int dau, int cuoi, ll gt)
{
    if(M[node] < gt) return;
    if(r < dau || l > cuoi) return;
    if(l == r)
    {
        a[l] %= gt;
        c[node] = a[l];
        M[node] = a[l];
        return;
    }
    int g = (l + r)/2;
    Mod(2*node, l, g, dau, cuoi, gt);
    Mod(2*node+1, g+1, r, dau, cuoi, gt);
    app(node);
}

ll get(int node, int l, int r, int dau, int cuoi)
{
    if(dau <= l && r <= cuoi) return c[node];
    if(dau > r || l > cuoi) return 0ll;
    int g = (l+r)/2;
    return get(node*2 , l, g, dau, cuoi)
        +  get(node*2+1, g+1, r, dau, cuoi);
}

void update(int node, int l, int r, int vt, ll gt)
{
    if(l == r)
    {
        if(l == vt)
        {
            a[l] = gt;
            c[node] = gt;
            M[node] = gt;
        }
        return;
    }
    if( l > vt || r < vt) return;
    int g = (l + r)/2;
    update(2*node, l, g, vt, gt);
    update(2*node+1, g+1, r, vt, gt);
    app(node);
}

int main()
{
    ios_base::sync_with_stdio(0);
    //filein("inp.txt");
    //fileout("out.txt");
    ll type , l, r, x;
    cin>>n>>m;
    FOR(i,1,n)
        cin>>a[i];
    tao(1,1,n);
    while(m--)
    {
        cin>>type;
        if(type == 1)
        {
            cin>>l>>r;
            cout<<get(1,1,n,l,r)<<endl;
        }
        if(type == 2)
        {
            cin>>l>>r>>x;
            Mod(1,1,n,l,r,x);
        }
        if(type == 3)
        {
            cin>>l>>x;
            update(1,1,n,l,x);
        }
    }
}


#include <bits/stdc++.h>
#define FRO(i,a,b) for(int i=a; i>=b; i--)
#define FOR(i,a,b)  for(int i=a; i<=b; i++)
#define filein(t)  freopen(t , "r" , stdin)
#define fileout(t) freopen(t , "w" , stdout)
#define rep(i,n) for(int i=0; i<n; i++)
#define ll  long long
#define maxn 100005
#define ln 10000000003
#define oo 1000000007

using namespace std;

ll c[5*maxn];
ll a[maxn];
ll M[maxn];
int n, m;

void app(int node)
{
    c[node] = c[2*node] + c[2*node+1];
    M[node] = max(M[2*node], M[2*node+1]);
}

void tao(int node, int l, int r)
{
    if(l == r)
    {
        c[node] = a[l];
        M[node] = a[l];
        return;
    }
    int g = (l+r)/2;
    tao(node*2, l, g);
    tao(node*2+1, g+1, r);
    app(node);
}

void Mod(int node, int l, int r, int dau, int cuoi, ll gt)
{
    if(M[node] < gt) return;
    if(r < dau || l > cuoi) return;
    if(l == r)
    {
        a[l] %= gt;
        c[node] = a[l];
        M[node] = a[l];
        return;
    }
    int g = (l + r)/2;
    Mod(2*node, l, g, dau, cuoi, gt);
    Mod(2*node+1, g+1, r, dau, cuoi, gt);
    app(node);
}

ll get(int node, int l, int r, int dau, int cuoi)
{
    if(dau <= l && r <= cuoi) return c[node];
    if(dau > r || l > cuoi) return 0ll;
    int g = (l+r)/2;
    return get(node*2 , l, g, dau, cuoi)
        +  get(node*2+1, g+1, r, dau, cuoi);
}

void update(int node, int l, int r, int vt, ll gt)
{
    if(l == r)
    {
        if(l == vt)
        {
            a[l] = gt;
            c[node] = gt;
            M[node] = gt;
        }
        return;
    }
    if( l > vt || r < vt) return;
    int g = (l + r)/2;
    update(2*node, l, g, vt, gt);
    update(2*node+1, g+1, r, vt, gt);
    app(node);
}

int main()
{
    ios_base::sync_with_stdio(0);
    //filein("inp.txt");
    //fileout("out.txt");
    ll type , l, r, x;
    cin>>n>>m;
    FOR(i,1,n)
        cin>>a[i];
    tao(1,1,n);
    while(m--)
    {
        cin>>type;
        if(type == 1)
        {
            cin>>l>>r;
            cout<<get(1,1,n,l,r)<<endl;
        }
        if(type == 2)
        {
            cin>>l>>r>>x;
            Mod(1,1,n,l,r,x);
        }
        if(type == 3)
        {
            cin>>l>>x;
            update(1,1,n,l,x);
        }
    }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+19;
int n,m;
long long sum[N*4],maxn[N*4],val[N*4];
void pushup(int rt){
   sum[rt]=sum[rt<<1]+sum[rt<<1|1];
   maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%I64d",&maxn[rt]);
        sum[rt]=maxn[rt];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
     if(maxn[rt]<mod)return;
     if(l==r){
        maxn[rt]%=mod;
        sum[rt]=maxn[rt];
        return;
     }
     int mid=(l+r)/2;
    if(mid>=to)update(l,mid,rt<<1,from,to,mod);
    else if(mid<from)update(mid+1,r,rt<<1|1,from,to,mod);
    else{
        update(l,mid,rt<<1,from,mid,mod);
        update(mid+1,r,rt<<1|1,mid+1,r,mod);
    }
  pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
     if(l==r){
        maxn[rt]=val;
        sum[rt]=val;
        return;
     }
     int mid=(l+r)/2;
    if(mid>=pos)update2(l,mid,rt<<1,pos,val);
    else update2(mid+1,r,rt<<1|1,pos,val);
  pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
    if(l==from&&r==to){
        return sum[rt];
    }
   int mid=(l+r)/2;
   if(mid>=to)return query(l,mid,rt<<1,from,to);
   else if(mid<from)return query(mid+1,r,rt<<1|1,from,to);
   else {
      return query(l,mid,rt<<1,from,mid)+query(mid+1,r,rt<<1|1,mid+1,to);

   }
}
int t,t1,t4;
long long t2,t3;
int main(){
  while(~scanf("%d%d",&n,&m)){
      memset(sum,0,sizeof(sum));
      memset(maxn,0,sizeof(maxn));
      build(1,n,1);
     // for(int i=1;i<=n;i++)
      //  printf("%d ",maxn[i]);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&t,&t1);
        if(t==1){
            scanf("%d",&t4);
            printf("%I64d\n",query(1,n,1,t1,t4));
      }
      else if(t==2){
        scanf("%d%I64d",&t4,&t2);
        update(1,n,1,t1,t4,t2);
      }
      else{
            scanf("%I64d",&t2);
        update2(1,n,1,t1,t2);
      }

  }

  }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+19;
int n,m;
struct Node{
  int l,r;
  long long sum,maxn;
  inline int mid(){
    return (l+r)/2;
  }
}num[N*4];
void pushup(int rt){
   num[rt].sum=num[rt<<1].sum+num[rt<<1|1].sum;
   num[rt].maxn=max(num[rt<<1].maxn,num[rt<<1|1].maxn);
}
void build(int l,int r,int rt){
    num[rt].l=l;
    num[rt].r=r;
    if(l==r){
        scanf("%I64d",&num[rt].maxn);
        num[rt].sum=num[rt].maxn;
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int rt,int from,int to,long long mod){
     if(num[rt].maxn<mod)return;
     if(num[rt].l==num[rt].r){
        num[rt].maxn%=mod;
        num[rt].sum=num[rt].maxn;
        return;
     }
     int mid=num[rt].mid();
    if(mid>=to)update(rt<<1,from,to,mod);
    else if(mid<from)update(rt<<1|1,from,to,mod);
    else{
        update(rt<<1,from,mid,mod);
        update(rt<<1|1,mid+1,to,mod);
    }
  pushup(rt);
}
void update2(int rt,int pos,long long val){
     if(num[rt].l==num[rt].r){
       num[rt].maxn=val;
        num[rt].sum=val;
        return;
     }
     int mid=num[rt].mid();
    if(mid>=pos)update2(rt<<1,pos,val);
    else update2(rt<<1|1,pos,val);
  pushup(rt);
}
long long query(int rt,int from,int to){
    if(num[rt].l==from&&num[rt].r==to){
        return num[rt].sum;
    }
   int mid=num[rt].mid();
   if(mid>=to)return query(rt<<1,from,to);
   else if(mid<from)return query(rt<<1|1,from,to);
   else {
      return query(rt<<1,from,mid)+query(rt<<1|1,mid+1,to);

   }
}
int t,t1,t4;
long long t2,t3;
int main(){
  while(~scanf("%d%d",&n,&m)){
      //memset(sum,0,sizeof(sum));
     // memset(maxn,0,sizeof(maxn));
      build(1,n,1);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&t,&t1);
        if(t==1){
            scanf("%d",&t4);
            printf("%I64d\n",query(1,t1,t4));
      }
      else if(t==2){
        scanf("%d%I64d",&t4,&t2);
         update(1,t1,t4,t2);
      }
      else{
            scanf("%I64d",&t2);
       update2(1,t1,t2);
      }
   }
  }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+19;
int n,m;
long long sum[N*4],maxn[N*4];
void pushup(int rt){
   sum[rt]=sum[rt<<1]+sum[rt<<1|1];
   maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%I64d",&maxn[rt]);
        sum[rt]=maxn[rt];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
     if(maxn[rt]<mod)return;
     if(l==r){
        maxn[rt]%=mod;
        sum[rt]=maxn[rt];
        return;
     }
     int mid=(l+r)/2;
    if(mid>=to)update(l,mid,rt<<1,from,to,mod);
    else if(mid<from)update(mid+1,r,rt<<1|1,from,to,mod);
    else{
        update(l,mid,rt<<1,from,mid,mod);
        update(mid+1,r,rt<<1|1,mid+1,to,mod);
    }
  pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
     if(l==r){
        maxn[rt]=val;
        sum[rt]=val;
        return;
     }
     int mid=(l+r)/2;
    if(mid>=pos)update2(l,mid,rt<<1,pos,val);
    else update2(mid+1,r,rt<<1|1,pos,val);
  pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
    if(l==from&&r==to){
        return sum[rt];
    }
   int mid=(l+r)/2;
   if(mid>=to)return query(l,mid,rt<<1,from,to);
   else if(mid<from)return query(mid+1,r,rt<<1|1,from,to);
   else {
      return query(l,mid,rt<<1,from,mid)+query(mid+1,r,rt<<1|1,mid+1,to);

   }
}
int t,t1,t4;
long long t2,t3;
int main(){
  while(~scanf("%d%d",&n,&m)){
      memset(sum,0,sizeof(sum));
      memset(maxn,0,sizeof(maxn));
      build(1,n,1);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&t,&t1);
        if(t==1){
            scanf("%d",&t4);
            printf("%I64d\n",query(1,n,1,t1,t4));
      }
      else if(t==2){
        scanf("%d%I64d",&t4,&t2);
         update(1,n,1,t1,t4,t2);
      }
      else{
            scanf("%I64d",&t2);
        update2(1,n,1,t1,t2);
      }

  }

  }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+19;
int n,m;
long long sum[N*4],maxn[N*4];
void pushup(int rt){
   sum[rt]=sum[rt<<1]+sum[rt<<1|1];
   maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%I64d",&maxn[rt]);
        sum[rt]=maxn[rt];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
     if(maxn[rt]<mod)return;
     if(l==r){
        maxn[rt]%=mod;
        sum[rt]=maxn[rt];
        return;
     }
     int mid=(l+r)/2;
    if(mid>=to)update(l,mid,rt<<1,from,to,mod);
    else if(mid<from)update(mid+1,r,rt<<1|1,from,to,mod);
    else{
        update(l,mid,rt<<1,from,mid,mod);
        update(mid+1,r,rt<<1|1,mid+1,r,mod);
    }
  pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
     if(l==r){
        maxn[rt]=val;
        sum[rt]=val;
        return;
     }
     int mid=(l+r)/2;
    if(mid>=pos)update2(l,mid,rt<<1,pos,val);
    else update2(mid+1,r,rt<<1|1,pos,val);
  pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
    if(l==from&&r==to){
        return sum[rt];
    }
   int mid=(l+r)/2;
   if(mid>=to)return query(l,mid,rt<<1,from,to);
   else if(mid<from)return query(mid+1,r,rt<<1|1,from,to);
   else {
      return query(l,mid,rt<<1,from,mid)+query(mid+1,r,rt<<1|1,mid+1,to);

   }
}
int t,t1,t4;
long long t2,t3;
int main(){
  while(~scanf("%d%d",&n,&m)){
      memset(sum,0,sizeof(sum));
      memset(maxn,0,sizeof(maxn));
      build(1,n,1);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&t,&t1);
        if(t==1){
            scanf("%d",&t4);
            printf("%I64d\n",query(1,n,1,t1,t4));
      }
      else if(t==2){
        scanf("%d%I64d",&t4,&t2);
         update(1,n,1,t1,t4,t2);
      }
      else{
            scanf("%I64d",&t2);
        update2(1,n,1,t1,t2);
      }

  }

  }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+19;
int n,m;
long long sum[N*4],maxn[N*4];
void pushup(int rt){
   sum[rt]=sum[rt<<1]+sum[rt<<1|1];
   maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%I64d",&maxn[rt]);
        sum[rt]=maxn[rt];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
    // if(maxn[rt]<mod)return;
     if(l==r){
        maxn[rt]%=mod;
        sum[rt]=maxn[rt];
        return;
     }
     int mid=(l+r)/2;
    if(mid>=to)update(l,mid,rt<<1,from,to,mod);
    else if(mid<from)update(mid+1,r,rt<<1|1,from,to,mod);
    else{
        update(l,mid,rt<<1,from,mid,mod);
        update(mid+1,r,rt<<1|1,mid+1,r,mod);
    }
  pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
     if(l==r){
        maxn[rt]=val;
        sum[rt]=val;
        return;
     }
     int mid=(l+r)/2;
    if(mid>=pos)update2(l,mid,rt<<1,pos,val);
    else update2(mid+1,r,rt<<1|1,pos,val);
  pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
    if(l==from&&r==to){
        return sum[rt];
    }
   int mid=(l+r)/2;
   if(mid>=to)return query(l,mid,rt<<1,from,to);
   else if(mid<from)return query(mid+1,r,rt<<1|1,from,to);
   else {
      return query(l,mid,rt<<1,from,mid)+query(mid+1,r,rt<<1|1,mid+1,to);

   }
}
int t,t1,t4;
long long t2,t3;
int main(){
  while(~scanf("%d%d",&n,&m)){
      memset(sum,0,sizeof(sum));
      memset(maxn,0,sizeof(maxn));
      build(1,n,1);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&t,&t1);
        if(t==1){
            scanf("%d",&t4);
            printf("%I64d\n",query(1,n,1,t1,t4));
      }
      else if(t==2){
        scanf("%d%I64d",&t4,&t2);
         update(1,n,1,t1,t4,t2);
      }
      else{
            scanf("%I64d",&t2);
        update2(1,n,1,t1,t2);
      }

  }

  }
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+19;
int n,m;
long long sum[N*4],maxn[N*4];
void pushup(int rt){
   sum[rt]=sum[rt<<1]+sum[rt<<1|1];
   maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%I64d",&maxn[rt]);
        sum[rt]=maxn[rt];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
     //if(maxn[rt]<mod)return;
     if(l==r){
        maxn[rt]%=mod;
        sum[rt]=maxn[rt];
        return;
     }
     int mid=(l+r)/2;
    if(mid>=to)update(l,mid,rt<<1,from,to,mod);
    else if(mid<from)update(mid+1,r,rt<<1|1,from,to,mod);
    else{
        update(l,mid,rt<<1,from,mid,mod);
        update(mid+1,r,rt<<1|1,mid+1,to,mod);
    }
  pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
     if(l==r){
        maxn[rt]=val;
        sum[rt]=val;
        return;
     }
     int mid=(l+r)/2;
    if(mid>=pos)update2(l,mid,rt<<1,pos,val);
    else update2(mid+1,r,rt<<1|1,pos,val);
  pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
    if(l==from&&r==to){
        return sum[rt];
    }
   int mid=(l+r)/2;
   if(mid>=to)return query(l,mid,rt<<1,from,to);
   else if(mid<from)return query(mid+1,r,rt<<1|1,from,to);
   else {
      return query(l,mid,rt<<1,from,mid)+query(mid+1,r,rt<<1|1,mid+1,to);

   }
}
int t,t1,t4;
long long t2,t3;
int main(){
  while(~scanf("%d%d",&n,&m)){
      memset(sum,0,sizeof(sum));
      memset(maxn,0,sizeof(maxn));
      build(1,n,1);
      for(int i=1;i<=m;i++){
        scanf("%d%d",&t,&t1);
        if(t==1){
            scanf("%d",&t4);
            printf("%I64d\n",query(1,n,1,t1,t4));
      }
      else if(t==2){
        scanf("%d%I64d",&t4,&t2);
         update(1,n,1,t1,t4,t2);
      }
      else{
            scanf("%I64d",&t2);
        update2(1,n,1,t1,t2);
      }

  }

  }
}