結果

問題 No.2390 Udon Coupon (Hard)
ユーザー そすうぽよそすうぽよ
提出日時 2023-07-21 22:50:55
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 456 ms / 2,000 ms
コード長 2,027 bytes
コンパイル時間 2,197 ms
コンパイル使用メモリ 206,796 KB
実行使用メモリ 59,592 KB
最終ジャッジ日時 2024-10-06 01:54:33
合計ジャッジ時間 6,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <variant>

#define rep2(i, k, n) for (i64 i = (i64)(k); i < (i64)(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(x) begin(x), end(x)
#ifdef ENV_LOCAL
#define dump \
  if (1) cerr
#else
#define dump \
  if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

// using namespace harudake;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 n;
  cin >> n;
  vector<pair<i64, i64>> vp;
  rep(i, 3) {
    i64 a, b;
    cin >> a >> b;
    vp.emplace_back(a, b);
  }
  i64 m = vp[0].first * vp[1].first * 2;
  vi64 dp1(m + 1);
  rep(i, 2) {
    rep(j, m + 1) {
      i64 jj = j + vp[i].first;
      if (jj > m) break;
      dp1[jj] = max(dp1[jj], dp1[j] + vp[i].second);
    }
  }
  rep(i, m) dp1[i + 1] = max(dp1[i + 1], dp1[i]);
  i64 mm = vp[0].first * vp[1].first;
  i64 ans = 0;
  rep(i, mm) {
    i64 rem = n - vp[2].first * i;
    if (rem < 0) break;
    i64 q = rem / mm;
    i64 r = rem % mm;
    // cerr << q << " " << r << endl;
    if (q == 0) {
      ans = max(ans, dp1[r] + vp[2].second * i);
      continue;
    }
    i64 triple = mm * vp[2].first;
    i64 diff = mm * vp[2].second - dp1[mm] * vp[2].first;
    // cerr << diff << endl;
    i64 x = q / vp[2].first;
    i64 rem2 = rem - triple * x;
    i64 q2 = rem2 / mm;
    // cerr << x << " " << rem2 << " " << q2 << endl;
    if (q2 == 0) {
      ans = max(ans, dp1[r] + x * mm * vp[2].second + vp[2].second * i);
      if (x > 0) {
        ans = max(ans, dp1[mm + r] + dp1[mm] * (vp[2].first - 1) +
                           (x - 1) * mm * vp[2].second + vp[2].second * i);
      }
    } else {
      ans = max(ans, dp1[mm + r] + dp1[mm] * (q2 - 1) + x * mm * vp[2].second +
                         vp[2].second * i);
    }
    ans = max(ans, dp1[r + mm] + dp1[mm] * (q - 1) + vp[2].second * i);
  }
  cout << ans << endl;
  return 0;
}
0