結果

問題 No.3417 Tired Santa
コンテスト
ユーザー shinchan
提出日時 2025-12-18 18:08:35
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 2,732 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,127 ms
コンパイル使用メモリ 284,724 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-23 23:30:28
合計ジャッジ時間 4,289 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(),(v).end()
#define pb emplace_back
#define rep(i, n) for(int i=0;i<(n);i++)
#define foa(e, v) for(auto& e : v)
#define dout(a) cout<<fixed<<setprecision(10)<<a<<'\n';
#define Cout(a) cout<<a<<'\n';

using ll = long long;
using ld = long double;
using Int = __int128;
template <class T> using pqr = priority_queue<T, vector<T>, greater<T>>;

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
    bool compare = a < b;
    if(compare) a = b;
    return compare;
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
    bool compare = a > b;
    if(compare) a = b;
    return compare;
}
template <typename T> inline T back(std::set<T> &s) {
    return *s.rbegin();
}
template <typename T> inline T back(std::multiset<T> &s) {
    return *s.rbegin();
}
template <typename T> inline T pop_back(std::set<T> &s) {
    auto it = prev(s.end());
    T val = *it;
    s.erase(it); 
    return val;
}
template <typename T> inline T pop_back(std::multiset<T> &s) {
    auto it = prev(s.end());
    T val = *it;
    s.erase(it); 
    return val;
}

const int dy[8] = {-1, 0, 0, 1, 1, -1, 1, -1};
const int dx[8] = {0, -1, 1, 0, -1, -1, 1, 1};

const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (3LL << 59);
const int inf = 1 << 30;
const char br = '\n';

void solve() {
    ll n, s; cin >> n >> s;
    assert(n <= 5000 and s <= 1000000 and n >= 1 and s >= 1);
    vector<ll> x(n);
    rep(i, n) cin >> x[i];
    rep(i, n - 1) assert(x[i + 1] > x[i]);
    assert(x[0] > 0 and x.back() <= 1000000);
    assert(count(all(x), s) == 0);
    vector<ll> w(n);
    rep(i, n) cin >> w[i];
    assert(*max_element(all(w)) <= 1000000 and *min_element(all(w)) > 0);
    vector dp(n + 1, vector(2, INF));

    vector<ll> sum(n + 1, 0);
    rep(i, n) sum[i + 1] = sum[i] + w[i];
    for(int l = n - 1; l >= 0; l --) {
        vector nx(n + 1, vector(2, INF));
        rep(k, 2) chmin(dp[l + 1][k], abs(x[l] - s) * sum[n]);
        for(int r = l + 1; r <= n; r ++) {
            ll val = sum[n] - sum[r] + sum[l];
            if(r < n) {
                chmin(dp[r + 1][1], dp[r][0] + val * abs(x[l] - x[r]));
                chmin(dp[r + 1][1], dp[r][1] + val * abs(x[r - 1] - x[r]));
            }
            if(l > 0) {
                chmin(nx[r][0], dp[r][0] + val * abs(x[l] - x[l - 1]));
                chmin(nx[r][0], dp[r][1] + val * abs(x[r - 1] - x[l - 1]));
            }
        }
        if(l) dp = move(nx);
        
    }
    cout << min(dp[n][0], dp[n][1]) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int testcase = 1; 
    // cin >> testcase;
    while(testcase --) solve();

    return 0;
}
0