Editorial for 仁慈的導師3
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
參考code
#include<bits/stdc++.h>
#define ll0 (ll)0
using namespace std;
typedef long long ll;
int main() {
ll n; cin >> n;
ll sum = 0;
vector<ll> dp(n), v(n);
for(int i = 0;i < n;i++) {
cin >> v[i];
sum += v[i];
}
if(n == 1) {
cout << 0;
return 0;
}else if(n == 2) {
cout << min(v[0], v[1]);
return 0;
}else if(n == 3) {
cout << min(v[0] + v[2], v[1]);
return 0;
}
dp[0] = v[0];
dp[1] = v[1];
dp[2] = max(ll0, v[0]) + v[2];
ll ans = max({dp[0], dp[1], dp[2]});
for(int i = 3;i < n;i++) {
dp[i] = max(dp[i-2], dp[i-3]) + v[i];
ans = max(ans, dp[i]);
}
cout << sum-ans;
return 0;
}
Comments