結果
問題 |
No.1199 お菓子配り-2
|
ユーザー |
![]() |
提出日時 | 2020-09-11 18:39:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 223 ms / 1,000 ms |
コード長 | 1,445 bytes |
コンパイル時間 | 4,691 ms |
コンパイル使用メモリ | 171,220 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-26 10:03:56 |
合計ジャッジ時間 | 11,930 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; template<class T> inline bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; } struct Unionfind { vector<int> par; Unionfind(int n) : par(n) { for(int i = 0; i < n; ++i) par[i] = i; } int root(int x) { if(par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if(rx == ry) return; par[rx] =ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<ll> a(n); rep(i, n) { ll t = 0; rep(j, m) { ll ac; cin >> ac; t += ac; } a[i] = t; } vector<vector<ll>> dp(n, vector<ll>(2)); dp[0][0] = a[0]; dp[0][1] = 0; for(int i = 0; i < n - 1; ++i) { dp[i+1][0] = max(dp[i][1] + a[i+1], dp[i][0]); dp[i+1][1] = max(dp[i][0] - a[i+1], dp[i][1]); } cout << max(dp[n-1][0], dp[n-1][1]) << endl; return 0; }