Why this solution is not working for this B problem ?

Revision en1, by pranshu_2004, 2025-07-11 22:07:22

https://mirror.codeforces.com/contest/2022/problem/B Can anybody tell me that why my sol is not working for above problem

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
    int t;
    cin >> t;
    while (t--)
    {
        ll n,x,temp,ans=0;
        cin >> n >> x;
        multiset<ll> s;
        for (int i = 0; i < n; i++)
        {
            cin >> temp;
            s.insert(temp);
        }
        while (s.size()>x)
        {
            vector<ll> p;
            ll j=x,minm=*s.rbegin();
            while (j)
            {
              auto it=s.end();
              it--;
                minm=*it;
                p.push_back(minm);
                s.erase(it);
                j--;
            }
            
            for (int i = 0; i < p.size(); i++)
            {
              if(p[i]-minm!=0)
                s.insert(p[i]-minm);
            }
            ans+=minm;
        }
        if (s.size())
        {
            ans+=*s.rbegin();
        }
        cout << ans << endl;
    }
    return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English pranshu_2004 2025-07-11 22:07:22 1172 Initial revision (published)