1470A - Strange Birthday Party - CodeForces Solution


binary search dp greedy sortings two pointers *1300

Please click on ads to support us..

Python Code:

from collections import Counter
tests = int(input())

for test in range(tests):
   n_fre,n_pre= tuple(map(int,input().split(" ")))
   fre_k = list(map(int,input().split(" ")))
   cost = list(map(int,input().split(" ")))
   
   fr_k_cost = []
   for i,k in enumerate(fre_k):
      fr_k_cost.append([cost[k-1],k])

   fr_k_cost.sort(key = lambda x: -x[0])
   dolars = 0
   j = 1
   for pair in fr_k_cost:
      if  j <= pair[1] and  pair[0] > cost[j-1] :
         d = cost[j-1]
         j +=1
      else: d =  pair[0]
      dolars += d
      
   print(dolars)

C++ Code:

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

void solve(){
    ll n,m;
    cin>>n>>m;
    vector <ll> v1(n);
    vector <ll> v2(m);
    vector <ll> ans(m);
    for(ll i=0;i<n;i++)
    {
        cin>>v1[i]; 
        ans[v1[i]-1]++;
    }
    for(ll i=0;i<m;i++)
    {
        cin>>v2[i];
    }
    ll j=0;
    ll k=m-1;
    ll sum=0;
    for(ll i=0;i<n;i++)
    {
        if(ans[k]==0)
        {
            k--;
            while(1)
            {
                if(ans[k]!=0)
                {
                    break;
                }
                k--;
            }
        }
        if(ans[k]>0)
        {
            if(v2[j]<v2[k])
            {
                sum+=v2[j];
                j++;
            }
            else{
                sum+=v2[k];
            }
            ans[k]--;
        }
    }
    cout<<sum<<endl;
}

int main()                                                                  
{
    ios_base::sync_with_stdio(false);
    ll t = 1;
    cin>>t;
    while(t--)
    {
        solve();
    }   
    return 0;
}


Comments

Submit
0 Comments
More Questions

841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String