Buying Sweets Solution || Codechef Starters - 36

      

Buying Sweets Solution 

Codechef Starters - 36
Problem Code : BUYSWEET

Solution :

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

static bool mysort(vector<ll> &a, vector<ll> &b)
{
    return a[2] <= b[2];
}

int main()
{
    ll t = 1;
    cin >> t;
    while (t--)
    {
        ll n, r;
        cin >> n >> r;
        vector<vector<ll>> a(n, vector<ll>(3, 0));
        for (ll i = 0; i < n; i++)
        {
            cin >> a[i][0];
        }
        for (ll i = 0; i < n; i++)
        {
            cin >> a[i][1];
        }
        for (ll i = 0; i < n; i++)
        {
            a[i][2] = a[i][0] - a[i][1];
        }
        sort(a.begin(), a.end(), mysort);

        ll ans = 0;
        for (ll i = 0; i < n; i++)
        {
            while (r && a[i][0] <= r && a[i][2] <= r)
            {
                ll temp = r / a[i][0];
                ans += temp;
                r -= temp * a[i][2];
            }
        }
        cout << ans << "\n";
    }
}

Comments