結果

問題 No.324 落ちてた閉路グラフ
ユーザー nanophoto12
提出日時 2015-12-27 22:29:03
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 215 ms / 5,000 ms
コード長 1,490 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 70,648 KB
実行使用メモリ 326,300 KB
最終ジャッジ日時 2024-11-15 19:38:14
合計ジャッジ時間 9,722 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

#define FOR(x,y) for(int x = 0;x < y;x++)
#define LLI long long int

template<int um> class UF {
public:
    vector<int> par,rank,cnt;
    UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
    void reinit() {FOR(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
    int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
    int count(int x) { return cnt[operator[](x)];}
    int operator()(int x,int y) {
        if((x=operator[](x))==(y=operator[](y))) return x;
        cnt[y]=cnt[x]=cnt[x]+cnt[y];
        if(rank[x]>rank[y]) return par[x]=y;
        rank[x]+=rank[x]==rank[y]; return par[y]=x;
    }
};

int N,M;
int W[5000];
int dp[3][3][3030][3030];

void solve() {
    
    cin>>N>>M;
    FOR(i,N) cin>>W[i];
    
    memset(dp,0xce,sizeof(dp));
    
    dp[0][0][0][0]=dp[1][1][0][1]=0;
    int ma=-100000000;
    FOR(r,2) {
        FOR(i,N-1) {
            FOR(j,M+1) {
                dp[r][0][i+1][j]=max(dp[r][0][i][j],dp[r][1][i][j]);
                if(j) dp[r][1][i+1][j]=max(dp[r][0][i][j-1],dp[r][1][i][j-1]+W[i]);
            }
        }
        
        ma=max(ma,dp[r][0][N-1][M]);
        if(r==0) ma=max(ma,dp[r][1][N-1][M]);
        if(r==1) ma=max(ma,dp[r][1][N-1][M]+W[N-1]);
    }
    
    
    cout<<ma<<endl;
    
}
int main() {
    solve();
    return 0;
}
0