1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>

#define SC(n) scanf("%d", &n)
#define SC2(n, m) scanf("%d %d", &n, &m)
#define SCC(c) scanf(" %c", &c)
#define SCS(b) scanf("%s", b)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FORE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i)
#define PR(n) printf("%d ", (int) (n)) 
#define PRN(n) printf("%d\n", (int) (n)) 
#define elif else if
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(), v.end()
#define itr iterator
#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<pi, pi> piii;
typedef vector<vpi> vvpi;
typedef map<int, vpi> mvpi;
typedef map<ll, int> mli;

const int NMAX = 500*1000 + 7, TMAX = (1<<21) + 5;

int n, m, w, h, k, x1, yy, c, typ, mag;
mli posx, posy;
int x[NMAX], y[NMAX], v[NMAX], ql[NMAX], qr[NMAX];
piii el[NMAX];
ll out, s[NMAX], sum[NMAX];
ll tma[TMAX], ts[TMAX];

void init() {
	mag = 1;
	while(mag < n + m + 2)
		mag *= 2;
}

void addVal(int r, ll x) {
	dbg cout << "addVal " << r << " " << x << endl;
	r += mag;
	ts[r] += x;
	tma[r] += x;
	r /= 2;
	
	while(r > 0) {
		ts[r] = ts[2*r] + ts[2*r+1];
		tma[r] = max(tma[2*r+1], ts[2*r+1] + tma[2*r]);
		r /= 2;
	}
}

ll findMax(int r) {
	prd("findMax %d\n", r);
	int l = mag;
	r += mag;
	int kl = 0, kr = 0;
	
	while(l < r) {
	    if(l % 2 == 1) {
	      ql[kl++] = l;
	      l = l/2 + 1;
	    }
	    else
	      l /= 2;
	    
	    if(r % 2 == 0) {
	      qr[kr++] = r;
	      r = r/2 - 1;
	    }
	    else
	      r /= 2;
	  }
  	if(l == r)
  		qr[kr++] = r;
		
	FORD(i, kl-1, 0)
		qr[kr++] = ql[i];
	
	ll sum = 0, ans = 0;
	REP(i, kr) {
		ans = max(ans, sum + tma[qr[i]]);
		sum += ts[qr[i]];
		dbg cout << qr[i] << " sum: " << sum << " prop: " << sum - ts[qr[i]] + tma[qr[i]] << endl;
	}	
	return ans;
}

void printTree() {
	FORE(i, 1, mag + n + m)
		cout << i << ": " << ts[i] << " " << tma[i] << endl;
	prd("\n");
}

int main() {
	SC2(n, m);
	SC2(w, h);
	REP(i, n) {
		SC2(x[i], y[i]);
		SC(v[i]);
		out += v[i];
	}
	FORE(i, n, n + m - 1) {
		SC2(x[i], y[i]);
		SC(v[i]);
		out -= v[i];
	}
	out = max(out, 0LL); // przekupienie wszystkich
	
	REP(i, n+m) {	
		ll a1 = (ll) x[i] * h;
		ll a2 = (ll) y[i] * w;
		posx[a2 - a1] = 1;
		posy[a2 + a1] = 1;
		dbg cout << a2 - a1 << " " <<  a2 + a1 << endl;
	}
	prd("\n");

	k = 1;
	FORIT(it, posx, mli)
		it->yy = k++;
	k = 1;
	FORIT(it, posy, mli)
		it->yy = k++;
		
	REP(i, n+m) {	
		ll a1 = (ll) x[i] * h;
		ll a2 = (ll) y[i] * w;
		x1 = posx[a2 - a1];
		yy = posy[a2 + a1];
		
		if(i < n)
			sum[yy] += v[i];
		else
			sum[yy] -= v[i];
		
		el[i] = mp(mp(x1, yy), mp(v[i], (i<n)));
		prd("%d %d %d %d\n", x1, yy, v[i], (i<n));
	}	
	
	FORD(i, m+n+3, 0)
		sum[i] += sum[i+1];
	
	sort(el, el+n+m);
	reverse(el, el+n+m);
	init();
	
	REP(i, n+m) {
		x1 = el[i].xx.xx;
		yy = el[i].xx.yy;
		c = el[i].yy.xx;
		typ = el[i].yy.yy;
		prd("process %d %d %d\n", x1, yy, typ);
		
		if(typ == 1) // eksponat
			addVal(yy - 1, c);
		else { // straznik
			ll ss = s[yy] + findMax(yy - 1);
			ll s2 = max(s[yy], ss);
			
			if(s2 > s[yy])
				addVal(yy, s2 - s[yy]);
			addVal(yy - 1, s[yy] - s2 - c);
			
			dbg cout << x1 << " " << yy << " findMax: " << ss-s[yy] << " prop: " << ss << " + " << sum[yy + 1] << endl;
			s[yy] = s2;
			out = max(out, ss + sum[yy + 1]);
		}
		dbg printTree();
	}
	
	printf("%lld", out);
	//cout << out;
	return 0;
}