結果
問題 | No.2488 Mod Sum Maximization |
ユーザー |
![]() |
提出日時 | 2023-09-29 21:16:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 638 ms / 2,000 ms |
コード長 | 1,469 bytes |
コンパイル時間 | 2,145 ms |
コンパイル使用メモリ | 198,268 KB |
最終ジャッジ日時 | 2025-02-17 03:04:49 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h> using namespace std; const long long INF = (3LL << 59); class segtree { private: int sz; vector<long long> val; public: segtree() : sz(0), val(vector<long long>()) {} segtree(int n) : sz(n >= 3 ? 1 << (32 - __builtin_clz(n)) : n), val(vector<long long>(sz * 2, -INF)) {} void update(int pos, long long x) { pos += sz; val[pos] = x; while (pos > 1) { pos /= 2; val[pos] = max(val[pos * 2], val[pos * 2 + 1]); } } long long rangemax(int l, int r) const { l += sz; r += sz; long long ans = -INF; while (l != r) { if (l & 1) ans = max(ans, val[l++]); if (r & 1) ans = max(ans, val[--r]); l /= 2; r /= 2; } return ans; } }; int main() { // step #1. input cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } // step #2. calculate sum long long suma = 0; for (int i = 0; i < N; i++) { suma += A[i]; } // step #3. dynamic programming vector<long long> dp(N, -INF); segtree seg(N); dp[N - 1] = 0; seg.update(N - 1, dp[N - 1]); for (int i = N - 2; i >= 0; i--) { int posl = i + 1; for (int j = A[i]; j <= A[N - 1]; j += A[i]) { int posr = lower_bound(A.begin(), A.end(), j + A[i]) - A.begin(); long long res = seg.rangemax(posl, posr); dp[i] = max(dp[i], res - j); posl = posr; } seg.update(i, dp[i]); } // step #4. output long long ans = suma + dp[0]; cout << ans << endl; return 0; }