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;
import static x30_pkg.x30_util.DynamicObject;
import java.text.*;
import java.text.NumberFormat;
import java.util.TimeZone;
import java.awt.geom.*;
class main {
static class Best implements IBest {
A best;
double score;
boolean verboseNewBest, replaceIfSameScore;
final public Best setLowerIsBetter(boolean lowerIsBetter){ return lowerIsBetter(lowerIsBetter); }
public Best lowerIsBetter(boolean lowerIsBetter) { this.lowerIsBetter = lowerIsBetter; return this; } final public boolean getLowerIsBetter(){ return lowerIsBetter(); }
public boolean lowerIsBetter() { return lowerIsBetter; }
boolean lowerIsBetter = false;
transient Object onChange;
transient Object stringifier; // func(A) -> S
synchronized boolean isNewBest(double score) {
return best == null || !isNaN(score)
&& (replaceIfSameScore
? compareScores(score, this.score) >= 0
: compareScores(score, this.score) > 0);
}
double worstScore() {
return lowerIsBetter ? infinity() : minusInfinity();
}
int compareScores(double a, double b) {
return lowerIsBetter ? -cmp(a, b) : cmp(a, b);
}
synchronized double bestScore() {
return best == null ? worstScore() : score;
}
double score() { return bestScore(); }
double getScore() { return bestScore(); }
synchronized float floatScoreOr(float defaultValue) {
return best == null ? defaultValue : (float) score;
}
boolean put(Scored extends A> s) {
return s != null && put(s.get(), s.score());
}
boolean put(Pair extends A, Double> p) {
return p != null && put(p.a, p.b);
}
boolean put(Best extends A> b) {
return b != null && put(b.get(), b.score);
}
public boolean put(A a, double score) {
ping();
boolean change = false;
if (a != null) synchronized(this) {
if (isNewBest(score)) {
best = a;
this.score = score;
change = true;
}
}
if (change) {
if (verboseNewBest) print("New best! " + this);
pcallF(onChange);
}
return change;
}
synchronized A get() { return best; }
synchronized boolean has() { return best != null; }
synchronized Pair pair() { return main.pair(best, bestScore()); }
synchronized Scored scored() { return best == null ? null : new Scored(best, bestScore()); }
synchronized A getIfScoreAbove(double x) { return compareScores(score(), x) >= 0 ? best : null; }
public String toString() {
return "Score " + formatDouble_significant2(score, 4) + ": " + callStringifier(stringifier, best);
}
boolean putAndPrintIfNewBest(A a, double score) {
if (!put(a, score)) return false;
{ print(this); return true; }
}
synchronized void clear() { best = null; score = 0; }
}
static boolean isNaN(double d) {
return Double.isNaN(d);
}
static boolean isNaN(float f) {
return Float.isNaN(f);
}
static double infinity() {
return positiveInfinity();
}
static double minusInfinity() {
return negativeInfinity();
}
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 void put(Map map, A a, B b) {
if (map != null) map.put(a, b);
}
static void put(List l, int i, A a) {
if (l != null && i >= 0 && i < l(l)) l.set(i, a);
}
// legacy mode
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// ignore pingSource if not PingV3
static boolean ping(PingSource pingSource) { return ping(); }
// always returns true
static boolean ping() {
//ifdef useNewPing
newPing();
//endifdef
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal