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
// PA2014, runda 5, Muzeum
// Andrzej Pezarski

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

const long long INF=1000000000000000000LL;
int N, M, NL, L;
long long W, H;

vector<pair<long long, long long> > T;

long long update(int v, long long c, long long x=0LL) {
	if (v==1) {
		return x+c;		
	}
	x=update(v/2, c, max(x+T[v].first, T[v].second));
	
	if (v%2) {
		T[v-1].second=max(T[v-1].second, x);
	} else {
		T[v+1].first+=c;
		T[v+1].second+=c;
	}
	
	if (v>=L) {
		T[v]=make_pair(x, -INF);
	} else {
		T[2*v].first+=T[v].first;
		T[2*v].second+=T[v].first;
		T[2*v+1].first+=T[v].first;
		T[2*v+1].second+=T[v].first;
		
		T[2*v].second=max(T[2*v].second, T[v].second);
		T[2*v+1].second=max(T[2*v+1].second, T[v].second);
		
		T[v]=make_pair(0LL, -INF);
	}
	
	return x;
}

int main() {
	scanf("%d%d", &N, &M);
	scanf("%lld%lld", &W, &H);
	vector<pair <pair<long long, long long>, long long> > A(N+M);
	vector<long long> Y(N+M);
	for (int i=0; i<N; i++) {
		int x, y, v;
		scanf("%d%d%d", &x, &y, &v);
		A[i].second=v;
		A[i].first.first=-W*y+H*x;
		Y[i]=A[i].first.second=-W*y-H*x;
	}
	for (int i=N; i<N+M; i++) {
		int x, y, v;
		scanf("%d%d%d", &x, &y, &v);
		A[i].second=-v;
		A[i].first.first=-W*y+H*x;
		Y[i]=A[i].first.second=-W*y-H*x;
	}
	sort(Y.begin(), Y.end());
	Y.resize(NL=unique(Y.begin(), Y.end())-Y.begin());
	L=1; while(L<=NL) L*=2;
	T.resize(2*L);
	for (auto &t:T) t=make_pair(0LL,-INF);
	
	sort(A.begin(), A.end());
	long long R=0;
	for(auto a:A) {
		R=max(R, update(L+lower_bound(Y.begin(), Y.end(), a.first.second)-Y.begin(), a.second));
	}
	
//	printf("%lld\n", update(L, 0));
	printf("%lld\n", R);
	return 0;
}