Language: C++

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200200;
int u[maxn];
long long best[maxn], who[maxn];
vector<int> guys[maxn];
void init(){
  for(int i = 0; i < maxn; i++) u[i] = i;
}
int find(int x){
  if(x == u[x]) return x;
  return u[x] = find(u[x]);
}
int main(){
  int n, d; cin >> n >> d;
  vector<int> a(n); for(int & i : a) cin >> i;
  init();
  int comp = n;
  long long ans = 0;
  while(comp != 1){
    for(int i = 0; i < n; i++) guys[i].clear(), best[i] = 1ll<<62, who[i] = -1;
	multiset<pair<long long, int> > smallest;
    for(int i = 0; i < n; i++) {
      int id = find(i);
      for(int v : guys[id]) smallest.erase(smallest.find({-D * v + a[v], v}));
      if(!smallest.empty()){
      	pair<long long, int> myBest = *smallest.begin();
        if(best[id] > myBest.first){
          best[id] = myBest.first;
          who[id] = myBest.second;
        }
      }
      guys[id].push_back(i);
      for(int v : guys[id]) smallest.insert({-D * v + a[v], v});
    }
    for(int i = 0; i < n; i++) if(who[i] != -1){
      int id = find(i), idWho = find(who[i]);
      if(id != idWho){
        ans += best[id];
        u[id] = idWho;
        comp--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
    
  

Compiler output:
a.cc: In function 'int main()':
a.cc:25:60: error: 'D' was not declared in this scope
   25 |       for(int v : guys[id]) smallest.erase(smallest.find({-D * v + a[v], v}));
      |                                                            ^
a.cc:25:57: error: no matching function for call to 'std::multiset<std::pair<long long int, int> >::find(<brace-enclosed initializer list>)'
   25 |       for(int v : guys[id]) smallest.erase(smallest.find({-D * v + a[v], v}));
      |                                            ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/set:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:158,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_multiset.h:787:9: note: candidate: 'template<class _Kt> decltype (std::multiset<_Key, _Compare, _Alloc>::iterator{((std::multiset<_Key, _Compare, _Alloc>*)this)->std::multiset<_Key, _Compare, _Alloc>::_M_t._M_find_tr(__x)}) std::multiset<_Key, _Compare, _Alloc>::find(const _Kt&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >]'
  787 |         find(const _Kt& __x)
      |         ^~~~
/usr/include/c++/14/bits/stl_multiset.h:787:9: note:   template argument deduction/substitution failed:
a.cc:25:57: note:   couldn't deduce template parameter '_Kt'
   25 |       for(int v : guys[id]) smallest.erase(smallest.find({-D * v + a[v], v}));
      |                                            ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:793:9: note: candidate: 'template<class _Kt> decltype (std::multiset<_Key, _Compare, _Alloc>::const_iterator{((const std::multiset<_Key, _Compare, _Alloc>*)this)->std::multiset<_Key, _Compare, _Alloc>::_M_t._M_find_tr(__x)}) std::multiset<_Key, _Compare, _Alloc>::find(const _Kt&) const [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >]'
  793 |         find(const _Kt& __x) const
      |         ^~~~
/usr/include/c++/14/bits/stl_multiset.h:793:9: note:   template argument deduction/substitution failed:
a.cc:25:57: note:   couldn't deduce template parameter '_Kt'
   25 |       for(int v : guys[id]) smallest.erase(smallest.find({-D * v + a[v], v}));
      |                                            ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:777:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; key_type = std::pair<long long int, int>]'
  777 |       find(const key_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_multiset.h:777:28: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::multiset<std::pair<long long int, int> >::key_type&' {aka 'const std::pair<long long int, int>&'}
  777 |       find(const key_type& __x)
      |            ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_multiset.h:781:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::const_iterator std::multiset<_Key, _Compare, _Alloc>::find(const key_type&) const [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; key_type = std::pair<long long int, int>]'
  781 |       find(const key_type& __x) const
      |       ^~~~
/usr/include/c++/14/bits/stl_multiset.h:781:28: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::multiset<std::pair<long long int, int> >::key_type&' {aka 'const std::pair<long long int, int>&'}
  781 |       find(const key_type& __x) const
      |            ~~~~~~~~~~~~~~~~^~~
a.cc:34:47: error: 'D' was not declared in this scope
   34 |       for(int v : guys[id]) smallest.insert({-D * v + a[v], v});
      |                                               ^
a.cc:34:44: error: no matching function for call to 'std::multiset<std::pair<long long int, int> >::insert(<brace-enclosed initializer list>)'
   34 |       for(int v : guys[id]) smallest.insert({-D * v + a[v], v});
      |                             ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:553:9: note: candidate: 'template<class _InputIterator> void std::multiset<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >]'
  553 |         insert(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~
/usr/include/c++/14/bits/stl_multiset.h:553:9: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_multiset.h:504:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; value_type = std::pair<long long int, int>]'
  504 |       insert(const value_type& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_multiset.h:504:32: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::multiset<std::pair<long long int, int> >::value_type&' {aka 'const std::pair<long long int, int>&'}
  504 |       insert(const value_type& __x)
      |              ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_multiset.h:509:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; value_type = std::pair<long long int, int>]'
  509 |       insert(value_type&& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_multiset.h:509:27: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::multiset<std::pair<long long int, int> >::value_type&&' {aka 'std::pair<long long int, int>&&'}
  509 |       insert(value_type&& __x)
      |              ~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_multiset.h:534:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const_iterator, const value_type&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; value_type = std::pair<long long int, int>]'
  534 |       insert(const_iterator __position, const value_type& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_multiset.h:534:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_multiset.h:539:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const_iterator, value_type&&) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >; iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; const_iterator = std::_Rb_tree<std::pair<long long int, int>, std::pair<long long int, int>, std::_Identity<std::pair<long long int, int> >, std::less<std::pair<long long int, int> >, std::allocator<std::pair<long long int, int> > >::const_iterator; value_type = std::pair<long long int, int>]'
  539 |       insert(const_iterator __position, value_type&& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_multiset.h:539:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_multiset.h:565:7: note: candidate: 'void std::multiset<_Key, _Compare, _Alloc>::insert(std::initializer_list<_Tp>) [with _Key = std::pair<long long int, int>; _Compare = std::less<std::pair<long long int, int> >; _Alloc = std::allocator<std::pair<long long int, int> >]'
  565 |       insert(initializer_list<value_type> __l)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_multiset.h:565:43: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::initializer_list<std::pair<long long int, int> >'
  565 |       insert(initializer_list