Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 2e5+2;
int n, k1, k2, k3, v[N], f = 1e6, s = -1e6;
int a[N], b[N], c[N];
short dp[N][5][6][9][2];

bool ok(int i, int l0, int l1, int l2){
      return abs(l0-a[i]) <= k1 && abs(l1-b[i]) <= k2 && abs(l2-c[i]) <= k3;
}

short go(int i, int l0, int l1, int l2, bool f){
    if(i == n+1)return 0;
    short &ret = dp[i][l0][l1][l2][f];
    if(ret != -1)return ret;
    //cout << l0 <<" "<<l1<<" "<<l2<<" "<<i<<"\n";
    ret = go(i+1, l0, l1, l2, f);
   // if(i == 8)cout << l0 <<" "<<l1<<" "<<l2<<" "<<ok(i, l0, l1, l2) <<"\n";
    if(!f || ok(i, l0, l1, l2))ret = max(ret, (short)(go(i+1, a[i], b[i], c[i], 1)+1));
    return ret;
}

/*void bld(int i, int l0, int l1, int l2, bool f){
    if(i == n+1)return;
    if(go(i+1, l0, l1, l2) == go(i, l0, l1, l2)){
         bld(i+1, l0, l1, l2);
         return;
    }
    if(!i || ok(i, l0, l1, l2)){
         if(go(i+1, a[i], b[i], c[i])+1 == go(i, l0, l1, l2)){
              cout << i <<" ";
              bld(i+1, a[i], b[i], c[i]);
              return;
         }
    }
}*/

int main(){
    scanf("%d%d%d%d", &n, &k1, &k2, &k3);
    for(int i = 1; i<=n; ++i){
       scanf("%d", &v[i]);
       a[i] = v[i] % 4;
       b[i] = v[i] % 5;
       c[i] = v[i] % 8;
    }
    memset(dp, -1, sizeof dp);
    int an = go(1, 0, 0, 0, 0);
    printf("%d\n", an);
    //bld(1, 0, 0, 0, 0);
}
Copy
Band Song 2 molukhyyeh
GNU G++17
612 ms
236.8 MB
Wrong Answer