結果

問題 No.1522 Unfairness
ユーザー siman
提出日時 2023-05-05 19:37:47
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 1,252 ms
コンパイル使用メモリ 143,652 KB
実行使用メモリ 73,856 KB
最終ジャッジ日時 2024-11-23 04:02:40
合計ジャッジ時間 2,624 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#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