题解 | #买卖股票的最好时机(二)#
买卖股票的最好时机(二)
http://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281
#include<iostream>
#include<algorithm>
using namespace std;
int price[100000],dp[100000] = {0};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0;i < n;++i)
cin >> price[i];
for(int i = 1;i < n;++i){
int sum = price[i] - price[i - 1];
if(sum > 0)
dp[i] = dp[i - 1] + sum;
else
dp[i] = dp[i - 1];
}
cout << dp[n - 1];
}