Language: C++

#include<iostream>

const int MAXN = 1010;
int w[MAXN];
bool done[MAXN];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, mini = 100000;
    cin >> n;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        cin >> w[i];
        mp[w[i]] = 0;
        mini = min(mini, w[i]);
    }
    {
        int k = 0;
        for (auto& p : mp) p.second = k++;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (done[i]) continue;
        int cnt = 0, now = i, mi = 100000, sum = 0;
        while (1) {
            if (done[now]) break;
            cnt++;
            done[now] = true;
            mi = min(mi, w[now]);
            sum += w[now];
            now = mp[w[now]];
        }
        int tmp = sum+(cnt-2)*mi;
        tmp = min(tmp, sum+mi+mini*(cnt+1));
        ans += tmp;
    }
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:8:5: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
    8 |     cin.tie(0);
      |     ^~~
      |     std::cin
In file included from a.cc:1:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:9:5: error: 'ios' has not been declared
    9 |     ios::sync_with_stdio(false);
      |     ^~~
a.cc:12:5: error: 'map' was not declared in this scope
   12 |     map<int, int> mp;
      |     ^~~
a.cc:12:9: error: expected primary-expression before 'int'
   12 |     map<int, int> mp;
      |         ^~~
a.cc:15:9: error: 'mp' was not declared in this scope
   15 |         mp[w[i]] = 0;
      |         ^~
a.cc:16:16: error: 'min' was not declared in this scope; did you mean 'std::min'?
   16 |         mini = min(mini, w[i]);
      |                ^~~
      |                std::min
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: 'std::min' declared here
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
a.cc:20:24: error: 'mp' was not declared in this scope; did you mean 'p'?
   20 |         for (auto& p : mp) p.second = k++;
      |                        ^~
      |                        p
a.cc:30:18: error: 'min' was not declared in this scope; did you mean 'std::min'?
   30 |             mi = min(mi, w[now]);
      |                  ^~~
      |                  std::min
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: 'std::min' declared here
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
a.cc:32:19: error: 'mp' was not declared in this scope; did you mean 'mi'?
   32 |             now = mp[w[now]];
      |                   ^~
      |                   mi
a.cc:35:15: error: 'min' was not declared in this scope; did you mean 'std::min'?
   35 |         tmp = min(tmp, sum+mi+mini*(cnt+1));
      |               ^~~
      |               std::min
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: 'std::min' declared here
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
a.cc:38:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   38 |     cout << ans << endl;
      |     ^~~~
      |     std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:38:20: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   38 |     cout << ans << endl;
      |                    ^~~~
      |                    std::endl
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~