結果
| 問題 | No.3699 引き抜き交渉 |
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2026-09-09 22:34:31 |
| 言語 | Java (openjdk 26.0.2.1 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 73 ms / 2,000 ms |
| + 840µs | |
| コード長 | 34,837 bytes |
| 記録 | |
| コンパイル時間 | 4,035 ms |
| コンパイル使用メモリ | 110,164 KB |
| 実行使用メモリ | 42,396 KB |
| 最終ジャッジ日時 | 2026-09-09 22:36:05 |
| 合計ジャッジ時間 | 6,341 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 15 |
ソースコード
import java.util.*;
public class Main {
public static final int MOD998 = 998244353;
public static final int MOD100 = 1000000007;
public static void main(String[] args) throws Exception {
ContestScanner sc = new ContestScanner();
ContestPrinter cp = new ContestPrinter();
int N = sc.nextInt();
int M = sc.nextInt();
int[][] arr = sc.nextIntMatrix(N, 2);
int[][] edg = sc.nextIntMatrix(M, 3);
MaxFlow mf = new MaxFlow(N + 2);
long base = 0;
for (int i = 0; i < N; i++) {
base += arr[i][0];
base += arr[i][1];
mf.addEdge(N, i, arr[i][1]);
mf.addEdge(i, N + 1, arr[i][0]);
}
for (int i = 0; i < M; i++) {
mf.addEdge(edg[i][0] - 1, edg[i][1] - 1, edg[i][2]);
mf.addEdge(edg[i][1] - 1, edg[i][0] - 1, edg[i][2]);
}
long fl = mf.maxFlow(N, N + 1);
cp.println(base - fl);
cp.close();
}
//////////////////
// My Library //
//////////////////
static class MaxFlow {
private static final class InternalCapEdge {
final int to;
final int rev;
long cap;
InternalCapEdge(int to, int rev, long cap) {
this.to = to;
this.rev = rev;
this.cap = cap;
}
}
public static final class CapEdge {
public final int from, to;
public final long cap, flow;
CapEdge(int from, int to, long cap, long flow) {
this.from = from;
this.to = to;
this.cap = cap;
this.flow = flow;
}
@Override
public boolean equals(Object o) {
if (o instanceof CapEdge) {
CapEdge e = (CapEdge) o;
return from == e.from && to == e.to && cap == e.cap && flow == e.flow;
}
return false;
}
}
private static final class IntPair {
final int first, second;
IntPair(int first, int second) {
this.first = first;
this.second = second;
}
}
static final long INF = Long.MAX_VALUE;
private final int n;
private final java.util.ArrayList<IntPair> pos;
private final java.util.ArrayList<InternalCapEdge>[] g;
@SuppressWarnings("unchecked")
public MaxFlow(int n) {
this.n = n;
this.pos = new java.util.ArrayList<>();
this.g = new java.util.ArrayList[n];
for (int i = 0; i < n; i++) {
this.g[i] = new java.util.ArrayList<>();
}
}
public int addEdge(int from, int to, long cap) {
rangeCheck(from, 0, n);
rangeCheck(to, 0, n);
nonNegativeCheck(cap, "Capacity");
int m = pos.size();
pos.add(new IntPair(from, g[from].size()));
int fromId = g[from].size();
int toId = g[to].size();
if (from == to)
toId++;
g[from].add(new InternalCapEdge(to, toId, cap));
g[to].add(new InternalCapEdge(from, fromId, 0L));
return m;
}
private InternalCapEdge getInternalEdge(int i) {
return g[pos.get(i).first].get(pos.get(i).second);
}
private InternalCapEdge getInternalEdgeReversed(InternalCapEdge e) {
return g[e.to].get(e.rev);
}
public CapEdge getEdge(int i) {
int m = pos.size();
rangeCheck(i, 0, m);
InternalCapEdge e = getInternalEdge(i);
InternalCapEdge re = getInternalEdgeReversed(e);
return new CapEdge(re.to, e.to, e.cap + re.cap, re.cap);
}
public CapEdge[] getEdges() {
CapEdge[] res = new CapEdge[pos.size()];
java.util.Arrays.setAll(res, this::getEdge);
return res;
}
public void changeEdge(int i, long newCap, long newFlow) {
int m = pos.size();
rangeCheck(i, 0, m);
nonNegativeCheck(newCap, "Capacity");
if (newFlow > newCap) {
throw new IllegalArgumentException(
String.format("Flow %d is greater than the capacity %d.", newCap, newFlow));
}
InternalCapEdge e = getInternalEdge(i);
InternalCapEdge re = getInternalEdgeReversed(e);
e.cap = newCap - newFlow;
re.cap = newFlow;
}
public long maxFlow(int s, int t) {
return flow(s, t, INF);
}
public long flow(int s, int t, long flowLimit) {
rangeCheck(s, 0, n);
rangeCheck(t, 0, n);
long flow = 0L;
int[] level = new int[n];
int[] que = new int[n];
int[] iter = new int[n];
while (flow < flowLimit) {
bfs(s, t, level, que);
if (level[t] < 0)
break;
java.util.Arrays.fill(iter, 0);
while (flow < flowLimit) {
long d = dfs(t, s, flowLimit - flow, iter, level);
if (d == 0)
break;
flow += d;
}
}
return flow;
}
private void bfs(int s, int t, int[] level, int[] que) {
java.util.Arrays.fill(level, -1);
int hd = 0, tl = 0;
que[tl++] = s;
level[s] = 0;
while (hd < tl) {
int u = que[hd++];
for (InternalCapEdge e : g[u]) {
int v = e.to;
if (e.cap == 0 || level[v] >= 0)
continue;
level[v] = level[u] + 1;
if (v == t)
return;
que[tl++] = v;
}
}
}
private long dfs(int cur, int s, long flowLimit, int[] iter, int[] level) {
if (cur == s)
return flowLimit;
long res = 0;
int curLevel = level[cur];
for (int itMax = g[cur].size(); iter[cur] < itMax; iter[cur]++) {
int i = iter[cur];
InternalCapEdge e = g[cur].get(i);
InternalCapEdge re = getInternalEdgeReversed(e);
if (curLevel <= level[e.to] || re.cap == 0)
continue;
long d = dfs(e.to, s, Math.min(flowLimit - res, re.cap), iter, level);
if (d <= 0)
continue;
e.cap += d;
re.cap -= d;
res += d;
if (res == flowLimit)
break;
}
return res;
}
public boolean[] minCut(int s) {
rangeCheck(s, 0, n);
boolean[] visited = new boolean[n];
int[] stack = new int[n];
int ptr = 0;
stack[ptr++] = s;
visited[s] = true;
while (ptr > 0) {
int u = stack[--ptr];
for (InternalCapEdge e : g[u]) {
int v = e.to;
if (e.cap > 0 && !visited[v]) {
visited[v] = true;
stack[ptr++] = v;
}
}
}
return visited;
}
private void rangeCheck(int i, int minInclusive, int maxExclusive) {
if (i < 0 || i >= maxExclusive) {
throw new IndexOutOfBoundsException(
String.format("Index %d out of bounds for length %d", i, maxExclusive));
}
}
private void nonNegativeCheck(long cap, String attribute) {
if (cap < 0) {
throw new IllegalArgumentException(String.format("%s %d is negative.", attribute, cap));
}
}
}
public static class SlopeTrick {
private PriorityQueue<Long> lq = new PriorityQueue<>(Comparator.reverseOrder());
private PriorityQueue<Long> rq = new PriorityQueue<>();
private long lshift = 0;
private long rshift = 0;
private long min = 0;
public long getMin() {
return min;
}
public long get(long x) {
long val = min;
for (long l : lq) {
if (l - x > 0) {
val += l - x;
}
}
for (long r : rq) {
if (x - r > 0) {
val += x - r;
}
}
return val;
}
public long getMinPosLeft() {
return lq.isEmpty() ? Long.MIN_VALUE : lq.peek() + lshift;
}
public long getMinPosRight() {
return rq.isEmpty() ? Long.MAX_VALUE : rq.peek() + rshift;
}
public void addConst(long a) {
min += a;
}
public void addSlopeRight(long a) {
if (!lq.isEmpty() && lq.peek() + lshift > a) {
min += lq.peek() + lshift - a;
lq.add(a - lshift);
rq.add(lq.poll() + lshift - rshift);
} else {
rq.add(a - rshift);
}
}
public void addSlopeLeft(long a) {
if (!rq.isEmpty() && rq.peek() < a) {
min += a - rq.peek() - rshift;
rq.add(a - rshift);
lq.add(rq.poll() + rshift - lshift);
} else {
lq.add(a - lshift);
}
}
public void addAbs(long a) {
addSlopeLeft(a);
addSlopeRight(a);
}
public void shift(long a) {
lshift += a;
rshift += a;
}
public void slideLeft(long a) {
lshift += a;
}
public void slideRight(long a) {
rshift += a;
}
public void clearLeft() {
lq.clear();
}
public void clearRight() {
rq.clear();
}
public void clearMin() {
min = 0;
}
}
public static int zeroOneBFS(int[][][] weighted_graph, int start, int goal) {
int[] dist = new int[weighted_graph.length];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int now = queue.poll();
if (now == goal) {
return dist[goal];
}
for (int[] info : weighted_graph[now]) {
if (dist[info[0]] > dist[now] + info[1]) {
dist[info[0]] = dist[now] + info[1];
if (info[1] == 0) {
queue.addFirst(info[0]);
} else {
queue.addLast(info[0]);
}
}
}
}
return -1;
}
public static int[] zeroOneBFSAll(int[][][] weighted_graph, int start) {
int[] dist = new int[weighted_graph.length];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int now = queue.poll();
for (int[] info : weighted_graph[now]) {
if (dist[info[0]] > dist[now] + info[1]) {
dist[info[0]] = dist[now] + info[1];
if (info[1] == 0) {
queue.addFirst(info[0]);
} else {
queue.addLast(info[0]);
}
}
}
}
return dist;
}
static int[][] scanGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) {
int[][] arr = sc.nextIntArrayMulti(edge, 2);
for (int n = 0; n < edge; n++) {
arr[0][n]--;
arr[1][n]--;
}
return GraphBuilder.makeGraph(node, edge, arr[0], arr[1], undirected);
}
static int[][][] scanWeightedGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) {
int[][] arr = sc.nextIntArrayMulti(edge, 3);
for (int n = 0; n < edge; n++) {
arr[0][n]--;
arr[1][n]--;
}
return GraphBuilder.makeGraphWithWeight(node, edge, arr[0], arr[1], arr[2], undirected);
}
static class EdgeData {
private int capacity;
private int[] from, to, weight;
private int p = 0;
private boolean weighted;
public EdgeData(boolean weighted) {
this(weighted, 500000);
}
public EdgeData(boolean weighted, int initial_capacity) {
capacity = initial_capacity;
from = new int[capacity];
to = new int[capacity];
weight = new int[capacity];
this.weighted = weighted;
}
public void addEdge(int u, int v) {
if (weighted) {
System.err.println("The graph is weighted!");
return;
}
if (p == capacity) {
int[] newfrom = new int[capacity * 2];
int[] newto = new int[capacity * 2];
System.arraycopy(from, 0, newfrom, 0, capacity);
System.arraycopy(to, 0, newto, 0, capacity);
capacity *= 2;
from = newfrom;
to = newto;
}
from[p] = u;
to[p] = v;
p++;
}
public void addEdge(int u, int v, int w) {
if (!weighted) {
System.err.println("The graph is NOT weighted!");
return;
}
if (p == capacity) {
int[] newfrom = new int[capacity * 2];
int[] newto = new int[capacity * 2];
int[] newweight = new int[capacity * 2];
System.arraycopy(from, 0, newfrom, 0, capacity);
System.arraycopy(to, 0, newto, 0, capacity);
System.arraycopy(weight, 0, newweight, 0, capacity);
capacity *= 2;
from = newfrom;
to = newto;
weight = newweight;
}
from[p] = u;
to[p] = v;
weight[p] = w;
p++;
}
public int[] getFrom() {
int[] result = new int[p];
System.arraycopy(from, 0, result, 0, p);
return result;
}
public int[] getTo() {
int[] result = new int[p];
System.arraycopy(to, 0, result, 0, p);
return result;
}
public int[] getWeight() {
int[] result = new int[p];
System.arraycopy(weight, 0, result, 0, p);
return result;
}
public int size() {
return p;
}
}
////////////////////////////////
// Atcoder Library for Java //
////////////////////////////////
static class MathLib {
private static long safe_mod(long x, long m) {
x %= m;
if (x < 0)
x += m;
return x;
}
private static long[] inv_gcd(long a, long b) {
a = safe_mod(a, b);
if (a == 0)
return new long[] { b, 0 };
long s = b, t = a;
long m0 = 0, m1 = 1;
while (t > 0) {
long u = s / t;
s -= t * u;
m0 -= m1 * u;
long tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0)
m0 += b / s;
return new long[] { s, m0 };
}
public static long gcd(long a, long b) {
a = java.lang.Math.abs(a);
b = java.lang.Math.abs(b);
if (a == 0) {
return b;
} else if (b == 0) {
return a;
}
return inv_gcd(a, b)[0];
}
public static long lcm(long a, long b) {
a = java.lang.Math.abs(a);
b = java.lang.Math.abs(b);
return a / gcd(a, b) * b;
}
public static long pow_mod(long x, long n, int m) {
assert n >= 0;
assert m >= 1;
if (m == 1)
return 0L;
x = safe_mod(x, m);
long ans = 1L;
while (n > 0) {
if ((n & 1) == 1)
ans = (ans * x) % m;
x = (x * x) % m;
n >>>= 1;
}
return ans;
}
public static long[] crt(long[] r, long[] m) {
assert (r.length == m.length);
int n = r.length;
long r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
assert (1 <= m[i]);
long r1 = safe_mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) {
long tmp = r0;
r0 = r1;
r1 = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 % m1 == 0) {
if (r0 % m1 != r1)
return new long[] { 0, 0 };
continue;
}
long[] ig = inv_gcd(m0, m1);
long g = ig[0], im = ig[1];
long u1 = m1 / g;
if ((r1 - r0) % g != 0)
return new long[] { 0, 0 };
long x = (r1 - r0) / g % u1 * im % u1;
r0 += x * m0;
m0 *= u1;
if (r0 < 0)
r0 += m0;
// System.err.printf("%d %d\n", r0, m0);
}
return new long[] { r0, m0 };
}
public static long floor_sum(long n, long m, long a, long b) {
long ans = 0;
if (a >= m) {
ans += (n - 1) * n * (a / m) / 2;
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
long y_max = (a * n + b) / m;
long x_max = y_max * m - b;
if (y_max == 0)
return ans;
ans += (n - (x_max + a - 1) / a) * y_max;
ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
return ans;
}
public static java.util.ArrayList<Long> divisors(long n) {
java.util.ArrayList<Long> divisors = new ArrayList<>();
java.util.ArrayList<Long> large = new ArrayList<>();
for (long i = 1; i * i <= n; i++)
if (n % i == 0) {
divisors.add(i);
if (i * i < n)
large.add(n / i);
}
for (int p = large.size() - 1; p >= 0; p--) {
divisors.add(large.get(p));
}
return divisors;
}
}
static class HashMultiset<T> extends java.util.HashMap<T, Long> {
public HashMultiset() {
super();
}
public HashMultiset(java.util.List<T> list) {
super();
for (T e : list)
this.addOne(e);
}
public long count(Object elm) {
return getOrDefault(elm, 0L);
}
public void add(T elm, long amount) {
if (!this.containsKey(elm))
put(elm, amount);
else
replace(elm, get(elm) + amount);
if (this.count(elm) == 0)
this.remove(elm);
}
public void addOne(T elm) {
this.add(elm, 1);
}
public void removeOne(T elm) {
this.add(elm, -1);
}
public void removeAll(T elm) {
this.add(elm, -this.count(elm));
}
public static <T> HashMultiset<T> merge(HashMultiset<T> a, HashMultiset<T> b) {
HashMultiset<T> c = new HashMultiset<>();
for (T x : a.keySet())
c.add(x, a.count(x));
for (T y : b.keySet())
c.add(y, b.count(y));
return c;
}
}
static class GraphBuilder {
public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to,
boolean undirected) {
int[][] graph = new int[NumberOfNodes][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = to[i];
if (undirected)
graph[to[i]][--outdegree[to[i]]] = from[i];
}
return graph;
}
public static int[][][] makeGraphWithWeight(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to,
int[] weight, boolean undirected) {
int[][][] graph = new int[NumberOfNodes][][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]][];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i] };
if (undirected)
graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i] };
}
return graph;
}
public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to,
boolean undirected) {
int[][][] graph = new int[NumberOfNodes][][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]][];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], i, 0 };
if (undirected)
graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], i, 1 };
}
return graph;
}
public static int[][][] makeGraphWithWeightAndEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from,
int[] to, int[] weight, boolean undirected) {
int[][][] graph = new int[NumberOfNodes][][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]][];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i], i, 0 };
if (undirected)
graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i], i, 1 };
}
return graph;
}
}
static class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in) {
this.in = in;
}
public ContestScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class ContestPrinter extends java.io.PrintWriter {
public ContestPrinter(java.io.PrintStream stream) {
super(stream);
}
public ContestPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
}
}