-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathwine_selling_problem_recursive.cpp
36 lines (34 loc) · 1.08 KB
/
wine_selling_problem_recursive.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
/*
Given n wines in a row, with integers denoting the cost of each wine respectively.
Each year you can sale the first or the last wine in the row. However, the price
of wines increases over time. Let the initial profits from the wines be P1, P2, P3…Pn.
On the Yth year, the profit from the ith wine will be Y*Pi.
Calculate the maximum profit from all the wines.
Input : 5
: 2 4 6 2 5
Output : 64
*/
// Recursive Approach TC : O(2^n)
// Program Author : Abhisek Kumar Gupta
#include<bits/stdc++.h>
using namespace std;
int find_max_profit(int *A, int start, int end, int year){
if(start > end)
return 0;
int r1 = A[start] * year + find_max_profit(A, start + 1, end, year + 1);
int r2 = A[end] * year + find_max_profit(A, start, end - 1, year + 1);
return max(r1, r2);
}
int main(){
int n;
cin >> n;
int *A;
for(int i = 0; i < n; i++)
cin >> A[i];
int start = 0;
int end = n - 1;
int year = 1;
int result = find_max_profit(A, start, end, year);
cout << result;
return 0;
}