One day a thief plans to rob PQR bank. All the money in the bank is kept in a secret room. The secret room has N vaults arranged in a circle and the amount of money inside each vault will be displayed on the vault door. An alarm sounds If the thief attempt to break more than 2 of any 5 adjacent vaults and thief will be caught. Calculate the maximum amount of money that he can rob without sounding the alarm. Note: The vaults are arranged in a circle, the last vault is adjacent to the first one. Input Format: The first line contains an integer N which is the number of vaults. The next line has a sequence of positive integers of length N, giving the amount of cash in the vaults in order. Output Format: The maximum amount of money that the thief can rob without sounding the alarm. Constraints: N<=25, Amount in each vault <=50000 Sample Input 1: 9 1000 2000 1000 5000 9000 5000 3000 4000 1000 Sample Output 1: 15000 Explanation: The vaults 1, 5, 6 are looted, giving a total loot of (1000+5000+9000) = 15000 Sample Input 2: 10 1000 2000 3000 5000 9000 7000 6000 4000 7000 5000 Sample Output 2: 26000 Explanation: One way of getting the maximum amount is to loot vaults 4, 5, 9 and 10 giving a total of 26000. Hence the output is 26000. Note that no 5 adjacent vaults have more than 2 looted. give the code in c++
Question
One day a thief plans to rob PQR bank. All the money in the bank is kept in a secret room. The secret room has N vaults arranged in a circle and the amount of money inside each vault will be displayed on the vault door. An alarm sounds If the thief attempt to break more than 2 of any 5 adjacent vaults and thief will be caught. Calculate the maximum amount of money that he can rob without sounding the alarm.
Note: The vaults are arranged in a circle, the last vault is adjacent to the first one.
Input Format:
The first line contains an integer N which is the number of vaults. The next line has a sequence of positive integers of length N, giving the amount of cash in the vaults in order.
Output Format:
The maximum amount of money that the thief can rob without sounding the alarm.
Constraints:
N<=25, Amount in each vault <=50000
Sample Input 1:
9
1000 2000 1000 5000 9000 5000 3000 4000 1000
Sample Output 1:
15000
Explanation:
The vaults 1, 5, 6 are looted, giving a total loot of (1000+5000+9000) = 15000
Sample Input 2:
10
1000 2000 3000 5000 9000 7000 6000 4000 7000 5000
Sample Output 2:
26000
Explanation:
One way of getting the maximum amount is to loot vaults 4, 5, 9 and 10 giving a total of 26000. Hence the output is 26000. Note that no 5 adjacent vaults have more than 2 looted.
give the code in c++
Solution
Here is a C++ solution for the problem using dynamic programming:
#include<bits/stdc++.h>
using namespace std;
int dp[25][25][5][5];
int solve(int l, int r, int a, int b, vector<int>& arr) {
if (l > r) return 0;
if (dp[l][r][a][b] != -1) return dp[l][r][a][b];
int res = 0;
for (int i = 0; i < 3; ++i) {
if (a + i > 2) break;
for (int j = 0; j < 3; ++j) {
if (b + j > 2 || i + j > 2) break;
res = max(res, solve(l + 1, r - j, a + i, max(0, j - 1), arr) + i * arr[l] + j * arr[r]);
}
}
return dp[l][r][a][b] = res;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
memset(dp, -1, sizeof(dp));
int ans = 0;
for (int i = 0; i < n; ++i) {
rotate(arr.begin(), arr.begin() + 1, arr.end());
ans = max(ans, solve(0, n - 1, 0, 0, arr));
}
cout << ans << endl;
return 0;
}
This code works by iterating over all possible states and calculating the maximum amount of money that can be robbed for each state. The states are defined by the left and right boundaries of the vaults that are being considered, and the number of vaults that have been robbed on the left and right sides. The dynamic programming array dp is used to store the maximum amount of money that can be robbed for each state, and the solve function is used to calculate this value recursively. The rotate function is used to consider all possible starting points for the robbery, and the maximum amount of money that can be robbed is updated for each starting point.
Similar Questions
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. Example 1:Input: nums = [1,2,3,1]Output: 4Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).Total amount you can rob = 1 + 3 = 4.
A Dubai based gangster Chhota Vakil is in Switzerland. He wants to rob a bank there, whose locker code according to his information is an odd number between 50 and 450. He also knows that the numbers are from the set: 0, 1, 2, 3, 4, 5}. How many maximum trials he has to take to unlock the locker?
A Dubai based gangster Chhota Vakil is in Switzerland. He wants to rob a bank there, whose locker code according to his information is an odd number between 50 and 450. He also knows that the numbers are from the set: 0, 1, 2, 3, 4, 5}. How many maximum trials he has to take to unlock the locker?Choices:- 54 72 78 106
For safety reasons, four different alarm systems were installed in the vault containing the safety deposit boxes at a Beverly Hills bank. Each of the four systems detects theft with a probability of 0.99independently of the others.What is the probability that when a theft occurs, all four systems will detect it? (0.99)4 (0.99) * 4 (0.01)4 4 * (0.01) * (0.99)3 4 * (0.99) * (0.01)3
A person invests 1000 dollars in a bank. The bank pays 6.75% interest compounded monthly. To the nearest tenth of a year, how long must the person leave the money in the bank until it reaches 2500 dollars?A, equals, P, left bracket, 1, plus, start fraction, r, divided by, n, end fraction, right bracket, start superscript, n, t, end superscriptA=P(1+ nr ) nt
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.