-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buy_and_sell_stock_with_cooldown.cpp
49 lines (45 loc) · 1.13 KB
/
Buy_and_sell_stock_with_cooldown.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <unordered_map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <climits>
#include <utility>
using namespace std;
#define mp make_pair
#define INFI 10e8
#define INF 10e7
#define mod 1000000007
#define sieve_limit 10e6
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<long long int> vlli;
typedef vector<bool> vb;
typedef vector<vector<int> > vvi;
typedef vector<vector<long long> > vvll;
typedef vector<vector<long long int> > vvlli;
typedef vector<vector<bool> > vvb;
int main(){
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vi v(n);
vvi dp(n, vi(3, 0));
for(int i = 0; i < n; i++){
cin >> v[i];
}
dp[0][0] -= v[0], dp[0][1] = 0, dp[0][2] = 0;
for(int i = 1; i < n; i++){
dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - v[i]);
dp[i][1] = max(dp[i - 1][1], v[i] + dp[i - 1][0]);
dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]);
}
cout << dp[n - 1][1] << '\n';
}