結果

問題 No.1522 Unfairness
コンテスト
ユーザー siman
提出日時 2023-05-05 19:37:47
言語 C++17(clang)
(17.0.6 + boost 1.89.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,092 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,235 ms
コンパイル使用メモリ 148,584 KB
実行使用メモリ 73,832 KB
最終ジャッジ日時 2025-11-29 14:47:35
合計ジャッジ時間 2,598 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:28:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   28 |   ll dp[N][M + 1];
      |            ^~~~~
main.cpp:28:12: note: read of non-const variable 'M' is not allowed in a constant expression
main.cpp:17:10: note: declared here
   17 |   int N, M;
      |          ^
main.cpp:28:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   28 |   ll dp[N][M + 1];
      |         ^
main.cpp:28:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:17:7: note: declared here
   17 |   int N, M;
      |       ^
2 warnings generated.

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  ll dp[N][M + 1];
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  if (A[0] - A[1] <= M) {
    dp[1][A[0] - A[1]] = A[0];
  }

  for (int i = 1; i < N; ++i) {
    for (int j = 0; j <= M; ++j) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);

      if (i - 2 >= 0) {
        int ba = j - (A[i - 1] - A[i]);
        if (ba >= 0 && dp[i - 2][ba] >= 0) {
          dp[i][j] = max(dp[i][j], dp[i - 2][ba] + A[i - 1]);
        }
      }
    }

    /*
    for (int j = 0; j <= M; ++j) {
      cerr << dp[i][j] << " ";
    }
    cerr << endl;
    */
  }

  ll ans = 0;

  for (int m = 0; m <= M; ++m) {
    ans = max(ans, dp[N - 1][m]);
  }

  cout << ans << endl;

  return 0;
}
0