/* -*- coding: utf-8 -*- * * 2434.cc: No.2434 RAKUTAN de RAKUTAN - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_GN = 1000; const int MAX_BN = 1000; const int MAX_X = 5; const int MAX_M = MAX_GN + MAX_BN * (MAX_X * 2 - 1) + 2; const int MAX_H = MAX_BN; const int INF = 1 << 30; /* typedef */ using pii = pair; /* global variables */ pii ps[MAX_M], lrs[MAX_M]; int uxs[MAX_M], uvs[MAX_M]; int dp[MAX_M][MAX_H + 1]; /* subroutines */ inline void setmax(int &a, int b) { if (a < b) a = b; } /* main */ int main() { int n, h, x; scanf("%d%d%d", &n, &h, &x); int m = 0; ps[m++] = {0, 0}; int gn; scanf("%d", &gn); for (int i = 0; i < gn; i++) { int gi; scanf("%d", &gi); ps[m++] = {gi, 1}; } int bn; scanf("%d", &bn); for (int i = 0; i < bn; i++) { int bi; scanf("%d", &bi); ps[m++] = {bi, -1}; } sort(ps, ps + m); if (ps[m - 1].first < n) ps[m++] = {n, 0}; //printf(" m=%d\n", m); //for (int i = 0; i < m; i++) // printf(" %d,%d", ps[i].first, ps[i].second); //putchar('\n'); for (int i = 0; i < m; i++) { auto [xi, v] = ps[i]; if (v < 0) lrs[i] = {max(0, xi - (x - 1)), min(n + 1, xi + x)}; else lrs[i] = {xi, xi + 1}; } sort(lrs, lrs + m); //for (int i = 0; i < m; i++) // printf(" %d,%d", lrs[i].first, lrs[i].second); //putchar('\n'); int l = -1, r = -1, k = 0; for (int i = 0; i < m; i++) { auto [li, ri] = lrs[i]; if (r < li) { if (l >= 0) while (l < r) uxs[k++] = l++; l = li, r = ri; } else r = max(r, ri); } while (l < r) uxs[k++] = l++; //for (int i = 0; i < k; i++) printf(" %d", uxs[i]); putchar('\n'); for (int i = 0, j = 0; i < k; i++) { while (j < m && ps[j].first < uxs[i]) j++; uvs[i] = (j < m && ps[j].first == uxs[i]) ? ps[j].second : 0; } //for (int i = 0; i < k; i++) printf(" %d,%d", uxs[i], uvs[i]); //putchar('\n'); h = min(h, bn); for (int i = 0; i < k; i++) fill(dp[i], dp[i] + h + 1, -INF); dp[0][0] = 0; for (int i = 0; i < k - 1; i++) for (int j = 0; j <= h; j++) if (dp[i][j] > -INF) { setmax(dp[i + 1][j], dp[i][j] + uvs[i + 1]); if (j < h) { int i1 = i + 1; while (i1 < k && uxs[i1] < uxs[i] + x) i1++; if (i1 < k && uxs[i1] == uxs[i] + x) setmax(dp[i1][j + 1], dp[i][j] + uvs[i1]); } } int maxd = -INF; for (int j = 0; j <= h; j++) setmax(maxd, dp[k - 1][j]); printf("%d\n", maxd); return 0; }