class Euler21 {

    static class Options {
        int limit = 10000;
        boolean runCheckpoints = true;
    }

    static boolean parseIntAfterPrefix(String arg, String prefix, Options options) {
        if (!arg.startsWith(prefix)) {
            return false;
        }
        String tail = arg.substring(prefix.length());
        if (tail.isEmpty()) {
            return false;
        }

        long parsed = 0;
        for (int i = 0; i < tail.length(); i++) {
            char c = tail.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
            parsed = parsed * 10 + (c - '0');
            if (parsed > Integer.MAX_VALUE) {
                return false;
            }
        }
        options.limit = (int) parsed;
        return true;
    }

    static boolean parseArguments(String[] args, Options options) {
        for (String arg : args) {
            if ("--skip-checkpoints".equals(arg)) {
                options.runCheckpoints = false;
                continue;
            }
            if (parseIntAfterPrefix(arg, "--limit=", options)) {
                continue;
            }
            System.err.println("Unknown argument: " + arg);
            return false;
        }
        return options.limit >= 2;
    }

    static long properDivisorSumViaFactorization(long n) {
        if (n <= 1) {
            return 0;
        }

        long remaining = n;
        long sigma = 1;

        int exponent = 0;
        while ((remaining & 1L) == 0L) {
            remaining >>= 1L;
            ++exponent;
        }
        if (exponent > 0) {
            long term = 1;
            long pow = 1;
            for (int i = 0; i < exponent; ++i) {
                pow <<= 1L;
                term += pow;
            }
            sigma *= term;
        }

        for (long p = 3; p <= remaining / p; p += 2) {
            exponent = 0;
            while (remaining % p == 0) {
                remaining /= p;
                ++exponent;
            }
            if (exponent > 0) {
                long term = 1;
                long pow = 1;
                for (int i = 0; i < exponent; ++i) {
                    pow *= p;
                    term += pow;
                }
                sigma *= term;
            }
        }

        if (remaining > 1) {
            sigma *= (1 + remaining);
        }

        return sigma - n;
    }

    static long[] properDivisorSumsUpTo(int limit) {
        long[] sums = new long[limit];
        for (int d = 1; d <= (limit - 1) / 2; ++d) {
            for (int m = d * 2; m < limit; m += d) {
                sums[m] += d;
            }
        }
        return sums;
    }

    static long solve(int limit) {
        long[] sums = properDivisorSumsUpTo(limit);

        long total = 0;
        for (int a = 2; a < limit; ++a) {
            long b = sums[a];
            if (b == a || b <= 0) {
                continue;
            }

            long backSum;
            if (b < limit) {
                backSum = sums[(int) b];
            } else {
                backSum = properDivisorSumViaFactorization(b);
            }

            if (backSum == a) {
                total += a;
            }
        }

        return total;
    }

    static boolean runCheckpoints() {
        if (properDivisorSumViaFactorization(220) != 284 ||
            properDivisorSumViaFactorization(284) != 220) {
            System.err.println("Checkpoint failed for amicable pair (220, 284)");
            return false;
        }
        if (solve(300) != 504) {
            System.err.println("Checkpoint failed for limit=300");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Options options = new Options();
        if (!parseArguments(args, options)) {
            System.exit(1);
        }
        if (options.runCheckpoints && !runCheckpoints()) {
            System.exit(2);
        }
        System.out.println(solve(options.limit));
    }
}
