Source Code
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define fi first
#define se second
#define pb push_back
#define md ((st + nd) >> 1)
#define lc (1 + (idx << 1))
#define rc (2 + (idx << 1))
const int N = 200005;
int a[N], b[N], c[N], d[N], lst1[N], lst2[N], lst3[N];
int dp[10][10][10];

int main() {
	int n, k1, k2, k3;
	scanf("%d %d %d %d", &n, &k1, &k2, &k3);
	int res = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i] % 4;
		c[i] = a[i] % 5;
		d[i] = a[i] % 8;
		int mx = 0;
		for (int x = 0; x < 4; x++) {
			if (abs(x - b[i]) > k1) continue;
			for (int y = 0; y < 5; y++) {
				if (abs(y - c[i]) > k2) continue;
				for (int z = 0; z < 8; z++) {
					if (abs(z - d[i]) > k3) continue;
					mx = max(mx, dp[x][y][z]);
				}
			}
		}
		dp[b[i]][c[i]][d[i]] = mx + 1;
		res = max(res, mx + 1);
	}
	printf("%d", res);
	return 0;
}
Copy
Band Song 2 aboAdnan
GNU G++17
104 ms
3.6 MB
Accepted