- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to find out the maximum amount of money that can be made from selling cars
Suppose, there is a demand for red and blue cars for sale. An automobile company decides to sell p red cars and q blue cars of different prices. Currently, the company has 'a' number of red cars, 'b' number of blue cars, and 'c' numbers of colorless cars (the cars are yet to be painted) in their stock. The values of the different cars are given in arrays A, B, and C. So, the company has to sell p + q number of cars in a day and they have to make maximum profit from them. The colorless cars can be painted in any color, red or blue. We find out the maximum amount of profit that can be achieved from selling the cars.
So, if the input is like p = 3, q = 3, a = 3, b = 3, c = 2, A = {150000, 200000, 200000}, B = {150000, 120000, 180000}, C = {210000, 160000, 150000}, then the output will be 1100000.
The company can sell blue cars of value 200000, 200000 and paint the car of value 210000 to blue. Total value obtained from selling blue cars will be 610000. Also, they can sell red car of value 18000 and paint cars of value 160000 and 150000 to gain a total of 490000. The total profit value obtained will be 610000 + 490000 = 1100000.
To solve this, we will follow these steps −
Define an array dp sort the arrays A, B, and C for initialize i := 0, when i < p, update (increase i by 1), do: insert A[i] at the end of dp for initialize i := 0, when i < q, update (increase i by 1), do: insert B[i] at the end of dp sort the array dp reverse the array dp for initialize i := 1, when i < size of dp, update (increase i by 1), do: dp[i] := dp[i] + dp[i - 1] tmp := 0 res := last element of dp for initialize i := 1, when i < (minimum of (c and p +q), update (increase i by 1), do: tmp := tmp + C[i - 1] res := maximum of (res and dp[p + q - i] + tmp) return res
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int p, int q, int a, int b, int c, vector<int> A, vector<int> B, vector<int> C){ vector<int> dp(1, 0); sort(A.rbegin(), A.rend()); sort(B.rbegin(), B.rend()); sort(C.rbegin(), C.rend()); for(int i = 0; i < p; ++i) dp.push_back(A[i]); for(int i = 0; i < q; ++i) dp.push_back(B[i]); sort(dp.begin(), dp.end()); reverse(dp.begin() + 1, dp.end()); for(int i = 1; i < (int)dp.size(); ++i) dp[i] += dp[i - 1]; int tmp = 0; int res = dp.back(); for(int i = 1; i <= min(c, p + q); ++i) { tmp += C[i - 1]; res = max(res, dp[p + q - i] + tmp); } return res; } int main() { int p = 3, q = 3, a = 3, b = 3, c = 2; vector<int> A = {150000, 200000, 200000}, B = {150000, 120000, 180000}, C = {210000, 160000, 150000}; cout<< solve(p, q, a, b, c, A, B, C); return 0; }
Input
3, 3, 3, 3, 2, {150000, 200000, 200000}, {150000, 120000, 180000}, {210000, 160000, 150000}
Output
1100000
- Related Articles
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- C++ program to find out the number of coordinate pairs that can be made
- C++ program to find out the maximum number of cells that can be illuminated
- C++ Program to find out the least amount of money required to subscribe to the OTT services
- C++ Program to find out if a palindromic matrix can be made from a given matrix
- Maximum money that can be withdrawn in two steps in C
- C++ code to find out the total amount of sales we made
- Python program to find how much money will be earned by selling shoes
- Program to Find Out the Amount of Rain to be Caught between the Valleys in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Maximum elements that can be made equal with k updates in C++
- C++ code to count maximum groups can be made
- Program to find maximum number of package that can be bought by buyers in C++
- C++ program to find out the maximum possible tally from given integers
