code
stringlengths
4
1.01M
language
stringclasses
2 values
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y", long, "cost"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); bool t; foreach (e; s) t |= e == '7'; if (t) writeln("Yes"); else writeln("No"); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.algorithm; import std.array; import std.ascii; import std.bigint; import std.complex; import std.container; import std.conv; import std.functional; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } void readlnTo(T...)(ref T t) { auto s = readln().split(); assert(s.length == t.length); foreach(ref ti; t) { ti = s[0].to!(typeof(ti)); s = s[1..$]; } } const real eps = 1e-10; void main(){ auto s = readln().chomp(); long ans; long ng; long np; foreach(si; s) { if(si == 'p') { if(np < ng) { ++np; } else { ++ng; --ans; } } else { if(np < ng) { ++np; ++ans; } else { ++ng; } } } writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } T binarySearch(alias pred, T)(T ok, T ng) { while (abs(ok-ng) > 1) { auto mid = (ok+ng)/2; if (unaryFun!pred(mid)) ok = mid; else ng = mid; } return ok; } void main() { auto n = RD!int; auto k = RD!int; auto a = RDA!int; bool f(int x) { { auto dp = new int[](n+2); auto begin = k % 2 ? 1 : 0; auto end = n-1; foreach (i; begin..end) { if (a[i] <= x) { if (dp[i]+1 == k/2) return true; dp[i+2].chmax(dp[i]+1); } dp[i+1].chmax(dp[i]); } } { auto dp = new int[](n+2); auto begin = k % 2 ? 0 : 1; auto end = n; foreach (i; begin..end) { if (a[i] <= x) { if (dp[i]+1 == (k+1)/2) return true; dp[i+2].chmax(dp[i]+1); } dp[i+1].chmax(dp[i]); } } return false; } auto ans = binarySearch!(f)(10^^9, 0); writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto S = readln.chomp; auto D = S.map!(c => c == 'D').sum; auto R = N - D; auto dead = new bool[](N); int can_kill_D, can_kill_R; while (D > 0 && R > 0) { foreach (i, c; enumerate(S)) { if (dead[i]) continue; if (c == 'D' && can_kill_D > 0) { D--; can_kill_D--; dead[i] = true; } else if (c == 'D') { can_kill_R++; } else if (c == 'R' && can_kill_R > 0) { R--; can_kill_R--; dead[i] = true; } else if (c == 'R') { can_kill_D++; } } } writeln(D > 0 ? "D" : "R"); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; long a, b, c, x, y; rd(a, b, c, x, y); if(a+b<c*2){ writeln(x*a+y*b); }else{ auto z=min(x, y), ret=z*c*2; x-=z; y-=z; // writeln(ret); if(x>0){ ret+=x*min(a, c*2); }else if(y>0){ ret+=y*min(b, c*2); } writeln(ret); } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!int; auto f = new int[](n); foreach (i; 0..n) f[i] = readln.split.to!(int[]).toBit; auto p = new int[][](n, 11); foreach (i; 0..n) p[i] = readln.split.to!(int[]); auto ans = long.min; foreach (i; 1..1<<10) { auto r = 0; foreach (j; 0..n) r += p[j][(i&f[j]).popcnt]; ans = max(ans, r); } writeln(ans); } auto toBit(int[] a) { auto r = 0; foreach (i, ai; a) if (ai) r = r.bitSet(i); return r; } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); } pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); } pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); } import core.bitop; pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); } pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); } pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); } }
D
void main() { long a, b; rdVals(a, b); foreach (i; 1 .. 10001) { long x = i * 8 / 100; long y = i * 10 / 100; if (x == a && y == b) { i.writeln; return; } } writeln(-1); } enum long mod = 10^^9 + 7; enum long inf = 1L << 60; T rdElem(T = long)() if (!is(T == struct)) { return readln.chomp.to!T; } alias rdStr = rdElem!string; alias rdDchar = rdElem!(dchar[]); T rdElem(T)() if (is(T == struct)) { T result; string[] input = rdRow!string; assert(T.tupleof.length == input.length); foreach (i, ref x; result.tupleof) { x = input[i].to!(typeof(x)); } return result; } T[] rdRow(T = long)() { return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { return iota(col).map!(x => rdRow!T).array; } void rdVals(T...)(ref T data) { string[] input = rdRow!string; assert(data.length == input.length); foreach (i, ref x; data) { x = input[i].to!(typeof(x)); } } void wrMat(T = long)(T[][] mat) { foreach (row; mat) { foreach (j, compo; row) { compo.write; if (j == row.length - 1) writeln; else " ".write; } } } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.traits; import std.container; import std.functional; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void main() { string[] c; readC(2, c); writeln(c[0][0] == c[1][2] && c[0][1] == c[1][1] && c[0][2] == c[1][0] ? "YES" : "NO"); }
D
/+ dub.sdl: name "D" dependency "dcomp" version=">=0.6.0" +/ //ICPC Bangkok Regional 2016 F import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { auto sc = new Scanner(stdin); int n; sc.read(n); int[][] g = new int[][](n); foreach (i; 0..n-1) { int a, b; sc.read(a, b); a--; b--; g[a] ~= b; g[b] ~= a; } int dfs(int p, int b) { int sm = 0; foreach (d; g[p]) { if (d == b) continue; sm ^= dfs(d, p); } if (p != 0) sm++; return sm; } if (dfs(0, -1)) { writeln("Alice"); } else { writeln("Bob"); } return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] > ip[2]){ "NO".writeln; } else if(ip[2] - ip[0] <= ip[1]){ "YES".writeln; } else { "NO".writeln; } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long n, d; scan(n, d); long cnt; foreach (_; 0 .. n) { long x, y; scan(x, y); if (x ^^ 2 + y ^^ 2 <= d ^^ 2) { cnt += 1; } } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n; readV(n); auto f = new int[](n+1); foreach (i; 1..n+1) { while (i > 1) { foreach (j; 2..i+1) while (i%j == 0) { ++f[j]; i /= j; } } } auto c = f.filter!"a >= 2".array, m = c.length, r = 0; foreach (i; 0..m) foreach (j; 0..m) foreach (k; j+1..m) { if (i != j && i != k && c[i] >= 2 && c[j] >= 4 && c[k] >= 4) ++r; } foreach (i; 0..m) foreach (j; 0..m) { if (i != j && c[i] >= 2 && c[j] >= 24) ++r; if (i != j && c[i] >= 4 && c[j] >= 14) ++r; } foreach (i; 0..m) if (c[i] >= 74) ++r; writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto len = readln.chomp.to!long; auto as = readln.split; string[] ret; ret.length = len; size_t p = len / 2; auto v = len & 1 ? 1 : -1; foreach (i, a; as) { v = -v; p += i * v; ret[p] = a; } writeln(ret.join(" ")); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto M = readln.chomp.to!int; long b = 0, res; foreach (_; 0..M) { auto dc = readln.split.to!(long[]); int d = dc[0].to!int; auto c = dc[1]; int[] at; while (c != 1) { if (c%2 == 1) at ~= d; c /= 2; res += c; d *= 2; if (d >= 10) { res += c; d = d/10 + d%10; } } while (!at.empty) { res += 1; d = d + at[0]; at = at[1..$]; if (d >= 10) { res += 1; d = d/10 + d%10; } } if (b == 0) { b = d; } else { res += 1; b += d; if (b >= 10) { res += 1; b = b/10 + b%10; } } } writeln(res); }
D
import std.stdio; import std.string; import std.array; import std.range; import std.algorithm; import std.conv; import std.typecons; long solve(long d, long n){ if(n != 100){ return 100 ^^ d * n; }else{ return 100 ^^ (d+1) + 100 ^^ d; } } void main(){ auto dn = readln().chomp().split().map!(to!long).array(); auto d = dn[0]; auto n = dn[1]; solve(d, n).writeln(); }
D
void main() { auto b = rs; switch(b) { case "A": writeln("T"); break; case "T": writeln("A"); break; case "G": writeln("C"); break; case "C": writeln("G"); break; default: break; } } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } void main(){ long n = read.to!long; long k = read.to!long; long[] as = readln.chomp.split.to!(long[]); long ans = ((n - 1) + (k - 1 - 1)) / (k - 1); ans.writeln; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { auto c = new int[][](3, 3); foreach (i; 0..3) readA(3, c[i]); writeln(c[1][1] == c[1][0]-c[0][0]+c[0][1] && c[1][2] == c[1][0]-c[0][0]+c[0][2] && c[2][1] == c[2][0]-c[0][0]+c[0][1] && c[2][2] == c[2][0]-c[0][0]+c[0][2] ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!long; writeln(N * (N-1) / 2); }
D
import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { int X, Y, r; auto input = readln.split.to!(int[]); X = input[0]; Y = input[1]; while(X%Y) { r = X%Y; X = Y; Y = r; } writeln(Y); }
D
import std.stdio; import std.conv; import std.string; void main() { int n = readln.chomp.to!int; if (n == 1){ "Hello World".writeln; return; } int a = readln.chomp.to!int; int b = readln.chomp.to!int; (a + b).writeln; }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; void main(){ long[] ds = readln.chomp.split.map!(to!long).array; long a = ds[0], b = ds[1], c = ds[2]; long ans; if(a % 2 == 0 || b % 2 == 0 || c % 2 == 0) ans = 0; else ans = min(a * b, a * c, b * c); ans.writeln; }
D
void main() { int n = readln.chomp.to!int; writeln(n * (n + 1) / 2); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
void main() { int n = readln.chomp.to!int; int[] tmp = readln.split.to!(int[]); int a = tmp[0], b = tmp[1]; int[] p = readln.split.to!(int[]); int p1, p2, p3; foreach (x; p) { if (x <= a) ++p1; if (a + 1 <= x && x <= b) ++p2; if (x >= b + 1) ++p3; } min(p1, p2, p3).writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long a, b, c, d; scan(a, b, c, d); auto tmp = new long[](100000); foreach (i; a .. b) { tmp[i] += 1; } foreach (i; c .. d) { tmp[i] += 1; } // writeln(tmp); long cnt; foreach (i; 0 .. tmp.length) { if (tmp[i] == 2) { cnt += 1; } } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.algorithm; import std.string; import std.conv; import std.math; void main(){ int ans=0; while(true){ auto s = readln(); if(stdin.eof()) break; s = chomp(s); int len = to!int(s.length); int l,r; if(len%2==1){ l = len/2 - 1; r = len/2 + 1; }else{ l = len/2 - 1; r = len/2; } bool flg = true; for(int i=0;i<to!int(len/2);i++){ if(s[l]!=s[r]) { flg = false; break; } l--; r++; } if(flg) ans++; } writeln(ans); }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; auto rdsp(){return readln.splitter;} void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;} void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { string s; readV(s); writeln(s[0], s.length-2, s[$-1]); }
D
import std.algorithm; import std.array; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; long calc(int n, int k) { auto m = new long[k]; for (int i = 1; i <= n; i++) m[i % k]++; long ans = 0; for (int i = 1; i <= n; i++) { // a を固定 int a = i % k; int b = (k - a) % k; int c = (k - a) % k; if ((b + c) % k == 0) { ans += m[b] * m[c]; } } return ans; } long calc2(int n, int k) { long ans = 0; for (int i = 1; i <= n; i++) { // a を固定 int a = i % k; long b = (k - a) % k; long c = (k - a) % k; if ((b + c) % k == 0) { ans += ((n + b) / k) * ((n + c) / k); } } return ans; } void main() { int n, k; scan(n, k); writeln(calc2(n, k)); }
D
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.3" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; int main() { Scanner sc = new Scanner(stdin); int n; sc.read(n); int[] g = new int[n]; foreach (i; 0..n-1) { int a, b, z; sc.read(a, b, z); g[a] ^= z; g[b] ^= z; } int f; int ans = g.count(0).to!int; foreach (d; 1..16) { int u = g.count(d).to!int; ans += u/2; if (u % 2) f |= (1<<d); } int[] ls; foreach (i; 1..(1<<16)) { int x = 0; foreach (j; 0..16) { if (i & (1<<j)) x ^= j; } if (x == 0) ls ~= i.to!int; } int[] dp = new int[1<<16]; foreach (i; 0..(1<<16)) { if (i & ~f) continue; foreach (d; ls) { if (d & ~i) continue; if (d > i) break; dp[i] = max(dp[i], dp[i^d]+1); } } ans += dp[f]; writeln(n - ans); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { /* Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d Copyright: Andrei Alexandrescu 2008-. License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). */ template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.array; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succW() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (!line.empty && line.front.isWhite) { line.popFront; } return !line.empty; } private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; line = lineBuf[]; f.readln(line); if (!line.length) return false; } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else static if (isStaticArray!T) { foreach (i; 0..T.length) { bool f = succW(); assert(f); x[i] = line.parse!E; } } else { FastAppender!(E[]) buf; while (succW()) { buf ~= line.parse!E; } x = buf.data; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} struct FastAppender(A, size_t MIN = 4) { import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private alias T = ElementEncodingType!A; private T* _data; private uint len, cap; @property size_t length() const {return len;} bool empty() const { return len == 0; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } ref inout(T) back() inout { assert(len); return _data[len-1]; } ref inout(T) opIndex(size_t i) inout { return _data[i]; } T[] data() { return (_data) ? _data[0..len] : null; } } /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { auto s = readln().chomp().to!(char[]); reverse(s); writeln(s); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto S = readln.chomp.to!(dchar[]); sort(S); writeln(S == "abc" ? "Yes" : "No"); }
D
// Try Codeforces // author: Leonardone @ NEETSDKASU import std.stdio : readln, writeln; import std.string : chomp; import std.array : split; import std.conv : to; import std.bigint : BigInt; auto gets() { return readln.chomp; } auto getV(T)() { return gets.to!T; } auto getVals(T)() { return gets.split.to!(T[]); } void main() { auto abc = getVals!BigInt; auto ax = abc[0], ay = abc[1]; auto bx = abc[2], by = abc[3]; auto cx = abc[4], cy = abc[5]; auto dxab = ax - bx; auto dyab = ay - by; auto dxbc = bx - cx; auto dybc = by - cy; if (dxab ^^ 2 + dyab ^^ 2 != dxbc ^^ 2 + dybc ^^ 2) { writeln("No"); return; } if (ax + cx == 2 * bx && ay + cy == 2 * by) { writeln("No"); return; } writeln("Yes"); }
D
import std.stdio, std.string, std.algorithm, std.conv, std.range, std.math; void main() { auto p = readln.chomp.split.map!(to!double); auto ans = sqrt(pow(p[2] - p[0], 2) + pow(p[3] - p[1], 2)); printf("%.8lf\n", ans); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf3 = 1_001_001_001; enum inf6 = 1_001_001_001_001_001_001L; enum mod = 1_000_000_007L; void main() { long n, a, b; scan(n, a, b); if (n == 1) { if (a != b) { writeln(0); } else { writeln(1); } return; } if (a > b) { writeln(0); return; } auto mn = (n - 1) * a + b; auto mx = a + (n - 1) * b; auto ans = (mx - mn + 1); writeln(ans); } int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new int[][](n, 0); foreach (i ; 0 .. m) { int u, v; scan(u, v); if (is1indexed) { u--, v--; } adj[u] ~= v; if (isUndirected) { adj[v] ~= u; } } return adj; } alias Edge = Tuple!(int, "to", int, "cost"); Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) { auto adj = new Edge[][](n, 0); foreach (i ; 0 .. m) { int u, v, c; scan(u, v, c); if (is1indexed) { u--, v--; } adj[u] ~= Edge(v, c); if (isUndirected) { adj[v] ~= Edge(u, c); } } return adj; } void yes(bool b) {writeln(b ? "Yes" : "No");} void YES(bool b) {writeln(b ? "YES" : "NO");} T[] readArr(T)() {return readln.split.to!(T[]);} T[] readArrByLines(T)(int n) {return iota(n).map!(i => readln.chomp.to!T).array;} void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
void main(){ string n = readln().chomp(); writeln(n.count('9')==0? "No": "Yes"); } import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math; // 1要素のみの入力 T _scan(T= int)(){ return to!(T)( readln().chomp() ); } // 1行に同一型の複数入力 T[] _scanln(T = int)(){ T[] ln; foreach(string elm; readln().chomp().split()){ ln ~= elm.to!T(); } return ln; }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long a, b, c, d; scan(a, b, c, d); max(a * c, b * c, a * d, b * d).writeln(); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { immutable N = 8; immutable n = readln().chomp().to!int(); foreach(i;0..n) { readln(); auto m = iota(N).map!(_=>readln.chomp().to!(char[])()).array(); immutable x = readln().chomp().to!int(); immutable y = readln().chomp().to!int(); void dfs(int x, int y) { if(x<0 || N<=x || y<0 || N<=y || m[y][x]=='0') return; m[y][x]='0'; foreach(d;[[1,0],[-1,0],[0,1],[0,-1]]) foreach(k;1..4) dfs(x+d[0]*k,y+d[1]*k); } dfs(x-1,y-1); writeln("Data ",i+1,":"); m.join("\n").writeln(); } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long a = lread(); long b = lread(); long h = lread(); writeln((a + b) * h / 2); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; static import std.ascii; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); string[] itos; void init(string S) { if (S.length == 3) { itos ~= S; return; } foreach (c; "ACGT") init(S ~ c); return; } init(""); long M = itos.length; long[string] stoi; foreach (i; 0 .. M) stoi[itos[i]] = i; auto dp = new long[][](N + 1, M); foreach (i; 0 .. M) { dp[3][i] = (itos[i] != "AGC" && itos[i] != "ACG" && itos[i] != "GAC"); } foreach (i; 3 .. N) { foreach (j; 0 .. M) { if (itos[j] == "AGC" || itos[j] == "ACG" || itos[j] == "GAC") continue; foreach (c; "ACGT") { string s4 = itos[j] ~ c; // writeln(s4); string s3 = s4[1 .. $]; if (s3 == "AGC" || s3 == "ACG" || s3 == "GAC") continue; if (s4[0] == 'A' && s4[2 .. $] == "GC") continue; if (s4[0 .. 2] == "AG" && s4[$ - 1] == 'C') continue; dp[i + 1][stoi[s3]] += dp[i][j]; dp[i + 1][stoi[s3]] %= MOD; } } // writeln(dp[i]); } long ans; foreach (a; dp[N]) ans = (ans + a) % MOD; writeln(ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long P = 998244353L; long[(10^^5)*3+50] F, RF; long pow(long x, long n) { long y = 1; while (n) { if (n%2 == 1) y = (y * x) % P; x = x^^2 % P; n /= 2; } return y; } long inv(long x) { return pow(x, P-2); } void init() { F[0] = F[1] = 1; foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P; { RF[$-1] = 1; auto x = F[$-1]; auto k = P-2; while (k) { if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P; x = x^^2 % P; k /= 2; } } foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P; } long comb(N)(N n, N k) { if (k > n) return 0; auto n_b = F[n]; // n! auto nk_b = RF[n-k]; // 1 / (n-k)! auto k_b = RF[k]; // 1 / k! auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k! return (n_b * nk_b_k_b) % P; // n! / (n-k)!k! } void main() { init(); auto nabk = readln.split.to!(long[]); auto N = nabk[0]; auto A = nabk[1]; auto B = nabk[2]; auto K = nabk[3]; long r; foreach (a; 0..N+1) { auto ap = A * a; auto bp = K - ap; if (bp < 0 || bp%B != 0 || bp/B > N) continue; auto b = bp/B; (r += comb(N, a) * comb(N, b) % P) %= P; } writeln(r); }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; bool isConnected(int[26][] c) { bool[26] appear; foreach(i; 0..26) { foreach(j; 0..26) { if(c[i][j]) { appear[i] = true; appear[j] = true; } } } bool[26] arrived; int[] next; foreach(i; 0..26){ foreach(j; 0..26) { if(c[i][j]){ arrived[i] = true; arrived[j] = true; next ~= j; } } if(!next.empty()) break; } while(!next.empty()) { int[] newNext; foreach(t; next) { foreach(i; 0..26) { if(!arrived[i] && c[t][i]){ arrived[i] = true; newNext ~= i; } } } next = newNext; } return appear == arrived; } bool isEuler(int[26][] c) { foreach(i; 0..26) { int to, from; foreach(j; 0..26) { to += c[j][i]; from += c[i][j]; } if(to != from) return false; } return true; } void main() { auto n = to!int(readln().strip()); while(n) { int[26][26] c; bool[26] appear; foreach(i; 0..n) { auto l = readln().strip(); int s = l[0]-'a'; int t = l[$-1]-'a'; appear[s] = true; appear[t] = true; ++c[s][t]; } if(isConnected(c) && isEuler(c)) writeln("OK"); else writeln("NG"); n = to!int(readln().strip()); } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { auto n = lread(); auto a = aryread(); // writeln(a); auto s = new long[](n + 1); s[1] = a[0]; foreach (i; 1 .. n) { s[i + 1] = s[i] + a[i]; } // writeln("s:", s); long ans = long.max; foreach (i; 0 .. n - 1) { // writeln(a[0 .. i + 1], a[i + 1 .. $]); //sを使って左と右の長さを求める // s[i + 1].writeln(); //左 // (s[$ - 1] - s[i + 1]).writeln(); //右 ans = min(ans, abs(s[i + 1] - (s[$ - 1] - s[i + 1]))); } writeln(ans); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
void main() { auto N = ri; pow(2, log2(N).to!int).writeln; } // =================================== import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { writeln(readln.chomp[0..$-8]); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { readln; auto ws = readln.split.to!(int[]); int ret, l, r; foreach (w; ws) r += w; ret = abs(l-r); foreach (w; ws) { r -= w; l += w; ret = min(ret, abs(l-r)); } writeln(ret); }
D
import std.stdio, std.string, std.conv, std.array, std.algorithm; void main() { auto a = readln.split.map!(to!int); if (a[0] >= a[1]) { writeln("No"); return; } if (a[1] >= a[2]) { writeln("No"); return; } writeln("Yes"); }
D
import std.stdio, std.conv, std.array,std.string; void main() { string[] input=readln.split; int a=input[0].to!int,b=input[1].to!int; writeln((a-1)*(b-1)); }
D
import std.stdio, std.string, std.conv, std.math, std.algorithm, std.range; // indexから始めるstringのstartIndexを返す ulong calculateIndex( ulong index ) { index--; ulong sum = 0; ulong sumNumDiv3 = 0; ulong sumNumDiv5 = 0; ulong sumNumDiv15 = 0; ulong digit = 1; while ( 10 ^^ digit < index ) { immutable ulong ulim = ( 10 ^^ digit ) - 1; immutable ulong numDiv3 = ulim / 3 - sumNumDiv3; immutable ulong numDiv5 = ulim / 5 - sumNumDiv5; immutable ulong numDiv15 = ulim / 15 - sumNumDiv15; immutable ulong rest = ulim - ( 10 ^^ ( digit - 1 ) - 1 ) - numDiv3 - numDiv5 + numDiv15; sum += ( numDiv3 + numDiv5 ) * 4 + rest * digit; sumNumDiv3 += numDiv3; sumNumDiv5 += numDiv5; sumNumDiv15 += numDiv15; digit++; } immutable ulong numDiv3 = index / 3 - sumNumDiv3; immutable ulong numDiv5 = index / 5 - sumNumDiv5; immutable ulong numDiv15 = index / 15 - sumNumDiv15; immutable ulong rest = index - ( 10 ^^ ( digit - 1 ) - 1 ) - numDiv3 - numDiv5 + numDiv15; sum += ( numDiv3 + numDiv5 ) * 4 + rest * digit; return sum + 1; } ulong binarySearch( immutable ulong n ) { ulong left = 0; ulong right = 10UL ^^ 18; while ( right > left + 1 ) { immutable ulong middle = ( left + right ) / 2; immutable ulong val = calculateIndex( middle ); if ( val < n ) { left = middle; } else if ( val > n ) { right = middle; } else { return middle; } } if ( calculateIndex( left + 1 ) <= n ) { left++; } return left; } ulong calculateDigit( ulong n ) { return convert( n ).length; } string convert( ulong n ) { if ( n % 15 == 0 ) return "FizzBuzz"; else if ( n % 3 == 0 ) return "Fizz"; else if ( n % 5 == 0 ) return "Buzz"; else return to!string( n ); } void main() { immutable ulong n = to!ulong( strip( readln ) ); solve( n ); } void solve( ulong n ) { ulong index = binarySearch( n ); ulong startIndex = n - calculateIndex( index ); ulong sum = calculateDigit( index ) - startIndex; write( convert( index )[ startIndex .. $ ] ); index++; while ( sum < 20UL ) { ulong endIndex = min( calculateDigit( index ), 20UL - sum ); write( convert( index )[ 0 .. endIndex ] ); index++; sum += endIndex; } writeln(); }
D
import std.stdio; import std.string; char[8][8] ch; void main(){ foreach (i; 0..8) ch[i] = chomp(readln()); for (int i = 1; i <= 3; i++) { rotate(); writeln(i*90); print(); } } void rotate(){ char[8][8] tmp; foreach (i; 0..8) foreach (j; 0..8) tmp[i][j] = ch[7-j][i]; foreach (i; 0..8) foreach (j; 0..8) ch[i][j] = tmp[i][j]; } void print(){ foreach (i; 0..8) { foreach (j; 0..8) { write(ch[i][j]); } writeln(); } }
D
import std.algorithm, std.string, std.array, std.stdio, std.range, std.conv; void main() { int[] AB = readln.chomp.split.to!(int[]); int A = AB[0], B = AB[1]; writeln([(A % 3), (B % 3), ((A + B) % 3)].any!(x => x == 0) ? "Possible" : "Impossible"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; void main() { auto hw = readln.split.to!(int[]); auto H = hw[0]; auto W = hw[1]; auto M = new int[][](H, W); foreach (i; 0..H) foreach (j, a; readln.split.to!(int[])) M[i][j] = a; foreach (i; 0..H) foreach (j, b; readln.split.to!(int[])) M[i][j] = abs(M[i][j] - b); auto max_d = 80*(H+W); auto DP = new int[][][](H, W, max_d+1); foreach (ref dp1; DP) foreach (ref dp2; dp1) dp2[] = -1; int solve(int i, int j, int d) { if (i == H-1 && j == W-1) return abs(d - M[i][j]); if (DP[i][j][d] == -1) { auto d1 = d + M[i][j]; auto d2 = abs(d - M[i][j]); if (d1 > max_d) d1 = d2; int r = max_d; if (i < H-1) { r = min(r, solve(i+1, j, d1)); r = min(r, solve(i+1, j, d2)); } if (j < W-1) { r = min(r, solve(i, j+1, d1)); r = min(r, solve(i, j+1, d2)); } DP[i][j][d] = r; } return DP[i][j][d]; } writeln(solve(0, 0, 0)); }
D
import std; // dfmt off T lread(T=long)(){return readln.chomp.to!T;} T[] lreads(T=long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);} void arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;} void scan(TList...)(ref TList Args){auto line = readln.split; foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}} void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length); static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}} alias sread=()=>readln.chomp();enum MOD =10^^9+7; alias PQueue(T,alias less="a<b") = BinaryHeap!(Array!T,less); // dfmt on void main() { auto S = sread(); auto T = sread(); writeln(S == T[0 .. $ - 1] ? "Yes" : "No"); }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.format; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void main() { long a,b,c; scan(a,b,c); if(a == b) { writeln(c); } if(b == c) { writeln(a); } if(c == a) { writeln(b); } }
D
void main() { long n = readln.chomp.to!long; bool[] cards = new bool[n]; long pair; foreach (i; 0 .. n) { long a = readln.chomp.to!long; if (i > 0 && cards[i-1] && a > 0) { ++pair; --a; } pair += a / 2; cards[i] = a % 2 == 1; } pair.writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test ABCABC ---+/ /+---test C ---+/ /+---test ABCACCBABCBCAABCB ---+/ void main(string[] args) { const S = readln.chomp.replace("BC", "D").splitter!(x => x == 'B' || x == 'C').array; long ans; foreach (S1; S) { long a_cnt; foreach (s; S1) { if (s == 'A') { ++a_cnt; } else { ans += a_cnt; } } } ans.writeln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto s = RD!string; char c = 'z'; size_t pos; foreach (i, e; s) { if (e < c) { pos = i; c = e; } } ans[ti] = [c, ' ']; foreach (i, e; s) { if (i == pos) continue; ans[ti] ~= e; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.array; import std.string; import std.algorithm; string[char] next; static this() { next = [ 'A': "BD", 'B': "ACE", 'C': "BF", 'D': "AEG", 'E': "BDFH", 'F': "CEI", 'G': "DH", 'H': "EGI", 'I': "FH" ]; } bool movable(char c, char t) { return !next[c].find(t).empty; } void main() { loop: foreach (_; 0 .. 1000) { string s = readln.chomp; foreach (i; 1 .. s.length) { if (!s[i - 1].movable(s[i])) { continue loop; } } s.writeln; } }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}} void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}} void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}} void main() { int a, b, k; readV(a, b, k); if (a+k-1 < b-k+1) { foreach (i; a..a+k) writeln(i); foreach (i; b-k+1..b+1) writeln(i); } else { foreach (i; a..b+1) writeln(i); } }
D
void main() { problem(); } void problem() { auto N = scan!int; string solve() { int hon, pon, bon; int n = N % 10; if (n == 3) { return "bon"; } if (n == 0 || n == 1 || n == 6 || n == 8) { return "pon"; } return "hon"; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
void main() { auto K = ri; auto S = rs; if(S.length <= K) writeln(S); else { writeln(S[0..K] ~ "..."); } } // =================================== import std.stdio; import std.string; import std.functional; import std.algorithm; import std.range; import std.traits; import std.math; import std.container; import std.bigint; import std.numeric; import std.conv; import std.typecons; import std.uni; import std.ascii; import std.bitmanip; import core.bitop; T readAs(T)() if (isBasicType!T) { return readln.chomp.to!T; } T readAs(T)() if (isArray!T) { return readln.split.to!T; } T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { res[i] = readAs!(T[]); } return res; } T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) { auto res = new T[][](height, width); foreach(i; 0..height) { auto s = rs; foreach(j; 0..width) res[i][j] = s[j].to!T; } return res; } int ri() { return readAs!int; } long rl() { return readAs!long; } double rd() { return readAs!double; } string rs() { return readln.chomp; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto K = RD; auto x = min(A, K); A -= x; K -= x; auto y = min(B, K); B -= y; writeln(A, " ", B); stdout.flush; debug readln; }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string n = chomp(readln()); int a = to!int(n); if(a%10 == a/100){ writeln("Yes"); } else { writeln("No"); } }
D
/+ dub.sdl: name "F" dependency "dcomp" version=">=0.6.0" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.datastructure.lazyseg; int main() { auto sc = new Scanner(stdin); int n, m; sc.read(n, m); int[] l = new int[n], r = new int[n]; int[][] rg = new int[][m+2]; foreach (i; 0..n) { sc.read(l[i], r[i]); rg[r[i]] ~= i; } auto seg = LazySeg!(int, int, (a, b) => min(a, b), (a, b) => a+b, (a, b) => a+b, 10^^9, 0)(m+2); foreach (i; 0..m+2) { seg[i] = i; } int ans = min(n, m); int off = n; foreach_reverse (ri; 2..m+2) { //right use [ri .. m+1] int buf = m+1 - ri; //add r[i] = ri foreach (i; rg[ri]) { off--; seg[0..l[i]] += 1; } ans = min(ans, buf + off + seg[0..ri-1].sum()); } writeln(n - ans); return 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.foundation; static if (__VERSION__ <= 2070) { template fold(fun...) if (fun.length >= 1) { auto fold(R, S...)(R r, S seed) { import std.algorithm : reduce; static if (S.length < 2) { return reduce!fun(seed, r); } else { import std.typecons : tuple; return reduce!fun(tuple(seed), r); } } } } version (X86) static if (__VERSION__ < 2071) { import core.bitop : bsf, bsr, popcnt; int bsf(ulong v) { foreach (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int bsr(ulong v) { foreach_reverse (i; 0..64) { if (v & (1UL << i)) return i; } return -1; } int popcnt(ulong v) { int c = 0; foreach (i; 0..64) { if (v & (1UL << i)) c++; } return c; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f) { this.f = f; } char[512] lineBuf; char[] line; private bool succ() { import std.range.primitives : empty, front, popFront; import std.ascii : isWhite; while (true) { while (!line.empty && line.front.isWhite) { line.popFront; } if (!line.empty) break; if (f.eof) return false; line = lineBuf[]; f.readln(line); } return true; } private bool readSingle(T)(ref T x) { import std.algorithm : findSplitBefore; import std.string : strip; import std.conv : parse; if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { auto r = line.findSplitBefore(" "); x = r[0].strip.dup; line = r[1]; } else { auto buf = line.split.map!(to!E).array; static if (isStaticArray!T) { assert(buf.length == T.length); } x = buf; line.length = 0; } } else { x = line.parse!T; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/datastructure/lazyseg.d */ // module dcomp.datastructure.lazyseg; struct LazySeg(T, L, alias opTT, alias opTL, alias opLL, T eT, L eL) { const int n, sz, lg; T[] d; L[] lz; @disable this(); this(int n) { import std.algorithm : fill, each; import core.bitop : bsr; if (n == 0) return; int lg = n.bsr; if ((2^^lg) < n) lg++; this.n = n; this.sz = 2^^lg; this.lg = lg; d = new T[](2*this.sz); d.each!((ref x) => x = eT); lz = new L[](2*this.sz); lz.each!((ref x) => x = eL); } private void lzAdd(int k, L x) { d[k] = opTL(d[k], x); lz[k] = opLL(lz[k], x); } private void push(int k) { if (lz[k] == eL) return; lzAdd(2*k, lz[k]); lzAdd(2*k+1, lz[k]); lz[k] = eL; } T sum(int a, int b, int l, int r, int k) { if (b <= l || r <= a) return eT; if (a <= l && r <= b) return d[k]; push(k); int md = (l+r)/2; return opTT(sum(a, b, l, md, 2*k), sum(a, b, md, r, 2*k+1)); } T single(int k) { k += sz; foreach_reverse (int i; 1..lg+1) { push(k>>i); } return d[k]; } void singleSet(T x, int k) { k += sz; foreach_reverse (int i; 1..lg+1) { push(k>>i); } d[k] = x; foreach (int i; 1..lg+1) { d[k>>i] = opTT(d[2*(k>>i)], d[2*(k>>i)+1]); } } T sum(int a, int b) { assert(0 <= a && a <= b && b <= n); return sum(a, b, 0, sz, 1); } void add(int a, int b, L x, int l, int r, int k) { if (b <= l || r <= a) return; if (a <= l && r <= b) { lzAdd(k, x); return; } push(k); int md = (l+r)/2; add(a, b, x, l, md, 2*k); add(a, b, x, md, r, 2*k+1); d[k] = opTT(d[2*k], d[2*k+1]); } void add(int a, int b, L x) { assert(0 <= a && a <= b && b <= n); add(a, b, x, 0, sz, 1); } @property int opDollar() const {return sz;} struct Range { LazySeg* seg; int start, end; @property T sum() { return seg.sum(start, end); } } T opIndex(int k) { assert(0 <= k && k < n); return single(k); } void opIndexAssign(T x, int k) { assert(0 <= k && k < n); singleSet(x, k); } int[2] opSlice(size_t dim)(int start, int end) { assert(0 <= start && start <= end && end <= sz); return [start, end]; } Range opIndex(int[2] rng) { return Range(&this, rng[0], rng[1]); } void opIndexOpAssign(string op : "+")(L x, int[2] rng) { add(rng[0], rng[1], x); } }
D
import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } class X{ } void main(){ // A=1、B=2として、区間の合計を3で割った余りが一致すればYES。一致しなければNO // xs[i]はSの先頭i文字を3で割った余りを保持する // ys[i]はTの先頭i文字を3で割った余りを保持する // 求めるものは (xs[b] - xs[a - 1]) == (ys[d] - ys[c - 1]) string s = readln.chomp; string t = readln.chomp; int[] xs = [0]; int x = 0; foreach(c; s){ if(c == 'A') x += 1; else x += 2; xs ~= x; } debug xs.writeln; int[] ys = [0]; int y = 0; foreach(c; t){ if(c == 'A') y += 1; else y += 2; ys ~= y; } debug ys.writeln; int q = read.to!int; for(int i = 0; i < q; i ++){ int a = read.to!int; int b = read.to!int; int c = read.to!int; int d = read.to!int; string ans; if((xs[b] - xs[a - 1]) % 3 == (ys[d] - ys[c - 1]) % 3) ans = "YES"; else ans = "NO"; ans.writeln; } }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.typecons; void main() { auto input = split(readln()); auto N = to!int(input[0]); long aM, aA, aR, aC, aH; // the number of the names that starts with ... foreach(i; 0..N) { auto S = split(readln())[0]; aM += (S[0] == 'M'? 1 : 0); aA += (S[0] == 'A'? 1 : 0); aR += (S[0] == 'R'? 1 : 0); aC += (S[0] == 'C'? 1 : 0); aH += (S[0] == 'H'? 1 : 0); } writeln( aM*aA*(aR+aC+aH) + aM*aR*(aC+aH) + aM*aC*aH + aA*aR*(aC+aH) + aA*aC*aH + aR*aC*aH ); }
D
import std.stdio, std.string, std.conv, std.range; import std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, std.random, core.bitop; enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long a, b, c; scan(a, b, c); auto ans = b; if (a + b >= c) { ans += c; } else { long t = a + b + 1; ans += t; } writeln(ans); } void scan(T...)(ref T args) { auto line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront; } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x > arg) { x = arg; isChanged = true; } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) if (x < arg) { x = arg; isChanged = true; } return isChanged; } void yes(bool ok, string y = "Yes", string n = "No") { return writeln(ok ? y : n); }
D
/* imports all std modules {{{*/ import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.net.curl, std.net.isemail, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.socket, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib; /*}}}*/ /+---test 5 1 3 5 4 2 ---+/ /+---test 9 9 6 3 2 5 8 7 4 1 ---+/ void main(string[] args) { readln; const P = readln.split.map!(to!long).array; long ans; foreach (i; 1..P.length-1) { if (min(P[i-1], P[i+1]) < P[i] && P[i] < max(P[i-1], P[i+1])) ++ans; } ans.writeln; }
D
import std.stdio; import std.numeric; import std.math; import std.algorithm; import std.string; import std.container; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } void main() { int p, q; scanf("%d %d\n", &p, &q); q /= gcd(p, q); int ans = 1; for (int n = 2; n * n <= q; n++) { if (q % n == 0) { while (q % n == 0) q /= n; ans *= n; } } ans *= q; writeln(ans); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; long ans; foreach (i; 1..N+1) { if (i % 2 == 0) continue; long cnt; foreach (j; 1..i+1) { if (i % j == 0) ++cnt; } if (cnt == 8) ++ans; } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.string, std.conv; void main() { int a = readln.chomp.to!int; string s = readln.chomp; writeln(a >= 3200 ? s : "red"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto kab = readln.split.to!(long[]); auto K = kab[0]; auto A = kab[1]; auto B = kab[2]; if (K <= A) { writeln(1); } else if (A - B <= 0) { writeln(-1); } else { writeln((K-B-1)/(A-B)*2 + 1); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto N = readln.chomp.to!int; int[] VS = [0], WS = [0]; foreach (_; 0..N) { auto vw = readln.split.to!(int[]); VS ~= vw[0]; WS ~= vw[1]; } auto Q = readln.chomp.to!int; auto len = min(2^^9+1, N+1); auto MAX_L = 10^^5; auto DP = new int[][](len, MAX_L+1); foreach (i; 1..len) { foreach (w; 0..MAX_L+1) { DP[i][w] = max(DP[i][w], DP[i/2][w]); if (w - WS[i] >= 0) DP[i][w] = max(DP[i][w], DP[i/2][w-WS[i]] + VS[i]); } } foreach (_; 0..Q) { auto vl = readln.split.to!(int[]); auto v = vl[0]; auto L = vl[1]; if (v < len) { writeln(DP[v][L]); } else { int solve(int i, int l) { if (i < len) return DP[i][l]; auto r1 = solve(i/2, l); int r2; if (l - WS[i] >= 0) r2 = solve(i/2, l - WS[i]) + VS[i]; return max(r1, r2); } writeln(solve(v, L)); } } }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.math; auto s=readln.chomp.to!(char[]); char[] t; foreach(c; s)if(c!='x') t~=c; bool ok=true; foreach(i; 0..(t.length/2)) ok&=(t[i]==t[$-i-1]); if(!ok){writeln(-1); return;} int cnt=0; int[] y; foreach(c; s){ if(c=='x'){ cnt++; }else{ y~=cnt; cnt=0; } } y~=cnt; int sum=0; foreach(i; 0..(y.length/2)) sum+=(y[i]-y[$-i-1]).abs; writeln(sum); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }
D
import std.stdio; void main() { int n, q; scanf("%d %d\n", &n, &q); auto c1 = 'X'; int cnt = 0; int[] cache = []; auto s = readln; foreach (c; s) { if (c1 == 'A' && c == 'C') { ++cnt; } c1 = c; cache ~= cnt; } foreach(_;0..q) { int l, r; scanf("%d %d", &l, &r); writeln(cache[r-1] - cache[l-1]); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range; enum P = 998244353L; void main() { auto nk = readln.split.to!(int[]); auto N = nk[0]; auto K = nk[1]; int[] LS, RS; foreach (_; 0..K) { auto lr = readln.split.to!(int[]); LS ~= lr[0]; RS ~= lr[1]; } auto DP = new long[](N+1); DP[1] = 1; DP[2] = P - 1; foreach (i; 1..N+1) { (DP[i] += DP[i-1]) %= P; foreach (j; 0..K) { auto l = LS[j]; auto r = RS[j]+1; if (i+l <= N) (DP[i+l] += DP[i]) %= P; if (i+r <= N) (DP[i+r] += P - DP[i]) %= P; } } writeln(DP[N]); }
D
import std.container; import std.range; import std.algorithm; import std.array; import std.string; import std.conv; import std.stdio; import std.container; void chmax(T)(ref T a, in T b) { if (a < b) { a = b; } } void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split(' ').map!(to!int).array; long s = 0; foreach (i;1..n) { if (a[i] < a[i - 1]) { auto d = a[i - 1] - a[i]; a[i] += d; s += d; } } writeln(s); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void main() { auto n = RD!int; auto s = RD!string; long l, r; foreach (i; 0..n) { if (s[i] == 'L') ++l; else ++r; } writeln(l+r+1); stdout.flush; debug readln; }
D
import std.stdio,std.conv,std.string; void main(){ auto args = readln().chomp().split(); auto a = to!int(args[0]); auto b = to!int(args[1]); writeln( a<b?"a < b":a==b?"a == b":"a > b" ); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 10^^9 + 7; void main() { int n, k; scan(n, k); auto h = readln.split.to!(int[]); auto dp = new int[](n); foreach (i ; 1 .. n) { dp[i] = inf; foreach (j ; max(0, i - k) .. i) { dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j])); } } writeln(dp[n-1]); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.stdio, std.array, std.conv; void main(){ string s; int i = 0,j = 0; int[] result; for(;;){ s=readln(); if(stdin.eof()) break; result.length += 1; string[] input = split(s); int a = to!int(input[0]); int b = to!int(input[1]); int k = 0; int keta = 1; while(a+b >= keta){ keta *= 10; ++k; } result[i] = k; ++i; } while(j < result.length){ writeln(result[j]); ++j; } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto S = readln.split.map!(to!int); auto N = S[0]; auto K = S[1]; auto A = readln.split.map!(to!long).array; long ans = 0; long tmp = 0; long s = A.map!(a => max(0, a)).sum; foreach (i; 0..K-1) { tmp += A[i]; s -= max(0, A[i]); } foreach (i; 0..N-K+1) { tmp += A[i+K-1]; s -= max(0, A[i+K-1]); ans = max(ans, s + max(0, tmp)); tmp -= A[i]; s += max(0, A[i]); } ans.writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto k = readln.chomp.to!int; auto x = readln.chomp.split.to!(int[]); int res; foreach (e; x) { auto a = k - e; res += min(a, e) * 2; } res.writeln; }
D
void main() { string s = readln.chomp; writeln( s.canFind('N') == s.canFind('S') && s.canFind('W') == s.canFind('E') ? "Yes" : "No" ); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* x: i日目にAをするときの最大 y: i日目にBをするときの最大 z: i日目にCをするときの最大 漸化式は x = as[i] + max(y, z) y = bs[i] + max(x, z) z = cs[i] + max(x, y) */ void main(){ long n = read.to!long; long[3][] xs; foreach(i; 0 .. n){ xs ~= [read.to!long, read.to!long, read.to!long]; } long[3] ans = [0, 0, 0]; foreach(x; xs){ ans = [ x[0] + max(ans[1], ans[2]), x[1] + max(ans[0], ans[2]), x[2] + max(ans[0], ans[1]) ]; } max(ans[0], ans[1], ans[2]).writeln; }
D
import std.stdio; import std.algorithm; import std.conv; import std.datetime; import std.numeric; import std.math; import std.string; string my_readln() { return chomp(readln()); } void main() { auto tokens = split(my_readln()); auto A = to!uint(tokens[0]); auto B = to!uint(tokens[1]); auto K = to!uint(tokens[2]); foreach (i; 0..K) { if (i % 2 == 0) { if (A % 2 == 1) --A; B += A / 2; A /= 2; } else { if (B % 2 == 1) --B; A += B / 2; B /= 2; } } writeln(A, " ", B); stdout.flush(); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N, A, B; scan(N, A, B); auto X = aryread(); long ans; foreach (i; 0 .. N - 1) { ans += min(A * (X[i + 1] - X[i]), B); } writeln(ans); } /// 素因数分解 long[long] factorize(long x) { assert(0 < x, "x is negative"); long[long] ps; while ((x & 1) == 0) { x /= 2; ps[2] = (2 in ps) ? ps[2] + 1 : 1; } for (long i = 3; i * i <= x; i += 2) while (x % i == 0) { x /= i; ps[i] = (i in ps) ? ps[i] + 1 : 1; } if (x != 1) ps[x] = (x in ps) ? ps[x] + 1 : 1; return ps; }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; import std.container; enum MAX = 1_000_100; ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); if(n % 2) { writeln(0); } else { long five_cnt; n /= 2; while(n > 0) { n /= 5; five_cnt += n; } writeln(five_cnt); } } T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; int ret; foreach (_; 0..N) { auto lr = readln.split.to!(int[]); ret += lr[1] - lr[0] + 1; } writeln(ret); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto B = new string[](N); foreach (i; 0 .. N) B[i] = sread(); long M = lread(); auto R = new string[](M); foreach (i; 0 .. M) R[i] = sread(); long[string] d; foreach (b; B) d[b] = d.get(b, 0) + 1; foreach (r; R) d[r] = d.get(r, 0) - 1; long ans = 0; foreach (k, v; d) ans = ans.max(v); writeln(ans); }
D
void main() { string[] s = readln.split; foreach (i; 0 .. 3) { s[i][0].toUpper.write; } writeln; } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.uni;
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long n, k; scan(n, k); auto a = aryread(); auto inversion = new long[](n); auto lower_than = new long[](n); foreach(i; iota(n)) { foreach (j; iota(i)) if(a[i] > a[j]) lower_than[i]++; foreach(j; iota(i + 1, n)) if(a[i] > a[j]) inversion[i]++, lower_than[i]++; } auto ans = k * inversion.sum() % MOD; foreach (e; lower_than) { auto s = (k * (k - 1) / 2) % MOD; ans += (e * s) % MOD; ans %= MOD; } writeln(ans % MOD); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; import std.range; import std.math; void main() { while (true) { int n = readln.chomp.to!int; if (n == 0) break; int[] s; foreach (i; 0..n) { s ~= readln.chomp.to!int; } writeln((s.reduce!("a + b") - s.minPos[0] - s.minPos!("a > b")[0]) / (n - 2)); } }
D
import std.stdio; import std.algorithm; import std.math; import std.string; import std.conv; import std.range; void main() { while (true) { int l = readln.chomp.to!int; if (l == 0) break; int[][] ary; for (int i = 0; i < 12; i++) { ary ~= readln.chomp.split.map!(to!int).array; } int sum = 0; int month = 0; foreach (int i, e; ary) { sum += e[0] - e[1]; if (sum >= l) { month = i + 1; break; } } writeln(month == 0 ? "NA" : month.to!string); } }
D
import std; alias sread = () => readln.chomp(); alias lread = () => readln.chomp.to!long(); alias aryread(T = long) = () => readln.split.to!(T[]); //aryread!string(); //auto PS = new Tuple!(long,string)[](M); //x[]=1;でlong[]全要素1に初期化 void main() { long n, m; scan(n, m); auto 色 = new bool[](n); 色[0] = true; auto 個数 = new long[](n); 個数[] = 1; foreach (i; 0 .. m) { long x, y; scan(x, y); // writeln(x, y); 個数[x - 1] -= 1; 個数[y - 1] += 1; if (色[x - 1]) { 色[y - 1] = true; } if (個数[x - 1] == 0) { 色[x - 1] = false; } } // writeln(個数); // writeln(色); long cnt; foreach (i; 0 .. n) { if (色[i] == true && 個数[i] >= 0) { cnt += 1; } } writeln(cnt); } void scan(L...)(ref L A) { auto l = readln.split; foreach (i, T; L) { A[i] = l[i].to!T; } } void arywrite(T)(T a) { a.map!text.join(' ').writeln; }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; void main() { auto n = readln.chomp.to!int; auto a = readln.chomp.split.map!(to!int); auto b = new int[](n); int cnt; foreach (i; 0..n) { if (a[i] < i+1) { if (b[a[i]-1] == i+1) { cnt++; } } else { b[i] = a[i]; } } cnt.writeln; }
D
import std.stdio, std.string, std.conv, std.algorithm, std.numeric; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; int[] di = [1,0,-1,0,1,1,-1,-1], dj = [0,1,0,-1,1,-1,1,-1]; int ans; void main() { while (1) { int w, h; scan(w, h); if (!w) return; auto c = iota(h).map!(i => readln.split.to!(int[])).array; int ans; foreach (i ; 0 .. h) { foreach (j ; 0 .. w) { if (c[i][j]) { dfs(h, w, c, i, j); ans++; } } } writeln(ans); } } void dfs(int h, int w, int[][] c, int i, int j) { c[i][j] = 0; foreach (k ; 0 .. 8) { int ni = i + di[k]; int nj = j + dj[k]; if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue; if (c[ni][nj]) { dfs(h, w, c, ni, nj); } } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import core.bitop; import std.algorithm; import std.ascii; import std.bigint; import std.conv; import std.functional; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.random; import std.typecons; alias sread = () => readln.chomp(); alias Point2 = Tuple!(long, "y", long, "x"); T lread(T = long)() { return readln.chomp.to!T(); } T[] aryread(T = long)() { return readln.split.to!(T[])(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } } void minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } void main() { auto s = sread(); writeln(s.map!(c => cast(long)(c - '0')).sum()); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto N = RD; auto M = RD; auto H = RDA; auto edges = new long[][](N); foreach (i; 0..M) { auto A = RD-1; auto B = RD-1; edges[A] ~= B; edges[B] ~= A; } long ans; foreach (i; 0..N) { auto x = H[i]; bool ok = true; foreach (to; edges[i]) { auto y = H[to]; if (y >= x) ok = false; } if (ok) ++ans; } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.algorithm, std.string, std.array; long seq(int a, int m) { int cnt = 1; int x = 1; while(true) { x = (a * x) % m; if (x == 1) break; else cnt++; } return cnt.to!long; } long gcd(long a, long b) { if (a > b) swap(a, b); while(true) { b %= a; if (b == 0) break; else swap(a, b); } return a; } long lcm(long a, long b) { return a * b / gcd(a, b); } long lcms(long[] arr) { long res = arr[0]; for(int i = 1; i < arr.length; i++) { res = lcm(res, arr[i]); } return res; } void main() { while(true) { int[] a = readln.chomp.split(" ").map!(to!int).array; if (a[0] == 0) break; lcms([seq(a[0], a[1]), seq(a[2], a[3]), seq(a[4], a[5])]).writeln; } }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; const W = 0; const B = 1; class S { public int stone; public int count; this(int stone, int count) { this.stone = stone; this.count = count; } } S[] group(string s) { S[] ss; int p = 0; int stone = -1; int count = 0; while (p < s.length) { int c = s[p] == '.' ? W : B; if (stone == c) { count++; } else { if (count > 0) ss ~= new S(stone, count); stone = c; count = 1; } p++; } if (count > 0) ss ~= new S(stone, count); /* foreach (a; ss) { writeln(a.stone == W ? "W" : "B", " ", a.count); } */ return ss; } int calc(string s) { S[] ss = group(s); int[] ww = new int[ss.length]; // [0, i] を全て W にするコスト int[] bb = new int[ss.length]; // [i, $] を全て B にするコスト int cost = 0; for (int i = 0; i < ss.length; i++) { // [0, i] を全て W にするコスト if (ss[i].stone == B) cost += ss[i].count; ww[i] = cost; } cost = 0; for (int i = cast(int)ss.length-1; i >= 0; i--) { if (ss[i].stone == W) cost += ss[i].count; bb[i] = cost; } int ans = min(ww[$-1], bb[0]); for (int i = 0; i < ss.length; i++) { // [0, i] を白、[i+1, $-1] を黒にするコスト int c = ww[i] + (i + 1 < ss.length ? bb[i + 1] : 0); ans = min(ans, c); } return ans; } void main() { readint; string s = read!string; int ans = calc(s); writeln(ans); }
D