-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcandies_dp.cpp
More file actions
44 lines (36 loc) · 789 Bytes
/
candies_dp.cpp
File metadata and controls
44 lines (36 loc) · 789 Bytes
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;
long long candies(int n, vector <int> arr) {
// Complete this function
vector<long long> dp(n);
dp[0] = 1;
for(int i = 1; i < n; i++)
dp[i] = 1;
for(int i = 1; i < n; i++)
{
if(arr[i] > arr[i-1])
dp[i] = dp[i-1] + 1;
}
for(int i = n-2 ; i >= 0 ; i--)
{
if(arr[i] > arr[i+1])
{
dp[i] = max(dp[i],dp[i+1] + 1);
}
}
long long sum = 0;
for(int i = 0; i < n; i++)
sum += dp[i];
return sum;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int arr_i = 0; arr_i < n; arr_i++){
cin >> arr[arr_i];
}
long long result = candies(n, arr);
cout << result << endl;
return 0;
}