Help me in solving ONEFROMK problem

My issue

please someone tell me where my code is failing.

My code

#include <iostream>
#include <set>
#define lld long long int 
using namespace std;

int main() {
	// your code goes here
	lld t,x;
	cin>>t;
	while(t--){
	    lld n;
	    set<lld,greater<lld>>s;
	    cin>>n;
	    lld res[n]={0};
	    for(lld i=0; i<n; i++){
	        cin>>x;
	        s.insert(x);
	    }
	    lld count=0;
	    for(auto it=s.begin(); it!=s.end(); it++){
	        if(count==0)
    	        res[count]=*it;
	        else
    	        res[count]=res[count-1]+*it;
    	    count++;
	    }
	    for(lld i=n-1; i>=0; i--){
	        cout<<res[i]<<" ";
	    }
	    s.clear();
	    cout<<endl;
	}
	return 0;
}

Problem Link: ONEFROMK Problem - CodeChef

@pksavetreelife
Your code will only work if your array contains all values different.
If any of the two values get same then it would show wrong output because set contains only distinct values .

1 Like