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

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

const int N = 2e5;

int arr1[N], arr2[N], arr3[N];
int dp[N][4][5][8];
int n, k1, k2, k3;

int calc(int ind, int a, int b, int c){
    if(ind == n){
        return 0;
    }
    if(dp[ind][a][b][c] != -1){
        return dp[ind][a][b][c];
    }
    int ch1 = calc(ind + 1, a, b, c);
    int ch2 = 0;
    if(abs(arr1[ind] - a) <= k1 && abs(arr2[ind] - b) <= k2 && abs(arr3[ind] - c) <= k3){
        ch2 = calc(ind + 1, arr1[ind], arr2[ind], arr3[ind]) + 1;
    }
    return dp[ind][a][b][c] = max(ch1, ch2);
}
int main(){
    FAST
    cin >> n >> k1 >> k2 >> k3;
    for(int i = 0; i < n; i++){
        cin >> arr1[i];
        arr2[i] = arr1[i] % 5;
        arr3[i] = arr1[i] % 8;
        arr1[i] %= 4;
    }
    memset(dp, -1, sizeof(dp));
    int ans = 0;
    for(int i = 0; i < n; i++){
        ans = max(ans, calc(i + 1, arr1[i], arr2[i], arr3[i]) + 1);
    }
    cout << ans;
    return 0;
}
Copy
Band Song 2 Basilhijaz
GNU G++17
476 ms
140.5 MB
Accepted