Task: A thief wants to steal as much as gold coins from gold mine. In the gold mine N number of gold boxes are there, in every box consisting of Ai gold plates, with consisting of Aj gold coins in each plate. Find the maximum number of gold coins can thief steal.
Example:
Input:
1
3 // Number of gold plates Thief can steal.
3 // Gold Boxes
1 3 2 2 3 1 // Gold plates and same number of Gold Coins on each plate.
Output:
7
7
Here is the Code:
Program in C++:
- #include <iostream>
- using namespace std;
- int maxPosition(int n,int *a)
- {
- int p,max=0;
- for(int i=1;i<n;i=i+2)
- {
- if(max<a[i])
- {
- max=a[i];
- p=i;
- }
- }
- return p;
- }
- int main() {
- int t;
- scanf("%d\n",&t);
- while(t--)
- {
- int nT;
- scanf("%d\n",&nT);
- int n;
- scanf("%d\n",&n);
- int a[2*n];
- for(int i=0;i<2*n;i++)
- {
- scanf("%d\n",&a[i]);
- }
- int p,gc=0;
- while(nT!=0)
- {
- p=maxPosition(2*n,a);
- if(nT-a[p-1]>=0)
- {
- gc+=a[p-1]*a[p];
- a[p]=0;
- nT-=a[p-1];
- }
- else
- {
- gc+=nT*a[p];
- nT=0;
- }
- }
- cout<<gc<<endl;
- }
- return 0;
- }
Here is the Video: