Why wont my code/idea work for 1255B

Revision en1, by TheInvadr, 2023-12-02 19:10:55

so fr problem 1255B, im trying to connect every fridge to the ones costing the smallest and second smallest then if need be connecting those 2 to each other.its saying that the 503th fridge in the first testcase in the second test can be opened by anyone ? can anyone explain why ? this is my code -

include <bits/stdc++.h>

using namespace std; int main() { int t; cin>>t; while(t--){ // int n,m; int n,m; cin>>n>>m; pair<int,int> arr[n]; int a[n]; for(int i=0;i<n;i++){ cin>>arr[i].first; arr[i].second=i; } if(n!=m||n==2){ cout<<-1<<endl; continue; } sort(arr,arr+n); vector<pair<int,int>> res; int sum = 0; int cntf = 0; int cnts = 0; for (int i = 2; i < n; i++) {

sum += arr[i].first + arr[0].first;
res.push_back({arr[i].second+1, arr[0].second + 1});
cntf++;

sum += arr[i].first + arr[1].first;
res.push_back({arr[i].second+1, arr[1].second + 1});
cnts++;

} if(cntf<2||cnts<2){ sum+=arr[0].first+arr[1].first; res.push_back({arr[0].second+1,arr[1].second+1}); } cout<<sum<<endl; for(pair<int,int> i : res)cout<<i.first<<" "<<i.second<<endl; } }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English TheInvadr 2023-12-02 19:11:58 1 Tiny change: 'so fr problem ' -> 'so for problem '
en2 English TheInvadr 2023-12-02 19:11:35 16
en1 English TheInvadr 2023-12-02 19:10:55 1397 Initial revision (published)