結果
問題 | No.324 落ちてた閉路グラフ |
ユーザー |
|
提出日時 | 2021-03-23 18:59:56 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 79 ms / 5,000 ms |
コード長 | 2,198 bytes |
コンパイル時間 | 1,413 ms |
コンパイル使用メモリ | 132,404 KB |
最終ジャッジ日時 | 2025-01-19 21:18:12 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 34 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <queue> #include <string> #include <map> #include <set> #include <stack> #include <tuple> #include <deque> #include <array> #include <numeric> #include <bitset> #include <iomanip> #include <cassert> #include <chrono> #include <random> #include <limits> #include <iterator> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <cstring> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; constexpr int INF = 1001001001; // constexpr int mod = 1000000007; constexpr int mod = 998244353; template<class T> inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template<class T> inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int dp[3005][3005][2]; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> w(n); for(int i = 0; i < n; ++i) cin >> w[i]; if(m <= 1){ cout << 0 << '\n'; return 0; } int ans = -INF; for(int mask = 0; mask < 2; ++mask){ for(int i = 0; i < n; ++i){ for(int j = 0; j <= m; ++j){ for(int k = 0; k < 2; ++k){ dp[i][j][k] = -INF; } } } dp[0][mask][mask] = 0; for(int i = 0; i < n - 2; ++i){ for(int j = 0; j <= m; ++j){ for(int k = 0; k < 2; ++k){ if(dp[i][j][k] == -INF) continue; for(int nk = 0; nk < 2; ++nk){ chmax(dp[i + 1][j + nk][nk], dp[i][j][k] + (k & nk ? w[i] : 0)); } } } } for(int j = m - 1; j <= m; ++j){ chmax(dp[n - 1][j][0], max(dp[n - 2][j][0], dp[n - 2][j][1])); chmax(dp[n - 1][j + 1][1], max(dp[n - 2][j][0], dp[n - 2][j][1] + w[n - 2]) + (mask ? w[n - 1] : 0)); } chmax(ans, max(dp[n - 1][m][0], dp[n - 1][m][1])); } cout << ans << '\n'; return 0; }