Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
815 B
1
Indexable
Never
import java.util.Scanner;

public class MinimizeSumOptimized {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int x = scanner.nextInt();

        int[] A = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = scanner.nextInt();
        }

        long minSum = minimizeSum(A, x);
        System.out.println(minSum);

        scanner.close();
    }

    public static long minimizeSum(int[] A, int x) {
        long sum = 0;
        int n = A.length;

        for (int i = 0; i < n; i++) {
            long f = A[i] / x;
            if (A[i] % x != 0) {
                f += (A[i] > 0) ? 1 : -1;
            }
            sum += f * x;
        }

        return sum;
    }
}