寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #D - Index × A(Not Continuous ver.)

AtCoder Beginner Contestの過去問から、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#D - Index × A(Not Continuous ver.)

atcoder.jp

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#D - Index × A(Not Continuous ver.)
https://atcoder.jp/contests/abc267/tasks/abc267_d
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
int main(void) {
	ll n, m;
	cin >> n >> m;
	ll inf = -1000000000000000000;
	vector<ll> dp(m + 1, inf);
	dp[0] = 0;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		for (ll j = i; j >= 0; j--) {
			dp[j + 1] = max(dp[j + 1], dp[j] + a * (j + 1));
		}
	}
	cout << dp[m] << endl;
	return 0;
}

AtCoder Beginner Contestは、オンラインジャッジによるプログラミングコンテストです。
日本語と英語に対応していて、週末ごとに実施されているみたいです。
https://practice.contest.atcoder.jp/tutorial
アカウントを登録すれば、誰でも参加できます。