Source Code
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define F first
#define S second

const int N = 2e5+2 ;
int a[N][4] ;
int n,ans ;
int k1, k2, k3 ;
int Dp[N][4][5][8] ;

int dp(int i, int lstFour, int lstFife, int lstEight)
{

    if (i == n+1)
        return 0 ;
    int &ret = Dp[i][lstFour][lstFife][lstEight] ;
    if (ret + 1)
        return ret ;
    int p1 = 0 ;
    int p2 = 0 ;

    if (
        abs(a[i][1] - lstFour) <= k1 &&
        abs(a[i][2] - lstFife) <= k2 &&
        abs(a[i][3] - lstEight) <= k3
    )
        p1 = 1 + dp(i+1, a[i][1], a[i][2], a[i][3]) ;
    p2 = dp(i+1, lstFour, lstFife, lstEight) ;

    return ret = max(p1, p2) ;

}
int main()
{
#ifdef Mohammad
    freopen("input.in","r",stdin);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> k1 >> k2 >> k3 ;
    memset(Dp, -1, sizeof Dp) ;

    for (int i = 1 ; i<= n ; i++)
        cin >> a[i][0] ;

    for (int i = 1 ; i<= n ; i++)
        a[i][1] = a[i][0] % 4 ;
    for (int i = 1 ; i<= n ; i++)
        a[i][2] = a[i][0] % 5 ;
    for (int i = 1 ; i<= n ; i++)
        a[i][3] = a[i][0] % 8 ;

    for (int i = 1 ; i<= n ; i++)
        ans = max(1 + dp(i+1, a[i][1], a[i][2], a[i][3]), ans) ;

    cout<<ans<<"\n";
}
Copy
Band Song 2 Mohammad.Nour
GNU G++17
333 ms
141.3 MB
Accepted