class Euler23 {

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

    private static boolean parseArguments(String[] args, Options options) {
        for (String arg : args) {
            if ("--skip-checkpoints".equals(arg)) {
                options.runCheckpoints = false;
                continue;
            }
            if (arg.startsWith("--limit=")) {
                try {
                    options.limit = Integer.parseInt(arg.substring(8));
                    if (options.limit < 1) {
                        System.err.println("Limit must be positive");
                        return false;
                    }
                    continue;
                } catch (NumberFormatException e) {
                    System.err.println("Invalid limit value: " + arg);
                    return false;
                }
            }
            System.err.println("Unknown argument: " + arg);
            return false;
        }
        return true;
    }

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

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

        java.util.ArrayList<Integer> abundant = new java.util.ArrayList<>();
        for (int n = 12; n <= limit; n++) {
            if (sums[n] > n) {
                abundant.add(n);
            }
        }

        boolean[] isAbundantSum = new boolean[limit + 1];
        for (int i = 0; i < abundant.size(); i++) {
            for (int j = i; j < abundant.size(); j++) {
                int value = abundant.get(i) + abundant.get(j);
                if (value > limit) {
                    break;
                }
                isAbundantSum[value] = true;
            }
        }

        long total = 0;
        for (int n = 1; n <= limit; n++) {
            if (!isAbundantSum[n]) {
                total += n;
            }
        }

        return total;
    }

    private static boolean runCheckpoints() {
        if (solve(50) != 891) {
            System.err.println("Checkpoint failed for limit=50");
            return false;
        }
        if (solve(100) != 2766) {
            System.err.println("Checkpoint failed for limit=100");
            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));
    }
}
