-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathwine_selling_problem_dp.cpp
57 lines (55 loc) · 1.52 KB
/
wine_selling_problem_dp.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
50
51
52
53
54
55
56
57
/*
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
*/
// Dynamic Programming Approach TC : O(N^2)
// Program Author : Abhisek Kumar Gupta
#include<bits/stdc++.h>
using namespace std;
int find_max_profit(int *A, int n){
int dp[100][100] = {};
int year = n;
for(int i = 0; i < n; i++){
dp[i][i] = year * A[i];
}
year--;
for(int i = 2; i <= n; i++){
int start = 0;
int end = n - i;
while(start <= end){
int end_window = start + i - 1;
int x = A[start] * year + dp[start + 1][end_window];
int y = A[end_window]* year + dp[start][end_window - 1];
dp[start][end_window] = max(x, y);
start++;
}
year--;
}
/* for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << setw(5) << dp[i][j] << " ";
}
cout << "\n";
}
*/
return dp[0][n-1];
}
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, n);
cout << result;
return 0;
}