Language: C++

// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>

using namespace std;
#define int long long
const int N = (int) 1e5 + 5, mod = (int) 0;
int a[N], cnt[N];
vector<int> p[N];
int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    int res = 0;
    int z = 1e17;
    while (z > 1) {
        int mx = 0;
        for (int i = 0; i < n; ++i) {
            a[i] %= z;
        }
        for (int i = 0; i < 10; ++i)
            p[i].clear();
        for (int i = 0; i < n; ++i)
            p[a[i] * 10 / z].push_back(a[i]);
        vector<int> all[10];
        for (int d = 0; d < 10; ++d) {
            if (p[d].size() > 0)
                mx = max(mx, d);
            sort(p[d].begin(), p[d].end());
            for (int i = 0; i < 10; ++i) cnt[i] = 0;
            for (int i = 0; i < 4; ++i) {
                int c = 0, cur = 0;
                if (i > 2) {
                    random_shuffle(p[d].begin(), p[d].end());
                }
                for (int x : p[d]) {
                    if (i > 2 || c >= i)
                        cur = (cur + x) % z;
          //          if (z == 10 && d == 3 && cur == 7) {
            //            cout << z << ' ' << x << ' ' << "wtf?" << endl;
              //      }
                    c++;
                    if (cnt[cur * 10 / z] < 4) {
                        cnt[cur * 10 / z]++;
                        all[d].push_back(cur);
                      //  if (z <= 1000) cout << cur << endl;
                        mx = max(mx, cur * 10 / z);
                    }
                }
            }
        }
        int ss = 0;
        for (int i = 0; i < n; ++i)
            ss = (ss + a[i]) % z;
        mx = max(mx, ss * 10 / z);
        for (int i = 0; i < n; ++i)
            mx = max(((ss - a[i] + z) % z) * 10 / z);
        for (int i = 0; i < 10; ++i) {
            if (i == 0) {
                sort(a, a + n);
            } else if (i == 1) {
                reverse(a, a + n);
            } else {
                random_shuffle(a, a + n);
            }
            int cur = 0;
            for (int j = 0; j < n; ++j)
                cur = (cur + a[j]) % z, mx = max(mx, cur * 10 / z);
        }
        for (int x0 = 0; x0 < 10; ++x0)
            for (int x1 = x0 + 1; x1 < 10; ++x1) {
                for (int ax : all[x0]) for (int bx : all[x1]) mx = max(mx, ((ax + bx) % z) * 10 / z);
                for (int x2 = x1 + 1; x2 < 10; ++x2) {
                    for (int ax : all[x0])
                        for (int bx : all[x1])
                            for (int cx : all[x2])
                                mx = max(mx, ((ax + bx + cx) % z) * 10 / z);
                }
            }
        /*
        for (int i = 0; i < m; ++i)
            for (int j = i + 1; j < m; ++j) if (all[i].second != all[j].second) {
                int sum = (all[i].first + all[j].first) % z;
                mx = max(mx, sum * 10 / z);
         //       if (z <= 10 && sum * 10 / z == 8) cout << " huuu " << sum * 10 / z << endl;
                for (int k = j + 1; k < m; ++k) if (all[i].second != all[k].second && all[j].second != all[k].second) {
                    sum = (all[i].first + all[j].first + all[k].first) % z;
                    mx = max(mx, sum * 10 / z);
           //         if (z <= 10 && sum * 10 / z == 8) {
             //           cout << sum * 10 / z << endl;
               //         cout << all[i].second << ' ' << all[j].second << ' ' << all[k].second << endl;
                 //   }
                }
            }*/
 //       if (mx) cout << " hi " << mx << endl;
        res += mx; 
        z /= 10;

    }
    cout << res << endl;
}



Compiler output:
a.cc: In function 'int32_t main()':
a.cc:51:35: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<long long int*, vector<long long int> >]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
   51 |                     random_shuffle(p[d].begin(), p[d].end());
      |                     ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h:4492:5: note: declared here
 4492 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~
a.cc:74:21: error: no matching function for call to 'max(long long int)'
   74 |             mx = max(((ss - a[i] + z) % z) * 10 / z);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   template argument deduction/substitution failed:
a.cc:74:21: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   74 |             mx = max(((ss - a[i] + z) % z) * 10 / z);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 1 provided
a.cc:81:31: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = long long int*]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
   81 |                 random_shuffle(a, a + n);
      |                 ~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4492:5: note: declared here
 4492 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~