-
Notifications
You must be signed in to change notification settings - Fork 0
/
100049. Beautiful Towers I.cpp
40 lines (35 loc) · 1.01 KB
/
100049. Beautiful Towers I.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
class Solution {
public:
long long maximumSumOfHeights(vector<int>& maxHeights) {
long long maxi=-1;
long long maxiIndex=-1;
for(int i=0;i<maxHeights.size();i++){
if(maxi<maxHeights[i]){
maxi=maxHeights[i];
maxiIndex=i;
}
}
long long ans=0;
for(int i=0;i<maxHeights.size();i++){
int prev=maxi;
long long sum=0;
for(int j=i;j<maxHeights.size();j++){
if(prev<=maxHeights[j]) sum+=prev;
else {
prev=maxHeights[j];
sum+=maxHeights[j];
}
}
prev=maxi;
for(int j=i-1;j>=0;j--){
if(prev<=maxHeights[j]) sum+=prev;
else {
prev=maxHeights[j];
sum+=maxHeights[j];
}
}
ans=max(ans,sum);
}
return ans;
}
};