結果

問題 No.1163 I want to be a high achiever
ユーザー tnakao0123
提出日時 2020-08-13 20:38:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,382 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 95,432 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-09 19:32:24
合計ジャッジ時間 1,948 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1163.cc:  No.1163 I want to be a high achiever - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 500;
const int MAX_A = 100;
const int MAX_K = MAX_N * MAX_A;

/* typedef */

/* global variables */

int as[MAX_N], bs[MAX_N];
int dp[MAX_K + 1];

/* subroutines */

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  for (int i = 0; i < n; i++) scanf("%d", as + i);
  for (int i = 0; i < n; i++) scanf("%d", bs + i);

  int m = 0, k = 0, bsum = 0;
  for (int i = 0; i < n; i++) {
    int d = as[i] - x;
    if (d >= 0) k += d;
    else {
      as[m] = -d, bs[m] = bs[i];
      bsum += bs[i];
      m++;
    }
  }

  if (m == n) {
    puts("-1");
    return 0;
  }

  memset(dp, -1, sizeof(dp));
  dp[k] = 0;

  for (int i = 0; i < m; i++)
    for (int j = as[i]; j <= k; j++)
      if (dp[j] >= 0) setmax(dp[j - as[i]], dp[j] + bs[i]);

  int maxd = 0;
  for (int j = 0; j <= k; j++) setmax(maxd, dp[j]);

  printf("%d\n", bsum - maxd);
  return 0;
}
0