제출 #986002

#제출 시각아이디문제언어결과실행 시간메모리
986002LOLOLOSirni (COCI17_sirni)C++17
140 / 140
4259 ms578120 KiB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 2e5 + 10;
const int M = 1e7 + 10;

vector <pair <int, int>> save[M + 1];
int a[N], pos[N], p[N], sz[N], mx[M + 1];

int get(int a) {
    return p[a] ? get(p[a]) : a;
}

bool unite(int a, int b) {
    a = get(a), b = get(b);
    if (a == b)
        return 0;

    if (sz[a] < sz[b])
        swap(a, b);

    sz[a] += sz[b];
    p[b] = a;
    return 1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        sz[i] = 1;
        cin >> a[i];
        mx[a[i]] = i;
    }

    if (a[mx[1]] == 1) {
        cout << 0 << '\n';
        return 0;
    }

    for (int i = M - 2; i >= 0; i--) {
        if (mx[i] == 0) {
            mx[i] = mx[i + 1];
        }
    }

    for (int i = 2; i < M; i++) {
        if (i == a[mx[i]]) {
            int j = i * 2, p = mx[i];
            while (j < M) {
                int nxt = mx[j];
                if (nxt == 0)
                    break;

                save[a[nxt] % i].pb({p, nxt});
                j = (a[nxt] / i + 1) * i;
            }

            if (mx[i + 1]) {
                save[a[mx[i + 1]] % i].pb({p, mx[i + 1]});
            }
        }
    }

    ll ans = 0;

    for (int i = 0; i < M; i++) {
        for (auto x : save[i]) {
            if (unite(x.f, x.s))
                ans += i;
        }
    }

    cout << ans << '\n';

    return 0;
} 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...