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.*;
class main {
static void bestPutIfNotNull(Best best, A a, Integer score) {
if (best != null && a != null && score != null)
best.put(a, score);
}
static class Best {
A best;
double score;
boolean verboseNewBest, replaceIfSameScore;
transient Object onChange;
transient Object stringifier; // func(A) -> S
synchronized boolean isNewBest(double score) {
return best == null || !isNaN(score)
&& (replaceIfSameScore
? score >= this.score
: score > this.score);
}
synchronized double bestScore() {
return best == null ? minusInfinity() : score;
}
double score() { return bestScore(); }
double getScore() { return bestScore(); }
synchronized float floatScoreOr(float defaultValue) {
return best == null ? defaultValue : (float) 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);
}
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 A getIfScoreAbove(double x) { return score() >= x ? 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;
}
}
static class Pair implements Comparable> {
A a;
B b;
Pair() {}
Pair(A a, B b) {
this.b = b;
this.a = a;}
public int hashCode() {
return hashCodeFor(a) + 2*hashCodeFor(b);
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
Pair t = (Pair) o;
return eq(a, t.a) && eq(b, t.b);
}
public String toString() {
return "<" + a + ", " + b + ">";
}
public int compareTo(Pair p) {
if (p == null) return 1;
int i = ((Comparable) a).compareTo(p.a);
if (i != 0) return i;
return ((Comparable) b).compareTo(p.b);
}
}
static boolean isNaN(double d) {
return Double.isNaN(d);
}
static boolean isNaN(float f) {
return Float.isNaN(f);
}
static double minusInfinity() {
return negativeInfinity();
}
static float score(Scored s) {
return s == null ? 0 : s.score();
}
static float getScore(Scored s) {
return s == null ? 0 : s.score;
}
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);
}
//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();
// always returns true
static boolean ping() {
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 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