Codeforces Round #694 (Div. 1) 1470A - Strange Birthday Party Solution

 


Problem Link: https://codeforces.com/problemset/problem/1470/A

Solve in C++:  

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,i,j,t,cs;
    cin>>t;
    while(t--)
    {
        cs=0;
        cin>>n>>m;
        long long k[n],c[m],nc[n];
        for(i=1; i<=n; i++)cin>>k[i];
        for(i=1; i<=m; i++)cin>>c[i];
        for(i=1; i<=n; i++)
        {
            nc[i]=c[k[i]];
        }
        sort(nc+1,nc+n+1);
        j=1;
        for(i=n; i>0; i--)
        {
            if(c[j]<nc[i])
            {
                cs+=c[j];
                j++;
            }
            else
                cs+=nc[i];
        }
        cout<<cs<<endl;
    }
}



No comments

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.