My issue
It is showing that 1 is correct 1 is WA,
I created a separate vector consisting of the song with same language and then sorted it and reversed it to find the maximum length of the playlist:
until k is reached!
My code
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
void solve()
{
int n, k, l;
cin >> n >> k >> l;
int m[n], L[n];
for (int i = 0; i < n; i++)
{
cin >> m[i] >> L[i];
}
vector<int> len;
int size{0};
for (int i = 0; i < n; i++)
{
if (L[i] == l)
{
len.push_back(m[i]);
size++;
}
}
if (size != 0)
{
int max_length = 0;
int term = 0;
sort(len.begin(), len.end());
for (int i = len.size() - 1; i >= 0; i--)
{
term++;
if (term > k)
{
break;
}
else
{
max_length += len.at(i);
}
}
cout << max_length << endl;
}else if(size==0){
cout<<"-1"<<endl;
}
}
int main()
{
int n;
cin >> n;
while (n--)
{
solve();
}
}
Problem Link: DOTIFYPLAY Problem - CodeChef