code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio, std.array, std.conv, std.string; void main() { string[] input = split(readln()); int[] a = new int[3]; for (int i = 0; i < 3; ++i) { a[i] = to!int(input[i]); } if (a[0] < a[1] && a[1] < a[2]) { writeln("Yes"); } else { writeln("No"); } }
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 M = s[1]; auto edges = new int[][](N); foreach (_; 0..M) { s = readln.split.map!(to!int); edges[s[0]-1] ~= s[1]-1; edges[s[1]-1] ~= s[0]-1; } auto used = new bool[][](N, 2); void dfs(int n, int x) { if (used[n][x]) return; used[n][x] = true; foreach (m; edges[n]) dfs(m, x^1); } dfs(0, 0); long a = used.map!(u => u[0]).sum; long b = used.map!(u => u[1]).sum; long c = used.map!(u => u[0] && u[1]).sum; writeln(a * b - M - c - c * (c - 1) / 2); }
D
import std.stdio, std.conv, std.string; void main() { auto ip = readln.split.to!(int[]), n = ip[0], m = ip[1]; writeln((n - 1)*(m - 1)); }
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; 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]; } const real eps = 1e-10; long count(int n, int k, int s, long[int[]] memo) { if(([n,k,s] in memo) !is null) { return memo[[n,k,s]]; } if(n <= 0 || s <= 0) { return 0; } if(k == 1) { if(n >= s) { return 1; } else { return 0; } } long ans; for(int i = 1; i <= n; ++i) { ans += count(i-1, k-1, s-i, memo); } memo[[n, k, s].idup] = ans; return ans; } void main(){ while(true) { auto nks = readInts(); auto n = nks[0]; auto k = nks[1]; auto s = nks[2]; if((n|k|s) == 0) return; long[int[]] memo; writeln(count(n, k, s, memo)); } }
D
import std.stdio,std.string,std.conv,std.algorithm; void main(){ auto readline = to!int(readln().chomp()); for( ; 0 < readline ; readline-- ){ auto arr = readln().chomp().split(); check( to!int(arr[0]),to!int(arr[1]),to!int(arr[2])); } } void check(int x,int y,int z){ if( x*x+y*y==z*z || x*x+z*z==y*y || y*y+z*z==x*x ){ writeln("YES"); }else{ writeln("NO"); } }
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; const MOD = 10^^9 + 7; int calc(string s) { auto dp = new int[][](s.length + 1, 13); // dp[i][j] // i : i 桁目までみた // j : 13 で割った余 // dp[0][0] = 1; if (s[0] == '?') { foreach (i; 0..10) { dp[1][i % 13]++; } } else { int d = s[0] - '0'; dp[1][d]++; } for (int i = 1; i < s.length; i++) { // i 桁目 for (int j = 0; j < 13; j++) { // 余り if (s[i] == '?') { // 0..9 foreach (k; 0..10) { dp[i + 1][(10 * j + k) % 13] += dp[i][j]; dp[i + 1][(10 * j + k) % 13] %= MOD; } } else { int d = s[i] - '0'; dp[i + 1][(10 * j + d) % 13] += dp[i][j]; dp[i + 1][(10 * j + d) % 13] %= MOD; } } } // foreach (a; dp) writeln(a); return dp[s.length][5]; } void main() { string s = read!string; writeln(calc(s)); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.range; void main() { auto N = readln.chomp.to!int; auto A = readln.chomp.to!int; writeln(N % 500 <= A ? "Yes" : "No"); }
D
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container, std.typecons; void main() { auto s = readln.chomp; auto t = readln.chomp[0..$-1]; if(s==t) writeln("Yes"); else writeln("No"); }
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 = new int[](n+1); long res; foreach (i; 1..n+1) { a[i] = readln.chomp.to!int; if (a[i-1]) { auto num = min(a[i-1], a[i]); a[i-1] -= num; a[i] -= num; res += num; } auto num = a[i] / 2; res += num; a[i] = a[i] % 2; } res.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)); } //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); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; int l = n, r = -1; foreach (int i; 0..n) { if (s[i] == '1') { l = i; break; } } foreach_reverse (int i; 0..n) { if (s[i] == '0') { r = i; break; } } if (l < r) ans[ti] = s[0..l] ~ '0' ~ s[r+1..s.length]; else ans[ti] = s[0..l] ~ s[r+1..s.length]; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import core.stdc.stdio; import std.typecons; import std.algorithm; void main(){ int n; long h; scanf("%d%lld",&n,&h); long[] ds = new long[n]; long[] hs = new long[n-1]; for(int i=0;i<n-1;i++) scanf("%lld%lld",&ds[i+1],&hs[i]); for(int i=1;i<n;i++) ds[i] += ds[i-1]; //ds.writeln; long heal=0; long ans=0; int b=0; int e=0; alias Tuple!(int,"prev",int,"next") pn; pn[] list = new pn[n]; for(int i=0;i<n;i++) list[i] = pn(i,i); for(int s=1;s<n;s++){ /* s.writeln; writeln(b," ",e," ",list[b].next); writeln("heal ",heal); writeln; */ while(h<=ds[s]-heal){ while(list[b].next != b){ int k = list[b].next; if(min(ds[b]-heal,hs[b])<=min(ds[k]-heal,hs[k])) b=k; else break; } list[b].prev = b; long t = min(ds[b]-heal,ds[s]-heal-h+1); //t.writeln; if(t>=hs[b]){ long c = t/hs[b]; ans += c; heal += hs[b]*c; }else{ ans++; heal += min(ds[b]-heal,hs[b]); } } if(s==n-1) break; list[s].prev = e; list[e].next = s; e = s; while(1){ int k=list[e].prev; if(hs[k]<=hs[e]){ if(k==b){ list[e].prev = e; b=e; break; }else{ list[e].prev = list[k].prev; list[list[e].prev].next = e; } }else break; } //writeln; } printf("%lld\n",ans); }
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; alias sread = () => readln.chomp(); long bignum = 1_000_000_007; 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, k; scan(a, b, c, k); if (k % 2) writeln(b - a); else writeln(a - b); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto ns = readln.split.to!(int[]); auto N = ns[0]; auto A = ns[1]; auto B = ns[2]; auto C = ns[3]; int[] ls; foreach (_; 0..N) ls ~= readln.chomp.to!int; auto as = new int[](N); int solve(int i) { if (i == N) { int a, b, c, an, bn, cn; foreach (j; 0..N) { switch (as[j]) { case 1: a += ls[j]; ++an; break; case 2: b += ls[j]; ++bn; break; case 3: c += ls[j]; ++cn; break; default: } } if (an == 0 || bn == 0 || cn == 0) return int.max; return abs(A - a) + abs(B - b) + abs(C - c) + (an-1)*10 + (bn-1)*10 + (cn-1)*10; } else { int r = int.max; foreach (j; 0..4) { as[i] = j; r = min(r, solve(i+1)); } return r; } } writeln(solve(0)); }
D
import std.algorithm; import std.bigint; import std.concurrency; import std.container; import std.conv; import std.functional; import std.format; import std.math; import std.meta; import std.numeric; import std.random; import std.range; import std.stdio; import std.string; import std.traits; import std.typecons; auto solve(string S) { if (S == "Sunny") return "Cloudy"; if (S == "Cloudy") return "Rainy"; if (S == "Rainy") return "Sunny"; return ""; } void main() { auto input = stdin.byLine.map!split.joiner; string S; S = input.front.to!string; input.popFront; static if (is(ReturnType!solve == void)) solve(S); else solve(S).writeln; }
D
void main() { auto N = rs; auto arr = [2,4,5,7,9]; auto arr2 = [0,1,6,8]; if(arr.canFind(N.back - '0')) { writeln("hon"); } else if(arr2.canFind(N.back - '0')) { writeln("pon"); } else writeln("bon"); } // =================================== 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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void solve() { auto N = readln.chomp.to!int; auto ps = readln.split.to!(int[]); auto ii = new size_t[](N); foreach (i, ref p; ps) { p -= 1; ii[p] = i; } auto bs = new bool[](N); size_t i = ii[0]; int n; for (;;) { bs[i] = true; ++i; ++n; if (n == N) break; if (i == N || bs[i]) { i = ii[n]; continue; } if (ps[i] != n) { writeln("No"); return; } } writeln("Yes"); } void main() { auto T = readln.chomp.to!int; foreach (_t; 0..T) { solve(); } }
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(string[] args) { int n = readln.chomp.to!int; string p = readln.chomp; string s = readln.chomp; for(int i = 0; i < n; i++){ if(p[i..$] == s[0..$-i]){ writeln(n+i); return; } } writeln(2*n); }
D
import std.stdio, std.string, std.conv; void main() { int[] buf = readln.chomp.split.to!(int[]); int N = buf[0], A = buf[1], B = buf[2]; int d = B - A; writeln(d & 1 ? "Borys" : "Alice"); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto a = readNums!int; foreach(i; 0 .. (2*a[0]-2)){ write(i + a[1]-a[0]+1, " "); } writeln(a[1]+a[0]-1); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; void main() { int n = readln.chomp.to!int; string a = readln.chomp.split.join; int m = readln.chomp.to!int; string b = readln.chomp.split.join; writeln(a < b ? 1 : 0); }
D
import std.stdio; import std.conv; import std.string; void main() { string s; while((s = readln()).length != 0) { int [] c = [500, 100, 50, 10, 5, 1]; int res = 0; int a; a = to!int(chomp(s)); if(a == 0) break; a = 1000 - a; for(int i = 0; i < c.length; i++) { if(a >= c[i]) { res++; a -= c[i]; i--; } } writeln(res); } }
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 readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);} void main() { int n, d0; readV(n, d0); int[] d; readA(n, d); int nq; readV(nq); int[] q; readA(nq, q); auto a = new int[](n+1); a[0] = d0; foreach (i; 0..n) a[i+1] = d[i] < a[i]*2 ? (a[i]-d[i]).abs : a[i]; auto b = new int[](n+1); b[n] = 1; foreach_reverse (i; 0..n) b[i] = b[i+1] <= d[i]/2 ? b[i+1] : b[i+1]+d[i]; foreach (qi; q) writeln(a[qi-1] >= b[qi] ? "YES" : "NO"); }
D
import std.stdio; import std.string; import std.array; // split import std.conv; // to void main() { string s = chomp(readln()); for(int i=0; i<18; ++i){ if(i == 5 || i == 13){ write(" "); } else { write(s[i]); } } writeln(s[18]); }
D
// dfmt off T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;} T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;} void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp(); void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}} static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop; // dfmt on void main() { long a, b, c, d; scan(a, b, c, d); if (a + b == c + d) { writeln("Balanced"); return; } if (a + b < c + d) { writeln("Right"); return; } writeln("Left"); }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.format, std.math; void main() { auto o = readln.chomp; auto e = readln.chomp; string p; if(o.length - e.length == 0){ foreach(i; 0..e.length){ p ~= o[i]; p ~= e[i]; } }else{ foreach(i; 0..o.length){ p ~= o[i]; p ~= e[i]; } p.popBack(); } writeln(p); }
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; 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); } } } 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; } enum inf = 1_001_001_001; enum infl = 1_001_001_001_001_001_001L; void main() { long N; scan(N); long ans = infl; for (long i = 1; i*i <= N; i++) { if (N % i == 0) { chmin(ans, f(i, N / i)); } } writeln(ans); } long f(long x, long y) { return max(x.to!string.length, y.to!string.length); }
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; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Goods = Tuple!(long, "w", long, "v"); 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() { auto s = sread(); long cnt, left, right = s.length - 1; bool batch = true; while (left <= right) { if (s[left] == s[right]) left++, right--; else if (s[left] == 'x') cnt++, left++; else if (s[right] == 'x') cnt++, right--; else { batch = false; break; } } if (batch) writeln(cnt); else writeln(-1); }
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; void main() { auto nq = readints; int n = nq[0], q = nq[1]; string s = read!string; // acc[i] = [0..1] までの AC の出現回数 auto acc = new int[s.length]; for (int i = 1; i < s.length; i++) { int x = s[i] == 'C' && s[i-1] == 'A'; acc[i] = x + acc[i - 1]; } foreach (_; 0..q) { auto lr = readints; int l = lr[0] - 1; int r = lr[1] - 1; int x = acc[r] - acc[l]; writeln(x); } }
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 ip1 = readln.chomp.to!int; auto ip2 = readln.chomp.to!int; auto ip3 = readln.chomp.to!int; auto ip4 = readln.chomp.to!int; (min(ip1, ip2) + min(ip3, ip4)).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)); } //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); } void main() { auto t = RD!int; auto ans = new string[](t); foreach (ti; 0..t) { auto n = RD!int; auto s = RD!string; foreach (i; 0..n) { ans[ti] ~= s[$/2]; } } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long[] AS, BS; void main() { auto N = readln.chomp.to!long; AS = readln.split.to!(long[]); BS = readln.split.to!(long[]); long[][3] DP; foreach (ref dp; DP) dp.length = N+1; foreach_reverse (j; 0..N+1) { foreach_reverse (i; 0..3) { long r1, r2; r1 = i == 2 ? N-j : DP[i+1][j]; if (j+AS[i] <= N) { r2 = DP[i][j+AS[i]] + BS[i]; } DP[i][j] = max(r1, r2); } } auto M = DP[0][0]; foreach (ref dp; DP) { dp[] = 0; dp.length = M+1; } foreach_reverse (j; 0..M+1) { foreach_reverse (i; 0..3) { long r1, r2; r1 = i == 2 ? M-j : DP[i+1][j]; if (j+BS[i] <= M) { r2 = DP[i][j+BS[i]] + AS[i]; } DP[i][j] = max(r1, r2); } } writeln(DP[0][0]); }
D
void main(){ auto NMX = readLine!size_t(); size_t minCost = size_t.max; size_t cost; size_t[][] bookList; foreach( n ; 0..NMX[0] ){ bookList ~= readLine!size_t(); } size_t[] skillList; foreach( i ; 0..(2.pow(NMX[0])) ){ skillList = new size_t[](NMX[1]); cost = 0; foreach( size_t j ; 0..NMX[0] ){ if ( ((i>>j)%2) == 1 ){continue;} skillList[0..$] += bookList[j][1..$]; cost += bookList[j][0]; } // skill is enough if ( skillList.minElement() >= NMX[2] ){ if( cost < minCost ){ minCost = cost; } //writeln("OK:",skillList,":cost",cost); } else { //writeln("NG:",skillList,":cost",cost); } } if( minCost == size_t.max ){ writeln(-1); } else { minCost.writeln(); } return; } import std; string readStr(){ return readln().chomp(); } T[] readLine( T = long )(){ return readln().split().to!(T[])(); }
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 nx = readln.split.to!(int[]); auto N = nx[0]; auto x = nx[1]; if (x == 1 || x == N*2-1) { writeln("No"); return; } writeln("Yes"); if (N == 2) { writeln("1\n2\n3"); return; } auto res = new int[](N*2-1); auto fs = new bool[](N*2); res[N-1] = x; fs[x] = true; res[N-2] = x-1; fs[x-1] = true; res[N] = x+1; fs[x+1] = true; if (x == 2) { res[N-3] = x+2; fs[x+2] = true; } else { res[N+1] = x-2; fs[x-2] = true; } int n = 1; foreach (ref r; res) if (r == 0) { while (fs[n]) ++n; r = n++; } foreach (ref r; res) writeln(r); }
D
// Vicfred // https://atcoder.jp/contests/abc176/tasks/abc176_c // greedy import std.algorithm; import std.array; import std.conv; import std.stdio; import std.string; void main() { long n = readln.chomp.to!long; long[] a = readln.split.map!(to!long).array; long last = a[0]; long ans = 0; for(int i = 1; i < n; i++) { if(a[i] < last) ans += last - a[i]; else last = a[i]; } ans.writeln; }
D
import std.stdio; import std.string; import std.conv; void main() { int c = 0; int[] numstack; string[] opstack; int calc() { ulong i = 0; int x = numstack[i++]; foreach (op; opstack) { switch (op) { case "+": x += numstack[i++]; break; case "-": x -= numstack[i++]; break; case "*": x *= numstack[i++]; break; case "/": x /= numstack[i++]; default: break; } } return x; } for (int i = 0; ; i++) { if (i % 2 == 0) { int n = stdin.readln.chomp.to!int; numstack ~= n; } else { string op = stdin.readln.chomp; if (op == "=") { calc.writeln; break; } else opstack ~= op; } } }
D
import std.stdio; // ??\???????????????????????? import std.string; // chomp????????????????????????(?????????????????????) import std.conv; // to???????????????????????? import std.array; // split????????????????????? import std.algorithm; // map????????????????????? void main() { int S = readln.chomp.to!int; int h = S / 3600; S = S - h * 3600; int m = S / 60; S = S - m * 60; writeln(h, ":", m, ":", S); }
D
import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math; void main() { int n, q; scan(n, q); auto sd = SqrtDecomp(n); foreach (_ ; 0 .. q) { auto line = readln.split.to!(int[]); if (line[0] == 0) { sd.add(line[1] - 1, line[2], line[3]); } else { writeln(sd.get(line[1] - 1)); } } } struct SqrtDecomp { private { enum rt = 512; int N, K; int[] bucket; int[] data; } this(int n) { N = n; K = (N + rt - 1) / rt; bucket = new int[](K); data = new int[](K * rt); } void add(int l, int r, int x) { foreach (k ; 0 .. K) { int bl = k * rt, br = (k + 1) * rt; if (r <= bl || br <= l) continue; if (l <= bl && br <= r) { bucket[k] += x; } else { foreach (i ; max(l, bl) .. min(r, br)) { data[i] += x; } } } } int get(int idx) { int k = idx / rt; foreach (i ; k * rt .. (k + 1) * rt) { data[i] += bucket[k]; } bucket[k] = 0; return data[idx]; } } 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() { import std.stdio, std.string, std.conv, std.algorithm; auto a = readln.split.to!(int[]); auto ch = ['A', 'B', 'C']; auto mx = a.reduce!(max); foreach (i; 0 .. 3) { if (a[i] == mx) { writeln(ch[i]); return; } } } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
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; alias sread = () => readln.chomp(); ulong bignum = 1_000_000_007; alias SugarWater = Tuple!(long, "swater", long, "sugar"); 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() { auto s = sread(); auto cost_right = new long[](s.length); auto cost_left = new long[](s.length); auto iter = iota(0, s.length); long count; foreach (i; iter) { if (s[i] == 'L') cost_left[i] = ++count; else { count = 0; } } count = 0; foreach_reverse (i; iter) { if (s[i] == 'R') cost_right[i] = ++count; else { count = 0; } } auto ans = new long[](s.length); foreach (i; iter) { if (s[i] == 'R') { if (cost_right[i] % 2) ans[i + cost_right[i] - 1]++; else ans[i + cost_right[i]]++; } else { if (cost_left[i] % 2) ans[i - cost_left[i] + 1]++; else ans[i - cost_left[i]]++; } } formatwrite(ans); // cost_left.writeln(); // cost_right.writeln(); } void formatwrite(T)(T a) { foreach (i, e; a) { if (i != a.length - 1) e.write(" "); else e.writeln(); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.typecons; void main() { auto n = readln.chomp.to!int; int min, max = -1; foreach (_; 0..n) { auto ab = readln.split.to!(int[]); if (ab[0] > max) { max = ab[0]; min = ab[1]; } } writeln(max + min); }
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 tmp = new long[](10 ^^ 5); foreach (i; 1 .. 101) { foreach (j; 1 .. 101) { foreach (k; 1 .. 101) { long tmptmp; tmptmp = i ^^ 2 + j ^^ 2 + k ^^ 2 + i * j + i * k + j * k; if (tmptmp <= 10 ^^ 4) { tmp[tmptmp] += 1; } } } } foreach (i; 1 .. n + 1) { writeln(tmp[i]); } } 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() { long n = readln.chomp.to!long; long a, b, ab; long cnt; foreach (i; 0 .. n) { string s = readln.chomp; cnt += s.count("AB"); if (s[$-1] == 'A' && s[0] == 'B') ++ab; else if (s[$-1] == 'A') ++a; else if (s[0] == 'B') ++b; } if (ab > 0) { cnt += ab - 1; if (max(a, b) > 0) cnt += min(a, b) + 1; } else { cnt += min(a, b); } cnt.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.stdio; import std.string; import std.algorithm; import std.exception; import std.conv; import std.array; import std.range; int toplamGuzellikSayisi = 0; void main() { auto satirSayisi = stdin.readln.strip.to!int(); char[] hulkunHisleri; for ( int i = 0; i < satirSayisi; i++ ) { if ( i == satirSayisi - 1 ) { if ( i % 2 ) hulkunHisleri ~= "I love it "; else hulkunHisleri ~= "I hate it "; } else if ( i % 2 ) hulkunHisleri ~= "I love that "; else hulkunHisleri ~= "I hate that "; } writeln(hulkunHisleri.chomp()); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Magic { int a; int b; } struct Input { int h; int n; Magic[] m; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto hn = readln().strip().split(); h = hn[0].to!int; n = hn[1].to!int; foreach (i; 0..n) { auto ab = readln().strip().split().map!(to!int).array; Magic temp; temp.a = ab[0]; temp.b = ab[1]; m ~= temp; } } } auto main2(Input* input) { int[10000+1] dp; dp[1..$] = int.max; for (int hp = 1; hp <= input.h; hp++) { foreach (m; input.m) { auto prevMp = m.a > hp ? 0 : dp[hp - m.a]; auto newMp = prevMp + m.b; if (newMp < dp[hp]) dp[hp] = newMp; } } return dp[input.h]; } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `9 3 8 3 4 2 2 1`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 4); } unittest // example2 { string example = `100 6 1 1 2 3 3 9 4 27 5 81 6 243`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 100); } unittest // example3 { string example = `9999 10 540 7550 691 9680 700 9790 510 7150 415 5818 551 7712 587 8227 619 8671 588 8228 176 2461`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == 139815); } unittest { writeln("end unittest"); } void parseExample(out Input input, string example) { struct Adapter { string[] _lines; this(string input) { _lines = input.splitLines(); } string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; } } parseInput(input, Adapter(example)); } } void printResult(T)(T result) { static if (isFloatingPoint!T) writefln("%f", result); else writeln(result); } void main() { Input input = void; parseInput(input, stdin); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto nab = readln.split.to!(long[]); auto n = nab[0]; auto a = nab[1]; auto b = nab[2]; auto hs = new long[n]; foreach (i; 0..n) { hs[i] = readln.chomp.to!long; } auto d = a - b; bool check(long n) { auto nn = n; foreach (h; hs) { h -= b * n; if (h <= 0) { continue; } else if (h <= d * nn) { nn -= (h + d - 1) / d; } else { return false; } } return true; } auto min = 0; auto max = 10^^9; while (min + 1 != max) { auto piv = (min + max) / 2; if (check(piv)) { max = piv; } else { min = piv; } } writeln(max); }
D
void main() { bool n, w, s, e; auto S = rs; foreach(c; S) { switch(c) { case 'N': n = true; break; case 'W': w = true; break; case 'S': s = true; break; case 'E': e = true; break; default: assert(0); } } if(n && !s || !n && s || w && !e || !w && e) writeln("No"); else writeln("Yes"); } // =================================== 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
// Your code here! import std.stdio; import std.range; import std.array; import std.algorithm; import std.conv; import std.string; void main(){ auto ab = readln().chomp().split().map!(to!long); auto a = ab[0]; auto b = ab[1]; solve(a, b).writeln(); } long solve(long a, long b){ auto eastIdx = b - a; long westLength = iota(1, eastIdx).sum; return westLength - a; }
D
import std; void main() { auto N = readln.split[0].to!long; auto A = readln.split.to!(long[]); ulong[long] Aminus; foreach (j; 0 .. N) { if (j-A[j] !in Aminus) Aminus[j-A[j]] = 0; Aminus[j-A[j]]++; } ulong ans; foreach (i; 0 .. N) { if (A[i]+i !in Aminus) continue; ans += Aminus[A[i]+i]; } writeln(ans); }
D
void main() { auto c = readMatrix!char(3, 3); foreach(i; 0..3) c[i][i].write; 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.string, std.conv; import std.typecons; import std.algorithm, std.array, std.range, std.container; import std.math; void main() { auto ABN = readln.split.to!(long[]); auto A = ABN[0], B = ABN[1], N = ABN[2]; auto x = min(B-1, N); writeln(cast(long) (floor(cast(double) A * cast(double) x / cast(double) B) - A * cast(double) floor(cast(double) x / cast(double) B))); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; int n, a; int[] x; void main() { scan(n, a); x = readln.split.to!(int[]); x[] -= a; auto dp = new long[][](n + 1, 5000 + 1); dp[0][2500] = 1; foreach (i ; 0 .. n) { foreach (j ; 0 .. 5000 + 1) { dp[i + 1][j] += dp[i][j]; dp[i + 1][j + x[i]] += dp[i][j]; } } writeln(dp[n][2500] - 1); } 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 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; const MOD = 10^^9 + 7; long calc(long k, int[] a) { long ans = 0; // a 内での転倒数 long p = 0; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) p++; } } ans += (p * k) % MOD; // 二つの a に対する転倒数 long q = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (a[i] > a[j]) q++; } } ans += q * (((k * (k - 1)) / 2) % MOD); // q * kC2 ans %= MOD; return ans; } void main() { int n, k; scan(n, k); auto a = readints; writeln(calc(k, a)); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container; void main() { auto nv = readln.split.to!(long[]); auto N = nv[0]; auto V = nv[1]; auto aa = readln.split.to!(long[]); auto bb = readln.split.to!(long[]); auto cc = readln.split.to!(long[]); auto dd = readln.split.to!(long[]); long[long] ab, cd; foreach (a; aa) foreach (b; bb) ab[a+b] += 1; foreach (c; cc) foreach (d; dd) cd[c+d] += 1; long r; foreach (x, n; ab) if (V-x in cd) r += n * cd[V-x]; writeln(r); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; int as; foreach (a; readln.split.to!(int[])) as += a; writeln(max(-1, N - as)); }
D
import std.stdio, std.string, std.conv; void main() { readln().chomp().toUpper().writeln(); }
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; auto D = RD; auto X = RD; foreach (i; 0..N) { auto a = RD; long b = 1; ++X; while (b + a <= D) { ++X; b += a; } } writeln(X); stdout.flush(); debug readln(); }
D
void main() { writeln(readln.split.join.to!int % 4 == 0 ? "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 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, "l ", long, "r"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { auto n = lread(); auto a = aryread(); auto b = aryread(); auto c = aryread(); long s = b.sum(); foreach (i; iota(n - 1)) { if(a[i + 1] - a[i] == 1) s += c[a[i] - 1]; } s.writeln(); } void scan(TList...)(ref TList Args) { auto line = readln.split(); foreach (i, T; TList) { T val = line[i].to!(T); Args[i] = val; } }
D
void main() { dchar[] s = readln.chomp.to!(dchar[]); long k = readln.chomp.to!long; long n = s.length; foreach (i, ref x; s) { if (i == n - 1) { s[i] = ((s[i] - 'a' + (k % 26)) % 26 + 'a').to!dchar; } else { long m = (26 - (s[i] - 'a')) % 26; if (k >= m) { s[i] = 'a'; k -= m; } else { continue; } } } s.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.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; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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 = 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 main() { auto s = RD!string; char best = s[0]; writeln("Mike"); foreach (c; s[1..$]) { if (c <= best) { writeln("Mike"); } else { writeln("Ann"); } best = min(best, c); } stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons; void main() { auto N = readln.chomp.to!int; long s; bool neq; long[] AS, BS; foreach (_; 0..N) { auto ab = readln.split.to!(long[]); auto A = ab[0]; auto B = ab[1]; s += A; if (A >= B) { AS ~= A; BS ~= B; } else { neq = true; } } if (!neq) { writeln(0); return; } long min_b = long.max; foreach (i; 0..AS.length) { if (AS[i] != BS[i] && BS[i] < min_b) min_b = BS[i]; } writeln(s - min_b); }
D
import std.stdio, std.algorithm, std.range, std.conv, std.string; import core.stdc.stdio; // foreach, foreach_reverse, writeln const int INF = 1_000_000_007; void main() { int n; scanf("%d", &n); int[] a = new int[n]; foreach (i; 0..n) scanf("%d", &a[i]); int p = 0, m = -INF; long val = -INF; foreach (parity; 0..2) { long sum = 0; int mx = -INF; foreach (i; 0..n) { if ((i&1) != parity) continue; if (a[i] > 0) sum += a[i]; else mx = max(mx, a[i]); } if (sum == 0) sum = mx; if (val < sum) { p = parity; val = sum; if (sum == mx) m = mx; else m = -INF; } } writeln(val); int[] ans; int len = n; int head = 0; foreach_reverse (i; 0..n) { if ((i&1) != p) continue; if (a[i] <= 0) { if (a[i] == m) { m = -INF; } else { ans ~= i+1; if (i == n-1) len--; else if (i == 0) len--, head = 1; else len -= 2; } } } if (p != head) ans ~= 1, len--; foreach (i; 0..len/2) ans ~= 2; writeln(ans.length); foreach (i; ans) writeln(i); }
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(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 bool[](t); foreach (ti; 0..t) { auto x = RD; auto y = RD; auto s = RD!string; long u, d, r, l; foreach (c; s) { if (c == 'U') ++u; else if (c == 'D') --d; else if (c == 'R') ++r; else --l; } ans[ti] = true; if (x > r || x < l) ans[ti] = false; if (y > u || y < d) ans[ti] = false; } foreach (e; ans) writeln(e ? "YES" : "NO"); stdout.flush; debug readln; }
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() { string s; scan(s); auto ok = s[0] == 'A' && s[2 .. $ - 1].count!"a == 'C'" == 1; foreach (ch ; s) { if (ch == 'A' || ch == 'C') continue; ok &= ('a' <= ch && ch <= 'z'); } writeln(ok ? "AC" : "WA"); } 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); } } } 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; }
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!int; writeln(n % 2 != 0 ? 0 : 2^^(n/2)); stdout.flush(); debug readln(); }
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; T lcm(T)(T a, T b) { return a * (b / gcd(a, b)); } long numDivisors(long n, long k) { return n / k; } long calc(long a, long b, long c, long d) { long x = numDivisors(b, c) + numDivisors(b, d) - numDivisors(b, lcm(c, d)); long y = numDivisors(a-1, c) + numDivisors(a-1, d) - numDivisors(a-1, lcm(c, d)); return (b - a + 1) - (x - y); } void main() { long a, b, c, d; scan(a, b, c, d); writeln(calc(a, b, c, d)); }
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; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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 = 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 main() { auto s = RD!string; auto len = s.length; bool flag; foreach (i; 0..len) { if (i == 0) continue; if (s[i] == '1') { flag = true;break; } } auto ans = (len-1) / 2; if (flag || (len-1) % 2 != 0) { ++ans; } writeln(ans); stdout.flush(); debug readln(); }
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; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; ulong MIDDLE = 100_000; alias Pair = Tuple!(long, "flag", long, "num"); 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() { auto n = lread(); auto s = new long[](n); foreach (ref e; s) e = lread(); auto score = s.sum(); if (!(score % 10)) { long subscore; foreach (e; s) { auto tmp = score - e; if (tmp % 10) subscore = max(tmp, subscore); } if(subscore) score = subscore; else score = 0; } score.writeln(); }
D
void main() { string s = rdStr; long[] cnts = new long[3]; foreach (x; s) { ++cnts[x-'a']; } cnts.sort!"a < b"; writeln(cnts[2] - cnts[0] < 2 ? "YES" : "NO"); } T rdElem(T = long)() { //import std.stdio : readln; //import std.string : chomp; //import std.conv : to; return readln.chomp.to!T; } alias rdStr = rdElem!string; dchar[] rdDchar() { //import std.conv : to; return rdStr.to!(dchar[]); } T[] rdRow(T = long)() { //import std.stdio : readln; //import std.array : split; //import std.conv : to; return readln.split.to!(T[]); } T[] rdCol(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdElem!T).array; } T[][] rdMat(T = long)(long col) { //import std.range : iota; //import std.algorithm : map; //import std.array : array; return iota(col).map!(x => rdRow!T).array; } void wrMat(T = long)(T[][] mat) { //import std.stdio : write, writeln; 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.container; import std.typecons; import std.ascii; import std.uni;
D
import std.stdio; import std.string; void main(){ int w, h; while(true){ scanf("%d %d", &h, &w); if (w == 0 && h == 0) break; foreach(i; 0..h){ foreach(j; 0..w){ write("#"); } writeln(); } 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() { string w; readV(w); auto c = new int[](26); foreach (wi; w) ++c[wi-'a']; writeln(c.all!(ci => ci%2 == 0) ? "Yes" : "No"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto nk = readln.split.to!(long[]); auto N = nk[0]; auto K = nk[1]; auto S = readln.chomp.to!(char[]); long[] NS; long n; char last; foreach (c; S) { if (c == last) { ++n; } else { if (n) NS ~= n; n = 1; last = c; } } NS ~= n; while (NS.length > 1 && K) { --K; if (NS.length == 2) { NS[0] += NS[1]; NS = NS[0..1]; } else if (NS.length == 3) { NS[0] += NS[1] + NS[2]; NS = NS[0..1]; } else { NS[1] += NS[0]; NS[$-2] += NS[$-1]; NS = NS[1..$-1]; } } long r; foreach (x; NS) r += x-1; writeln(r); }
D
import std.stdio,std.string,std.conv; int main() { int sum = 0,count = 0; double num = 0; string s; while((s = readln.chomp).length != 0) { count++; string[] _s = s.split(","); sum += _s[0].to!int * _s[1].to!int; num += _s[1].to!int; } sum.writeln; writeln(cast(int)((num/count) + 0.5)); return 0; }
D
import std.stdio, std.conv, std.array, std.algorithm, std.string, std.math; void main() { string s; while ((s = readln()).length != 0) { int[] nx; if (to!uint(s.chomp) == 0) {break;} for (uint i = 0; i < to!uint(s.chomp); ++i) { nx ~= to!int(readln().chomp); } writeln(solve(nx)); } } int solve(int[] tx) { if (negative_all(tx)) { int max = -100000; foreach(n ; tx) { if (n > max) { max = n; } } return max; } int[] nx = reduce_by_sign(tx); if (nx.length == 1) { return nx[0]; } if (nx[0] < 0) { nx = nx[1..(nx.length)]; } if (nx[nx.length-1] < 0) { nx = nx[0..(nx.length-1)]; } /*for (uint i = 0; i < nx.length; ++i) { if (nx[i] < 0) { if (nx[i-1] >= abs(nx[i]) && nx[i+1] >= abs(nx[i])) { int t = nx[i-1] + nx[i] + nx[i+1]; if (i+2 < nx.length) { nx = nx[0..i-1] ~ [t] ~ nx[i+2..nx.length]; } else { nx = nx[0..i-1] ~ [t]; } } } } int max = 0; foreach(n ; nx) { if (max < n) { max = n; } }*/ int max = nx[0]; for (uint i = 0; i < nx.length; ++i) { int max_ = nx[i]; int acc = max_; for (uint j = i+1; j < nx.length; ++j) { acc = acc + nx[j]; if (acc > max_) { max_ = acc; } } if (max_ > max) { max = max_; } } return max; } bool negative_all(int[] nx) { foreach(n ; nx) { if (n > 0) { return false; } } return true; } int[] reduce_by_sign(int[] nx) { bool sign = (nx[0] >= 0); uint index = 0; int[] ret = [nx[0]]; for (uint i = 1; i < nx.length; ++i) { if (sign != (0 <= nx[i])) { sign = !sign; ret ~= 0; ++index; } ret[index] += nx[i]; } return ret; }
D
import std.functional, std.algorithm, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.math; void main() { int N = readln.chomp.to!int; int[] a = readln.chomp.split.to!(int[]); int t = round((a.sum / a.length.to!double)).to!int; int ans; foreach (e; a) { ans += (e - t)^^2; } writeln(ans); }
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; alias sread = () => readln.chomp(); ulong MOD = 1_000_000_007; ulong INF = 1_000_000_000_000; alias Goods = Tuple!(long, "w", long, "v"); 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; } } auto dp = new long[][](110, 100_100); void main() { long N, W; scan(N, W); auto goods = new Goods[](N); foreach (ref e; dp) e[] = INF; foreach (i; iota(N)) { long w, v; scan(w, v); goods[i] = Goods(w, v); } foreach (i; iota(N)) { foreach (j; iota(100_100)) { dp[i + 1][j] = min(dp[i][j], dp[i + 1][j]); if (j == 0) if (goods[i].w <= W) dp[i + 1][j + goods[i].v] = goods[i].w; if (dp[i][j] != INF) if (dp[i][j] + goods[i].w <= W) dp[i + 1][j + goods[i].v] = dp[i][j] + goods[i].w; } // dp[i + 1].take(20).writeln(); } dp[N].maxidx.writeln(); } auto maxidx(T)(T a) { long ret; foreach (i, e; a) if (e != INF) ret = i; return ret; }
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; 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); auto t =[a + b, a + c, b + c]; t.reduce!(min).writeln(); }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); } void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1; if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; } void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); } string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); } 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; } T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void gen(){} void jury(){} void solve(){ long n = scan!long; long ans; foreach(long d; 1 .. n + 1){ long k = n / d; // d + 2d + 3d + ... + kd ans += d * k * (k + 1) / 2; } ans.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 n, m; { long[] tmp = readln.chomp.split.map!(to!long).array; n = tmp[0], m = tmp[1]; } long ans; /* long x = n * 2 + m; long y = x / 4; if(y > n) ans = y; else ans = m / 2; */ if(n < m / 2) ans = n + (m - n * 2) / 4; else ans = m / 2; ans.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; 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(); auto A = lreads(N); auto m1 = new long[](N + 1); m1[] = long.min; auto m2 = new long[](N + 1); m2[] = long.min; foreach (i; 0 .. N) { m1[i + 1] = max(m1[i], A[i]); } foreach_reverse (i; 0 .. N) { m2[i] = max(m2[i + 1], A[i]); } foreach (i; 0 .. N) { max(m1[i], m2[i + 1]).writeln(); } }
D
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons; 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); } void main() { auto N = readln.split.to!(ulong[]); string solve() { if (N[0] == N[1] && N[0] == N[2]) return "No"; if (N[0] != N[1] && N[1] != N[2] && N[0] != N[2]) return "No"; return "Yes"; } solve().writeln; }
D
void main(){ int n = _scan(); int[] a = _scanln(); int ans; for(int i=1; i<=a.length; i+=2){ if(a[i-1]&1)ans++; } ans.writeln(); } 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
void main() { import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto s = readln.chomp.to!(char[]); int mx = 0; foreach (i; 1 .. n) { bool[char] map; foreach (j; 0 .. i) { map[s[j]] = true; } int c = 0; foreach (j; i .. n) { if (s[j] in map) { c++; map.remove(s[j]); } } mx = max(mx, c); } writeln(mx); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
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; import std.ascii; void main() { auto n = readln.chomp.to!int; int cnt; foreach (i; 1..n/2+1) { auto j = n - i; if (i < j) { cnt++; } } cnt.writeln; }
D
import std; auto input() { return readln().chomp(); } void main() { // long[] ary = to!(long[])(split(readln())); auto ary = readln().split().to!(long[])(); // writeln(join(to!(string[])(ary), " ")); long a = ary[0]; long b = ary[1]; long c = ary[2]; long k = ary[3]; if (a >= k) { writeln(k); } else if (a + b >= k) { writeln(a); } else { writeln(a - (k - a - b)); } }
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; /// n 番目のフィボナッチ数を求める /// F(0) = 0, F(1] = 1, F(2) = 1, F(3) = 2, F(4) = 3 int fib(int n) { if (n == 0) return 0; auto mat = new Mat4x4([[1, 1], [1, 0]]); auto m = mat.pow(n, int.max); return m.get(0, 1); } void main() { int n = readint; // 問題文の定義だとF(0) = 1, F(1) = 1 なので値を 1 だけずらす writeln(fib(n + 1)); } class Mat4x4 { private int[][] _mat; this(const int[][] init) { assert(init.length == 2); assert(init[0].length == init[1].length && init[0].length == 2); _mat = new int[][](2, 2); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { _mat[i][j] = init[i][j]; } } } int get(int r, int c) const { return _mat[r][c]; } Mat4x4 mul(const Mat4x4 m, int mod) const { auto t = new int[][](2, 2); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int x = 0; for (int k = 0; k < 2; k++) { x = (x + (_mat[i][k] * m.get(k, j)) % mod) % mod; } t[i][j] = x; } } return new Mat4x4(t); } Mat4x4 copy() const { return new Mat4x4(_mat); } Mat4x4 pow(int k, int mod) const { assert(k > 0); if (k == 1) return this.copy(); auto m = this.pow(k / 2, mod); if (k % 2 == 0) return m.mul(m, mod); else return this.mul(m.mul(m, mod), mod); } void dump() const { writeln(_mat[0][0], " ", _mat[0][1]); writeln(_mat[1][0], " ", _mat[1][1]); } }
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; } T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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 = 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 main() { auto n = RD!int; auto x = RDA; long cnt; foreach (i; 0..n) { if (x[i] % 2 == 0) ++cnt; } writeln(n - max(cnt, n - cnt)); stdout.flush(); debug readln(); }
D
// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.datetime, std.bigint; int s, v1, v2, t1, t2; void main() { scan(s, v1, v2, t1, t2); int a, b; a = 2*t1 + s*v1; b = 2*t2 + s*v2; if (a < b) { writeln("First"); } else if (a > b) { writeln("Second"); } else { writeln("Friendship"); } } 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 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; void main() { auto nk = readints; int n = nk[0], k = nk[1]; writeln(n - k + 1); }
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.string; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; auto dp = new long[](N); fill(dp, 1L << 59); dp[0] = 0; dp[1] = abs(A[1] - A[0]); foreach (i; 2..N) { dp[i] = min(dp[i-2] + abs(A[i] - A[i-2]), dp[i-1] + abs(A[i] - A[i-1])); } dp[N-1].writeln; }
D
import std.array; import std.conv; import std.string; import std.algorithm; import std.stdio; void main() { int n = readln.chomp.to!int; auto a = readln.chomp.split(' ').map!(to!int).array; auto m = reduce!max(a); int count; bool top = false; while (m > 0) { foreach (ref e;a) { if (top) { if (e == m) { --e; } else { top = false; } } else { if (e == m) { top = true; ++count; --e; } } } top = false; --m; } count.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 inp = readln.chomp.split.to!(long[]); long num; if (inp[0] % inp[2]) { inp[0] += inp[2] - (inp[0] % inp[2]); } if (inp[1] % inp[2]) { inp[1] -= inp[1] % inp[2]; } if (inp[0] <= inp[1]) { num = (inp[1] - inp[0]) / inp[2] + 1; } num.writeln; }
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; long[] ts, xs, ys; foreach (_; 0..N) { auto txy = readln.split.to!(long[]); ts ~= txy[0]; xs ~= txy[1]; ys ~= txy[2]; } long t, x, y; foreach (i; 0..N) { auto d = ts[i] - t; auto dd = abs(xs[i]-x) + abs(ys[i]-y); if (dd > d || (d-dd)%2 != 0) { writeln("No"); return; } t = ts[i]; x = xs[i]; y = ys[i]; } writeln("Yes"); }
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 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); } enum MOD = (10 ^^ 9) + 7; void main() { long N = lread(); auto A = aryread(); long sum; foreach(a;A) { while (~a & 1) { sum++; a >>= 1; } } writeln(sum); }
D
import std.stdio, std.string, std.conv, std.algorithm; void main(string[] args) { auto x = readln().chomp.split.map!(to!int); auto a = x[0]-x[1]; if(a<0){0.writeln;}else{a.writeln;} }
D
// import chie template :) {{{ import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv, std.range, std.container, std.bigint, std.ascii, std.typecons; // }}} // nep.scanner {{{ class Scanner { import std.stdio; import std.conv : to; import std.array : split; import std.string : chomp; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.chomp.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() {} void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } } // }}} void main() { auto cin = new Scanner; int a, b, c; cin.scan(a, b, c); writeln(a + b >= c ? "Yes" : "No"); }
D
void main() { import std.stdio, std.string, std.conv, std.algorithm; import std.array; int n; rd(n); auto a = readln.split .to!(int[]) .map!((e) => (e + 1)) .array; int M = 100000 + 5; auto map = new int[](M); foreach (e; a) { for (int d = -1; d <= 1; d++) { map[e + d]++; } } writeln(reduce!(max)(map)); } void rd(T...)(ref T x) { import std.stdio : readln; import std.string : split; import std.conv : to; auto l = readln.split; assert(l.length == x.length); foreach (i, ref e; x) e = l[i].to!(typeof(e)); }
D
import std.stdio, std.conv, std.array, std.string; import std.algorithm; import std.container; void main() { auto N = readln.chomp.to!int; auto Hn = readln.split.to!(int[]); int prev = Hn[0]; int count = 0; int max_count = 0; foreach(h; Hn[1..Hn.length]) { if (h > prev) { count = 0; prev = h; continue; } if (max_count < ++count) max_count = count; prev = h; } writeln(max_count); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n, a, b; rd(n, a, b); writeln(min(a*n, b)); } 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
//dlang template---{{{ import std.stdio; import std.conv; import std.string; import std.array; import std.algorithm; // MIT-License https://github.com/kurokoji/nephele class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.array : split; import std.string; import std.traits : isSomeString; private File file; private char[][] str; private size_t idx; this(File file = stdin) { this.file = file; this.idx = 0; } this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) { this.file = file; this.idx = 0; fromString(s); } private char[] next() { if (idx < str.length) { return str[idx++]; } char[] s; while (s.length == 0) { s = file.readln.strip.to!(char[]); } str = s.split; idx = 0; return str[idx++]; } T next(T)() { return next.to!(T); } T[] nextArray(T)(size_t len) { T[] ret = new T[len]; foreach (ref c; ret) { c = next!(T); } return ret; } void scan()() { } void scan(T, S...)(ref T x, ref S args) { x = next!(T); scan(args); } void fromString(StrType)(StrType s) if (isSomeString!(StrType)) { str ~= s.to!(char[]).strip.split; } } //Digit count---{{{ int DigitNum(int num) { int digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } //}}} //}}} void main() { Scanner sc = new Scanner; int N, A, B; sc.scan(N, A, B); int res = 0; foreach (i; 0 .. N + 1) { if (A <= num(i) && num(i) <= B) res += i; } writeln(res); } int num(int n) { if (n / 10 == 0) return n % 10; return num(n / 10) + (n % 10); }
D