Source Code
/*
    # Enjoy the journey #
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define forq(i , a , b) for (int i = (a); i <= (b); ++i)
#define qrof(i , b , a) for (int i = (a); i >= (b); --i)
#define forr(i , b) forq( i , 0 , b - 1 )
#define F first
#define S second
#define IF ->first
#define IS ->second
#define endl '\n'
#define qqmemset(array , val) memset (array , val , sizeof(array))
#define ALLV(vect) vect.begin() , vect.end()
#define mid (st + en) / 2
#define mid1 (2 * st + en) / 3
#define mid2 (2 * en + st) / 3
#define lef 2 * Node
#define rig lef + 1
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define Ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)

ll R = 7 + 1e9 , R1 = 19491001 , R2 = 236 , NUMTESTCASE ;
const ll NN = 10 + 2e5 ;
const double pi = acos(-1.0) ;
int di [8] = {1 , 0 , -1 , 0  , 1 , -1 , 1  , -1 } , dj [8] = {0 , 1 , 0  , -1 , 1 , -1 , -1 , 1  } ;
pair <int, pair <int,int> > Ans , Curr ;
int n , k [5] , x , Len , T [5] , Arr [NN] , Memo [NN][4][5][8] ;
int Dp (int x , int l1 , int l2 , int l3) {
    if (x > n) return 0 ;
    int &Res = Memo [x][l1][l2][l3] ;
    if (Res + 1)
        return Res ;
    Res = Dp (x + 1 , l1 , l2 , l3) ;
    bool Ok = true;
    int y = Arr [x] ;
    int L [] = {0 , l1 , l2 , l3} ;
    forq (j , 1 , 3) {
        int z = y % T [j] ;
        z = abs(z - L [j]) ;
        if (z > k [j]) Ok = false ;
        L [j] = y % T [j] ;
    }
    if (Ok) Res = max(Res , 1 + Dp (x + 1 , L [1] , L [2] , L[3])) ;
    return Res ;
}
int main()
{
    T [1] = 4 ;
    T [2] = 5 ;
    T [3] = 8 ;
    ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL) ;
    cin >> n ;
    forq (i , 1 , 3) cin >> k [i] ;
    forq (i , 1 , n) cin >> Arr [i] ;
    qqmemset(Memo , -1) ;
    int Ans = 0 ;
    forr (x , T [1])
        forr (y , T [2])
            forr (z , T [3])
                Ans = max(Ans , Dp (1 , x , y , z)) ;

    cout << Ans ;
    return 0;
}
Copy
Band Song 2 Practice_Til_Red00
GNU G++17
1596 ms
151.5 MB
Time Limit Exceeded