結果

問題 No.324 落ちてた閉路グラフ
コンテスト
ユーザー yuppe19 😺
提出日時 2015-12-17 09:52:17
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 651 ms / 5,000 ms
コード長 1,290 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,718 ms
コンパイル使用メモリ 182,568 KB
実行使用メモリ 246,400 KB
最終ジャッジ日時 2026-03-08 16:05:07
合計ジャッジ時間 6,360 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:38:32: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |   for(int i : range(n)) { scanf("%d", &w[i]); }
      |                           ~~~~~^~~~~~~~~~~~~

ソースコード

diff #
raw source code

// (?ω?)<MLEかな?
#include <iostream>
#include <algorithm>
using namespace std;
using i64 = long long;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};

const int inf = 987654321;
int n, m;
vector<int> w;
vector<vector<vector<vector<int>>>> dp;

// i番目まで見た。j個の頂点を取った。before: 前を取った? first: 最初を取った?
// 最大の価値を返す。
int rec(int i, int j, bool before, bool first) {
  int &res = dp[before][first][i][j];
  if(i==n) {
    if(j==m) {
      return res = (before&&first ? w[i-1] : 0);
    } else {
      return res = -inf;
    }
  }
  if(res != -inf+1) { return res; }
  res = rec(i+1, j, false, first);
  if(j < m) {
    res = max(res, rec(i+1, j+1, true, first) + (before ? w[i-1] : 0));
  }
  return res;
}

int main(void) {
  scanf("%d%d", &n, &m);
  w.assign(n, 0);
  dp.assign(2, vector<vector<vector<int>>>(2, vector<vector<int>>(n+1, vector<int>(m+1, -inf+1))));
  for(int i : range(n)) { scanf("%d", &w[i]); }
  int res = max(rec(1, 1, true, true), rec(1, 0, false, false));
  printf("%d\n", res);
  return 0;
}
0