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

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


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

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

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

int main(){
    scanf("%d%d%d%d", &n, &k1, &k2, &k3);
    for(int i = 0; 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(0, 0, 0, 0, 0);
   printf("%d\n", an);
  //  bld(0, -1, -1, -1);
   // printf("%d %d\n", f+1, s+1);
}
Copy
Band Song 2 molukhyyeh
GNU G++17
266 ms
262.1 MB
Memory Limit Exceeded