提交时间:2023-11-18 10:11:28
运行 ID: 111083
#include<bits/stdc++.h> #define int long long using namespace std; int dp[114514][2],a[114514]; signed main(){ int n; cin>>n; for (int i = 1;i <= n;i++){ cin>>a[i]; } memset(dp,-0x7f7f7f7f,sizeof(dp)); dp[1][1] = -a[1]; dp[1][0] = 0; for (int i = 2;i <= n;i++){ dp[i][1] = -a[i]; dp[i][0] = 0; for (int j = 1;j < i;j++){ if (a[i] >= a[j]){ dp[i][0] = max(dp[i][0],dp[j][1] + a[i]); } if (a[i] <= a[j]){ dp[i][1] = max(dp[i][1],dp[j][0] - a[i]); } dp[i][0] = max(dp[i][0],dp[j][0]); dp[i][1] = max(dp[i][1],dp[j][1]); } //cout<<dp[4][1]<<'\n'; } cout<<dp[n][0]; return 0; }