Source Code
#include <bits/stdc++.h>
using namespace std;
 
#define S second
#define F first
#define LL long long 
#define LINF 100000000000000000
#define INF 1000000000
 
const int N = 2e5+10;
const LL MOD = 1e9+7;

int a[N][3];
int k[3];
int dp[N][4][5][8];
int n;

int solve(int i,int last1,int last2,int last3){

    if(i==n+1)
        return 0;

    if(dp[i][last1][last2][last3]!=-1)
        return dp[i][last1][last2][last3];

    dp[i][last1][last2][last3] = solve(i+1,last1,last2,last3);
    if(abs(a[i][0]-last1)<=k[0]&&abs(a[i][1]-last2)<=k[1]&&abs(a[i][2]-last3)<=k[2])
        dp[i][last1][last2][last3] = max(dp[i][last1][last2][last3],1+solve(i+1,a[i][0],a[i][1],a[i][2]));

    return dp[i][last1][last2][last3];
}

int main()
{      
    //freopen("out.txt","w",stdout);

    scanf("%d",&n);

    for(int i=0;i<3;i++)
        scanf("%d",&k[i]);

    for(int i=1;i<=n;i++){
        int x;scanf("%d",&x);
        a[i][0] = x%4;
        a[i][1] = x%5;
        a[i][2] = x%8;
    }

    memset(dp,-1,sizeof(dp));

    int mx = 0;
    for(int i=1;i<=n;i++)
        mx = max(mx,solve(i,a[i][0],a[i][1],a[i][2]));
    
    printf("%d\n",mx);
}
Copy
Band Song 2 Hazem17
GNU G++17
429 ms
140.6 MB
Accepted