import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import java.util.function.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.geom.*; import javax.imageio.*; import java.math.*; import java.time.Duration; import java.lang.invoke.VarHandle; import java.lang.invoke.MethodHandles; // Can a be converted to b? // score 0 = exact match // score Int.MAX_VALUE = no match // score 1 = conversion needed (boxing/unboxing or non-primitve widening) // score -2 = primitive widening needed class main { static int typeConversionScore(Class a, Class b) { if (a == b) return 0; if (b.isPrimitive()) { // target type is primitive if (a.isPrimitive()) { // both types are primitive // No widenings to bool if (b == boolean.class) return Integer.MAX_VALUE; // No widenings to byte if (b == byte.class) return Integer.MAX_VALUE; // byte can be converted to char if (b == char.class) return a == byte.class ? -2 : Integer.MAX_VALUE; // byte can be converted to short if (b == short.class) return a == byte.class ? -2 : Integer.MAX_VALUE; // byte, char and short can be converted to int if (b == int.class) return a == byte.class || a == char.class || a == short.class ? -2 : Integer.MAX_VALUE; // byte, char, short and int can be converted to long if (b == long.class) return a == byte.class || a == char.class || a == short.class || a == int.class ? -2 : Integer.MAX_VALUE; // byte, char, short and int can be converted to float if (b == float.class) return a == byte.class || a == char.class || a == short.class || a == int.class ? -2 : Integer.MAX_VALUE; // all primitive types except bool can be converted to double return a != boolean.class ? -2 : Integer.MAX_VALUE; } else { // source type is boxed - check if they're a match return primitiveToBoxedType(b) == a ? 1 : Integer.MAX_VALUE; } } else { // Target type b is not primitive. // Object type a is primitive - check for exact match if (a.isPrimitive()) return primitiveToBoxedType(a) == b ? 1 : Integer.MAX_VALUE; // BOTH TYPES ARE NOT PRIMITIVE. // if not assignable, no match if (!b.isAssignableFrom(a)) return Integer.MAX_VALUE; // if assignable, get precise score // if any is an interface, just get score 1 if (a.isInterface() || b.isInterface()) return 1; if (a.isArray() && b.isArray()) return typeConversionScore(a.getComponentType(), b.getComponentType()); // for classes, count subclass distance return subclassDistance(a, b)*2; } } static Class primitiveToBoxedType(Class type) { if (type == boolean.class) return Boolean.class; if (type == int.class) return Integer.class; if (type == long.class) return Long.class; if (type == float.class) return Float.class; if (type == short.class) return Short.class; if (type == char.class) return Character.class; if (type == byte.class) return Byte.class; if (type == double.class) return Double.class; return null; } static int subclassDistance(Class a, Class b) { int n = 0; while (a != b) { a = a.getSuperclass(); if (a == null) return Integer.MAX_VALUE; ++n; } return n; } }