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
#include <algorithm>
#include <cstdio>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORD(i,a,b) for (int i = (b) - 1; i >= (a); --i)
#define REPD(i,n) FORD(i,0,n)
typedef long long LL;

int n, m, w, h;
int x1[200000], why1[200000], v1[200000], x2[200000], y2[200000], v2[200000];
bool bad[200000];

bool see(LL x1, LL why1, LL x2, LL y2) {
	if (y2 > why1) return false;
	LL ww = (why1 - y2) * w / h;
	return x1 - ww <= x2 && x2 <= x1 + ww;
}

int main() {
	scanf("%d%d%d%d", &n, &m, &w, &h);
	REP(i,n) scanf("%d%d%d", &x1[i], &why1[i], &v1[i]);
	REP(i,m) scanf("%d%d%d", &x2[i], &y2[i], &v2[i]);
	REP(i,m) v2[i] = -v2[i];
	bool flipped = 0;
	if (n < m) {
		int nn = max(n, m);
		REP(i,nn) {
			swap(x1[i], x2[i]);
			swap(why1[i], y2[i]);
			swap(v1[i], v2[i]);
		}
		swap(n, m);
		REP(i,n) {
			why1[i] = -why1[i];
			v1[i] = -v1[i];
		}
		REP(i,m) {
			y2[i] = -y2[i];
			v2[i] = -v2[i];
		}
		flipped = 1;
	}
	int n2 = 1 << n;
	LL best = -8000000000000000000LL;
	REP(ma,n2) {
		LL r = 0;
		REP(i,m) bad[i] = 0;
		REP(i,n) if (ma & (1 << i)) {
			if (!flipped) r += v1[i];
			REP(j,m) if (see(x2[j], y2[j], x1[i], why1[i])) bad[j] = 1;
		} else if (flipped) r += v1[i];
		REP(i,m) if (bad[i] ^ flipped) r += v2[i];
		best = max(best, r);
	}
	printf("%lld\n", best);
}