This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
                              ///~~~LOTA~~~///
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define append push_back
#define add insert
#define nl "\n"
#define ff first
#define ss second
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL)
#define N 1501
int d;
int dp[N][N];
vector<int> a[N];
void dfs(int v){
    int m=(d+1)/2;
    for(auto& j:a[v]){
            dfs(j);
        for(int i=m;i<=d;i++)
            dp[v][i%d]+=dp[j][i-1];
    }
    for(int i=1;i<m;i++)
        for(auto& j:a[v])
            dp[v][i]=max(dp[v][i],dp[j][i-1]+dp[v][d-i]-dp[j][d-i-1]);
    dp[v][0]++;
    for(int i=d;i>0;i--)
        dp[v][i-1]=max(dp[v][i-1],dp[v][i]);
}
void solve(){
    int n,m;
    cin>>n>>d;
    for(int i=1;i<n;i++){
        cin>>m;
        a[m].append(i);
    }
    dfs(0);
    cout<<dp[0][0]<<nl;
}
int main(){
    L0TA;
    solve();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |