Darts (AOJ 0529) by mzsrkn10
Contest: 3575    RunID: 2375999    Status: Wrong Answer    Date: Sat Jun 17 14:06:11 JST 2017


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

long long int dp[1002][5] = {0};
int p[1002] = {0};

int main(){
    int n, m;
    while(scanf("%d %d",&n, &m), n != 0 || m != 0){
        for(int i=0;i<1002;++i){
            p[i] = 0;
            for(int j=0;j<4;++j)
                dp[i][j] = 0;
        }
        for(int i=1;i<=n;++i){
            cin >> p[i];
            dp[i][0] = p[i];
        }
        for(int i=1;i<4;++i){
            for(int j=0;j<=n;++j){
                for(int k=0;k<=n;++k){
                    dp[j][i] = max(dp[j][i], (dp[k][i-1] + p[j]) % (m+1));
                }
            }
        }
        long long int ans=0;
        for(int i=0;i<=n;++i)ans = max(ans, dp[i][3]);
        cout << ans << endl;
    }

    return 0;
}