GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

What's wrong with my solution?

Problem Description : Given two sequences a1,a2,...,an (ai is the profit per click of the i-th ad) and b1,b2,...,bn (bi is the average number of clicks per day of the i-th slot), we need to partition them into n pairs (ai,bj) such that the sum of their products is maximized.

My code:

#include <iostream>
#include <algorithm>
#include <iomanip>
#define ll long long
using namespace std;
int main()
{
    
    ll n,a[1001],b[1001],i,j,sum=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        
        cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
        
        cin>>b[i];
    }
    sort(a,a+n);
    sort(b,b+n);
    for(i=0;i<n;i++)
    {
        
        sum+=(a[i]*b[i]);
    }
    cout<<sum<<endl;
}

asked Aug 15, 2016 by Vikram

2 Answers

its knapsack problem.. google it.. u vl get the answer as wel as optimum code..

answered Aug 16, 2016 by anonymous

i don't think it's knapsack problem...by the way my code was accepted. Thanks for your reply :)

answered Aug 16, 2016 by anonymous
...