結果

問題 No.2488 Mod Sum Maximization
ユーザー square1001square1001
提出日時 2023-09-29 21:16:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 1,909 ms
コンパイル使用メモリ 204,164 KB
実行使用メモリ 14,824 KB
最終ジャッジ日時 2023-09-29 21:16:18
合計ジャッジ時間 11,002 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 396 ms
14,700 KB
testcase_04 AC 363 ms
14,720 KB
testcase_05 AC 376 ms
14,648 KB
testcase_06 AC 375 ms
14,484 KB
testcase_07 AC 243 ms
9,540 KB
testcase_08 AC 354 ms
14,824 KB
testcase_09 AC 260 ms
9,820 KB
testcase_10 AC 196 ms
9,028 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 175 ms
8,780 KB
testcase_13 AC 149 ms
6,392 KB
testcase_14 AC 32 ms
4,380 KB
testcase_15 AC 213 ms
9,236 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 59 ms
4,752 KB
testcase_18 AC 16 ms
4,376 KB
testcase_19 AC 284 ms
10,084 KB
testcase_20 AC 342 ms
14,528 KB
testcase_21 AC 306 ms
10,032 KB
testcase_22 AC 349 ms
14,692 KB
testcase_23 AC 377 ms
14,720 KB
testcase_24 AC 122 ms
6,544 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 15 ms
4,376 KB
testcase_27 AC 303 ms
8,984 KB
testcase_28 AC 291 ms
9,028 KB
testcase_29 AC 243 ms
9,140 KB
testcase_30 AC 179 ms
6,136 KB
testcase_31 AC 26 ms
4,376 KB
testcase_32 AC 452 ms
9,756 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 497 ms
14,692 KB
testcase_35 AC 136 ms
6,228 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0