Source Code
#include <bits/stdc++.h>
using namespace std;
    
int main (){

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k[3];
  cin >> n;
  for (int i = 0; i < 3; ++i) {
  	cin >> k[i];
  }
  int a[n];
  for (int i = 0; i < n; ++i) {
  	cin >> a[i];
  }
	int dp[n + 1][4][5][8] = {};
  for (int i = n - 1; ~i; --i) {
  	for (int m1 = 0; m1 < 4; ++m1) {
	  	for (int m2 = 0; m2 < 5; ++m2) {
		  	for (int m3 = 0; m3 < 8; ++m3) {
		  		dp[i][m1][m2][m3] = dp[i + 1][m1][m2][m3];
		  		if (abs((a[i] % 4) - m1) <= k[0] && abs((a[i] % 5) - m2) <= k[1] && abs((a[i] % 8) - m3) <= k[2]) {
		  			dp[i][m1][m2][m3] = max(dp[i][m1][m2][m3], dp[i + 1][a[i] % 4][a[i] % 5][a[i] % 8] + 1);
		  		}
		  	}
		  }
		}
	}
	int ans = 0;
	for (int m1 = 0; m1 < 4; ++m1) {
  	for (int m2 = 0; m2 < 5; ++m2) {
	  	for (int m3 = 0; m3 < 8; ++m3) {
	  		ans = max(ans, dp[0][m1][m2][m3]);
	  	}
	  }
	}
	cout << ans;

  return 0;
}
Copy
Band Song 2 SuhaibSawalha1
GNU G++17
301 ms
126.5 MB
Accepted