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 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 javax.imageio.*;
import java.math.*;
import static x30_pkg.x30_util.DynamicObject;
class main {
static class ArrayMatrix extends AbstractMatrix {
ArrayMatrix() {}
A[] data;
ArrayMatrix(int w, int h) {
this.h = h;
this.w = w; data = (A[]) new Object[w*h]; }
ArrayMatrix(int w, int h, A[] data) {
this.data = data;
this.h = h;
this.w = w;}
public A get(int x, int y) { return data[y*w+x]; }
public void set(int x, int y, A a) { data[y*w+x] = a; }
}
abstract static class AbstractMatrix implements Matrix {
AbstractMatrix() {}
int w, h;
AbstractMatrix(int w, int h) {
this.h = h;
this.w = w;}
public int getWidth() { return w; }
public int getHeight() { return h; }
public String toString() {
return flexLines(
roundBracket(w+"x"+h),
countIteratorToList(y -> str(getLine(y)), getHeight()));
}
}
interface Matrix {
public int getWidth();
public int getHeight();
public A get(int x, int y);
public void set(int x, int y, A a);
default Pt size() { return pt(getWidth(), getHeight()); }
default int nCells() { return getWidth()*getHeight(); }
default A get(Pt p) { return get(p.x, p.y); }
default void put(Pt p, A a){ set(p, a); }
default void set(Pt p, A a) { set(p.x, p.y, a); }
default List getLine(int y) {
return new RandomAccessAbstractList() {
public int size() { return getWidth(); }
public A get(int x) {
return Matrix.this.get(x, y);
}
public A set(int x, A val) {
A old = Matrix.this.get(x, y);
Matrix.this.set(x, y, val);
return old;
}
};
}
}
static class Pt implements Comparable {
int x, y;
Pt() {}
Pt(Point p) {
x = p.x;
y = p.y;
}
Pt(int x, int y) {
this.y = y;
this.x = x;}
Point getPoint() {
return new Point(x, y);
}
public boolean equals(Object o) {
return o instanceof Pt && x == ((Pt) o).x && y == ((Pt) o).y;
}
public int hashCode() {
return boostHashCombine(x, y);
}
// compare in scan order
public int compareTo(Pt p) {
if (y != p.y) return cmp(y, p.y);
return cmp(x, p.x);
}
public String toString() {
return x + ", " + y;
}
}
abstract static class RandomAccessAbstractList extends AbstractList implements RandomAccess {
}
static String flexLines(Object... l) {
return lines(flattenIterablesAndArrays(ll(l)));
}
static String roundBracket(String s) {
return "(" + s + ")";
}
static String roundBracket(Object s) {
return roundBracket(str(s));
}
static List countIteratorToList(int b) { return countIteratorToList(0, b); }
static List countIteratorToList(int a, int b) {
return asList(countIterator(a, b));
}
static List countIteratorToList(int b, IF1 f) { return countIteratorToList(0, b, f); }
static List countIteratorToList(int a, int b, IF1 f) {
return asList(countIterator(a, b, f));
}
static List countIteratorToList(int a, int b, int step) {
return asList(countIterator(a, b, step));
}
static List countIteratorToList(double a, double b, double step, IF1 f) {
return asList(countIterator(a, b, step, f));
}
static List countIteratorToList(double a, double b, double step) {
return asList(countIterator(a, b, step));
}
static List countIteratorToList(IF1 f, double a, double b, double step) {
return asList(countIterator(f, a, b, step));
}
static List countIteratorToList(IF1 f, int b) { return countIteratorToList(f, 0, b); }
static List countIteratorToList(IF1 f, int a, int b) {
return asList(countIterator(f, a, b));
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
// lineNr starts at 1
static String getLine(String s, int lineNr) {
return safeGet(toLines(s), lineNr-1);
}
static int getHeight(Component c) {
return c == null ? 0 : (int) swingCall(c, "getHeight");
}
static Pt pt(int x, int y) {
return new Pt(x, y);
}
static Pt pt(int x) {
return new Pt(x, x);
}
static int getWidth(Component c) {
return c == null ? 0 : (int) swingCall(c, "getWidth");
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
static A set(A o, String field, Object value) {
if (o == null) return null;
if (o instanceof Class) set((Class) o, field, value);
else try {
Field f = set_findField(o.getClass(), field);
makeAccessible(f);
smartSet(f, o, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
return o;
}
static void set(Class c, String field, Object value) {
if (c == null) return;
try {
Field f = set_findStaticField(c, field);
makeAccessible(f);
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field set_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field set_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static void set(BitSet bs, int idx) {
{ if (bs != null) bs.set(idx); }
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int cmp(Number a, Number b) {
return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}
static int cmp(double a, double b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(int a, int b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(long a, long b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(Object a, Object b) {
if (a == null) return b == null ? 0 : -1;
if (b == null) return 1;
return ((Comparable) a).compareTo(b);
}
static String lines(Iterable lines) { return fromLines(lines); }
static String lines(Object[] lines) { return fromLines(asList(lines)); }
static List lines(String s) { return toLines(s); }
// convenience map call
static String lines(Iterable l, IF1 f) {
return mapToLines(l, f);
}
static List flattenIterablesAndArrays(Iterable a) {
List l = new ArrayList();
for (Object x : a)
if (x instanceof Iterable)
l.addAll(flattenIterablesAndArrays((Iterable) x));
else if (x instanceof Iterator)
l.addAll(flattenIterablesAndArrays(asList((Iterator) x)));
else if (x instanceof Object[])
l.addAll(flattenIterablesAndArrays(asList((Object[]) x)));
else
l.add(x);
return l;
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
// unclear semantics as to whether return null on null
static ArrayList asList(A[] a) {
return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a));
}
static ArrayList asList(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(long[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (long i : a) l.add(i);
return l;
}
static ArrayList asList(float[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (float i : a) l.add(i);
return l;
}
static ArrayList asList(double[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (double i : a) l.add(i);
return l;
}
static ArrayList asList(short[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (short i : a) l.add(i);
return l;
}
static ArrayList asList(Iterator it) {
ArrayList l = new ArrayList();
if (it != null)
while (it.hasNext())
l.add(it.next());
return l;
}
// disambiguation
static ArrayList asList(IterableIterator s) {
return asList((Iterator) s);
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static List asList(Pair p) {
return p == null ? null : ll(p.a, p.b);
}
static IterableIterator countIterator(int b) { return countIterator(0, b); }
static IterableIterator countIterator(int a, int b) {
return countIterator_exclusive(a, b);
}
static IterableIterator countIterator(int b, IF1 f) { return countIterator(0, b, f); }
static IterableIterator countIterator(int a, int b, IF1 f) {
return countIterator_exclusive(a, b, f);
}
static IterableIterator countIterator(int a, int b, int step) {
return countIterator_exclusive_step(a, b, step);
}
static IterableIterator countIterator(double a, double b, double step, IF1 f) {
return countIterator_exclusive_step(a, b, step, f);
}
static IterableIterator countIterator(double a, double b, double step) {
return countIterator_exclusive_step(a, b, step);
}
static IterableIterator countIterator(IF1 f, double a, double b, double step) {
return countIterator(a, b, step, f);
}
static IterableIterator countIterator(IF1 f, int b) { return countIterator(f, 0, b); }
static IterableIterator countIterator(IF1 f, int a, int b) {
return countIterator_exclusive(a, b, f);
}
static A safeGet(List l, int i) {
return i >= 0 && i < l(l) ? l.get(i) : null;
}
static IterableIterator toLines(File f) {
return linesFromFile(f);
}
static List toLines(String s) {
List lines = new ArrayList();
if (s == null) return lines;
int start = 0;
while (true) {
int i = toLines_nextLineBreak(s, start);
if (i < 0) {
if (s.length() > start) lines.add(s.substring(start));
break;
}
lines.add(s.substring(start, i));
if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
i += 2;
else
++i;
start = i;
}
return lines;
}
static int toLines_nextLineBreak(String s, int start) {
int n = s.length();
for (int i = start; i < n; i++) {
char c = s.charAt(i);
if (c == '\r' || c == '\n')
return i;
}
return -1;
}
static Object swingCall(final Object o, final String method, final Object... args) {
return swing(new F0