import java.util.*;
import java.lang.*;
class Ideone {
public static int plainDotProduct(int[] v1, int[] v2) {
int dotProduct = 0;
for (int i = 0 ; i < v1.length; i++) {
dotProduct += v1[i] * v2[i];
}
return dotProduct;
}
public static int dotProduct(List<int[]> v1, List<int[]> v2) {
int dotProduct = 0;
for (int i = 0, j = 0; i < v1.size() && j < v2.size(); ) {
int[] a = v1.get(i);
int[] b = v2.get(j);
int multiplier
= Math.
min(a
[0], b
[0]); a[0] -= multiplier;
b[0] -= multiplier;
dotProduct += a[1] * b[1] * multiplier;
if (a[0] == 0) i++;
if (b[0] == 0) j++;
}
return dotProduct;
}
public static List<int[]> compress(int[] arr) {
List<int[]> compressed = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
int val = arr[i];
int count = 1;
for (; i + 1 < arr.length && arr[i + 1] == val; i++) {
count++;
}
compressed.add(new int[] {count, val});
}
return compressed;
}
public static void main
(String[] args
) { int[] a = {1, 1, 4, 4, 4, 4, 7, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
int[] b = {2, 2, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
System.
out.
println(plainDotProduct
(a, b
)); System.
out.
println(dotProduct
(compress
(a
), compress
(b
))); }
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CgpjbGFzcyBJZGVvbmUgewoJCglwdWJsaWMgc3RhdGljIGludCBwbGFpbkRvdFByb2R1Y3QoaW50W10gdjEsIGludFtdIHYyKSB7CgkJaW50IGRvdFByb2R1Y3QgPSAwOwoJCWZvciAoaW50IGkgPSAwIDsgaSA8IHYxLmxlbmd0aDsgaSsrKSB7CgkJCWRvdFByb2R1Y3QgKz0gdjFbaV0gKiB2MltpXTsKCQl9CgkJcmV0dXJuIGRvdFByb2R1Y3Q7Cgl9CgkKCXB1YmxpYyBzdGF0aWMgaW50IGRvdFByb2R1Y3QoTGlzdDxpbnRbXT4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