forked from avijit1258/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb.cpp
More file actions
44 lines (44 loc) · 1.08 KB
/
b.cpp
File metadata and controls
44 lines (44 loc) · 1.08 KB
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
#include<bits/stdc++.h>
using namespace std;
#define MX 1002
int stock_prices[MX], nDay, marked[MX];
int getMaximum(int start, int end){
int ret = -1, mx = 0;
for(int i = start; i <= end; i++){
if(!marked[i] && mx <= stock_prices[i]){
mx = stock_prices[i];
ret = i;
}
}
return ret;
}
int getMinimum(int start, int end){
int ret = -1, mn = 1000000000;
for(int i = start; i <= end; i++){
if(!marked[i] && mn > stock_prices[i]){
mn = stock_prices[i];
ret = i;
}
}
return ret;
}
int getProfit(int start, int end){
int profit = 0, i;
for(i = start; i <= end; i++) marked[i] = 0;
while(1){
int X = getMaximum(start, end);
int Y = getMinimum(start, X - 1);
if(X==-1 || Y==-1)break;
profit += stock_prices[X] - stock_prices[Y];
marked[X] = marked[Y] = 1;
}
return profit;
}
int main(){
scanf("%d", &nDay);
for(int i = 1; i <= nDay; i++)
scanf("%d", &stock_prices[i]);
int profit = getProfit(1, nDay);
printf("%d\n", profit);
return 0;
}