結果

問題 No.324 落ちてた閉路グラフ
ユーザー SHIJOU
提出日時 2020-08-18 20:39:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,870 bytes
コンパイル時間 2,281 ms
コンパイル使用メモリ 192,104 KB
最終ジャッジ日時 2025-01-13 03:27:02
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:36:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:37:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |   rep(i, n) scanf("%d", w+i);
      |             ~~~~~^~~~~~~~~~~

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

const int N = 3010;

//head

int n, m;
int w[N];
int dpe[N], dpn[N];
int ans;

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d", w+i);

  if(m == 0) {
    printf("0\n");
    return 0;
  }
  if(m == n) {
    printf("%d\n", accumulate(w, w+n, 0));
    return 0;
  }

  fill(dpe, dpe+m+1, -INF);
  dpe[1] = 0;
  fill(dpn, dpn+m+1, -INF);
  rep(i, n-2) {
    for(int j = m; j >= 0; j--) {
      chmax(dpn[j], dpe[j]);
      if(j) {
        dpe[j] = max(dpe[j-1]+w[i], dpn[j-1]);
      }
    }
  }
  chmax(dpn[m], dpe[m]);
  dpe[m] = max(dpe[m-1]+w[n-2]+w[n-1], dpn[m-1]+w[n-1]);
  ans = max(dpe[m], dpn[m]);


  fill(dpe, dpe+m+1, -INF);
  fill(dpn, dpn+m+1, -INF);
  dpn[0] = 0;
  rep(i, n-1) {
    for(int j = m; j >= 0; j--) {
      chmax(dpn[j], dpe[j]);
      if(j) {
        dpe[j] = max(dpe[j-1]+w[i], dpn[j-1]);
      }
    }
  }
  chmax(ans, max(dpe[m], dpn[m]));

  printf("%d\n", ans);
}
0