void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); int[] B = cin.ArrayInt(N); long ans = 0; for (int t = 0; t < N; t++) { long tmp = 0; for (int i = 0; i <= t; i++) { tmp += A[i]; } for (int j = t; j < N; j++) { tmp += B[j]; } ans = Math.Max(tmp, ans); } Console.WriteLine(ans); }
void calc() { cin = new Scanner(); N = cin.nextInt(); A = cin.ArrayInt(N); B = cin.ArrayInt(N); C = cin.ArrayInt(N); dicB = new long[N]; dicB2 = new int[N]; dicC = new long[N]; Array.Sort(A); Array.Sort(B); Array.Sort(C); Array.Reverse(A); Array.Reverse(B); Array.Reverse(C); for (int i = 0; i < N; i++) { dicB[i] = dicC[i] = -1; } long ans = 0; for (int i = 0; i < N; i++) { ans += dfsB(i); } Console.WriteLine(ans); }
static void Main() { int T = cin.Int(); int N = cin.Int(); var A = cin.ArrayInt(N); int M = cin.Int(); var B = cin.ArrayInt(M); int takoyaki = 0; var flag = new bool[N]; bool ans = true; for (int i = 0; i < M; i++) { while (!(A[takoyaki] <= B[i] && B[i] <= A[takoyaki] + T) || flag[takoyaki] == true) { takoyaki++; if (takoyaki >= N) { Console.WriteLine("no"); return; } } if (A[takoyaki] + T >= B[i] && flag[takoyaki] == false) { flag[takoyaki] = true; } else { ans = false; } } Console.WriteLine(ans ? "yes" : "no"); Console.ReadLine(); }
static void Main() { int N = cin.Int(); int C = cin.Int(); var D = new int[35, 35]; for (int i = 0; i < C; i++) { var tmp = cin.ArrayInt(C); for (int j = 0; j < C; j++) { D[i, j] = tmp[j]; } } var c = new int[5, 35]; for (int i = 0; i < N; i++) { var tmp = cin.ArrayInt(N); for (int j = 0; j < N; j++) { c[(i + j) % 3, tmp[j] - 1]++; //???3????????????? } } int ans = int.MaxValue; for (int i = 0; i < C; i++) { for (int j = 0; j < C; j++) { for (int k = 0; k < C; k++) { if (i != j && j != k && k != i) //???????????? { int tmp = 0; for (int l = 0; l < C; l++) { tmp += D[l, i] * c[0, l]; } for (int l = 0; l < C; l++) { tmp += D[l, j] * c[1, l]; } for (int l = 0; l < C; l++) { tmp += D[l, k] * c[2, l]; } ans = Math.Min(ans, tmp); } } } } Console.WriteLine(ans); Console.ReadLine(); }
static void Main(string[] args) { var D = cin.ArrayInt(7); var L = cin.ArrayInt(7); int ans = 0; for (int i = 0; i < 7; i++) { ans += Math.Max(D[i], L[i]); } Console.WriteLine(ans); Console.ReadLine(); }
void calc() { cin = new Scanner(); int[] NK = cin.ArrayInt(2, 0); int[] l = cin.ArrayInt(NK[0], 0); Array.Sort(l); Array.Reverse(l); int ans = 0; for (int i = 0; i < NK[1]; i++) { ans += l[i]; } Console.WriteLine(ans); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] T = cin.ArrayInt(N); int[] V = cin.ArrayInt(N); int sumT = T.Sum() * 2; double[] v = new double[sumT + 1]; for (int i = 0; i < v.Length; i++) { v[i] = 1e30; } v[0] = 0; v[sumT] = 0; int cnt = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < T[i] * 2; j++) { v[cnt] = Math.Min(v[cnt], V[i]); cnt++; v[cnt] = Math.Min(v[cnt], V[i]); } } for (int i = 1; i < v.Length - 1; i++) { v[i] = Math.Min(v[i], v[i - 1] + 0.5); v[i] = Math.Min(v[i], v[i + 1] + 0.5); } for (int i = v.Length - 2; i >= 1; i--) { v[i] = Math.Min(v[i], v[i - 1] + 0.5); v[i] = Math.Min(v[i], v[i + 1] + 0.5); } double ans = 0; for (int i = 0; i < sumT; i++) { ans += (v[i] + v[i + 1]) / 4; } Console.WriteLine("{0:0.000000000000000000}", ans); }
static void Main() { //string X = Console.ReadLine(); //string tmp = ""; //long cnt = 0; //?????? //for (int i = 0; i < X.Length; i++) { // if(X[i] == 'S') { // if (tmp == "") tmp += "S"; // else if (tmp[tmp.Length - 1] == 'S') tmp += "S"; // else { // cnt += 2 * Math.Min(tmp.ToCharArray().Count(x => x == 'S'), tmp.ToCharArray().Count(x => x == 'T')); // tmp = "S"; // } // } // else { // if (tmp != "") tmp += "T"; // } //} //cnt += Math.Min(tmp.ToCharArray().Count(x => x == 'S'), tmp.ToCharArray().Count(x => x == 'T')); //Console.WriteLine(X.Length - cnt); int N = cin.Int(); var L = cin.ArrayInt(2 * N); Array.Sort(L); int ans = 0; for (int i = L.Length - 2; i >= 0; i -= 2) { ans += L[i]; } Console.WriteLine(ans); Console.ReadLine(); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int L = cin.nextInt(); int[] A = cin.ArrayInt(N); for (int i = 0; i < N - 1; i++) { if (A[i] + A[i + 1] >= L) { Console.WriteLine("Possible"); for (int j = 1; j <= i; j++) { Console.WriteLine(j); } for (int j = N - 1; j > i; j--) { Console.WriteLine(j); } return; } } Console.WriteLine("Impossible"); }
internal static void Main(string[] args) { var N = sc.nextInt(); var M = sc.nextInt(); var a = sc.ArrayInt(N); var sum = new int[N + 1]; for (int i = 0; i < N; i++) { sum[i + 1] = (sum[i] + a[i]) % M; } long ans = 0; var dic = new Dictionary <int, int>(); for (int i = 0; i < sum.Length; i++) { if (!dic.ContainsKey(sum[i])) { dic.Add(sum[i], 0); } ans += dic[sum[i]]; dic[sum[i]]++; } WriteLine(ans); }
public void Solve() { var N = sc.nextInt(); var x = sc.nextInt(); var a = sc.ArrayInt(N); a = a.OrderBy(i => i).ToArray(); var ans = 0; for (int i = 0; i < N; i++) { if (x >= a[i]) { x -= a[i]; if (i == N - 1) { if (x == 0) { ans++; } } else { ans++; } } } WriteLine(ans); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); int ans = 1; int sign = 0; for (int i = 1; i < N; i++) { if (sign == 0) { sign = Math.Sign(A[i] - A[i - 1]); } else { if (sign != Math.Sign(A[i] - A[i - 1]) && A[i] != A[i - 1]) { sign = 0; ans++; } } } Console.WriteLine(ans); }
public void Solve() { var N = sc.nextInt(); var a = sc.ArrayInt(N); Array.Resize(ref a, a.Length + 1); a[N] = -1; var lst = new List <int>(); var bef = a[0]; var cnt = 1; for (int i = 1; i <= N; i++) { if (bef == a[i]) { cnt++; } else { lst.Add(cnt); cnt = 1; } bef = a[i]; } var ans = 0; foreach (var i in lst) { ans += i / 2; } WriteLine(ans); }
public void Solve() { var A = sc.ArrayInt(5); var bit = new int[][] { new int[] { 1, 1, 1, 0, 0 }, new int[] { 1, 1, 0, 1, 0 }, new int[] { 1, 1, 0, 0, 1 }, new int[] { 1, 0, 1, 1, 0 }, new int[] { 1, 0, 1, 0, 1 }, new int[] { 1, 0, 0, 1, 1 }, new int[] { 0, 1, 1, 1, 0 }, new int[] { 0, 1, 1, 0, 1 }, new int[] { 0, 1, 0, 1, 1 }, new int[] { 0, 0, 1, 1, 1 } }; var ans = new List <int>(); for (int i = 0; i < bit.Length; i++) { var sum = 0; for (int j = 0; j < bit[i].Length; j++) { if (bit[i][j] == 1) { sum += A[j]; } } ans.Add(sum); } WriteLine(ans.OrderByDescending(i => i).ToArray()[2]); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); int ans = 0; int tmp = 0; for (int i = 0; i < N; i++) { if (A[i] == i + 1) { tmp++; } else { ans += (tmp + 1) / 2; tmp = 0; } } ans += (tmp + 1) / 2; Console.WriteLine(ans); }
void calc() { cin = new Scanner(); int H = cin.nextInt(); int W = cin.nextInt(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); int[,] ans = new int[H, W]; int cnt = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < A[i]; j++) { int y = cnt / W; int x = cnt % W; if (y % 2 == 1) { x = (W - 1) - x; } ans[y, x] = i + 1; cnt++; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { Console.Write($"{ans[i,j]} "); } Console.WriteLine(); } }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); if (A[0] != 0) { Console.WriteLine(-1); return; } for (int i = 1; i < N; i++) { if (A[i] > A[i - 1] + 1) { Console.WriteLine(-1); return; } } long ans = 0; for (int i = 0; i < N; i++) { if (i != N - 1 && A[i + 1] == A[i] + 1) { continue; } ans += A[i]; } Console.WriteLine(ans); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] b = cin.ArrayInt(N, -1); List <int> L = new List <int>(b); List <int> ans = new List <int>(); while (L.Count > 0) { bool flag = false; for (int i = L.Count - 1; i >= 0; i--) { if (L[i] == i) { ans.Add(i + 1); L.RemoveAt(i); flag = true; break; } } if (!flag) { Console.WriteLine(-1); return; } } ans.Reverse(); Console.WriteLine(String.Join("\n", ans)); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] C = cin.ArrayInt(N); int SMAX = 200001; long[] stock = new long[SMAX]; long[] dp = new long[N + 1]; dp[0] = 1; long mod = 1000000007; for (int i = 0; i < N; i++) { dp[i + 1] = dp[i]; if (i == 0 || C[i] != C[i - 1]) { dp[i + 1] += stock[C[i]]; dp[i + 1] %= mod; stock[C[i]] += dp[i]; stock[C[i]] %= mod; } } Console.WriteLine(dp[N]); }
public void Solve() { var N = sc.nextInt(); var a = sc.ArrayInt(N); WriteLine(a.Max() - a.Min()); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); Array.Sort(A); Array.Reverse(A); long ans = 1; long mod = 1000000007; for (int i = 0; i < N; i++) { if (A[i] != (N - 1) - (i / 2 * 2)) { Console.WriteLine(0); return; } if (i % 2 == 1) { ans *= 2; ans %= mod; } } Console.WriteLine(ans); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); setFact(200000); long ans = 0; long now = 0; long mul = fact[N]; for (int i = 0; i < N; i++) { now += A[i]; now += A[N - 1 - i]; now %= mod; if (i == N - 1) { now *= revFact[2]; now %= mod; } //ans += now * mul / fact[N - i]; ans += now * mul % mod * powmod(N - i, mod - 2); ans %= mod; } Console.WriteLine(ans); }
static void Main() { Scanner cin = new Scanner(); int N = cin.Int(); var F = new int[N, 10]; var P = new int[N, 11]; for (int i = 0; i < N; i++) { var tmp = cin.ArrayInt(10); for (int j = 0; j < 10; j++) { F[i, j] = tmp[j]; } } for (int i = 0; i < N; i++) { var tmp = cin.ArrayInt(11); for (int j = 0; j < 11; j++) { P[i, j] = tmp[j]; } } int ans = int.MinValue; for (int i = 1; i < (1 << 10); i++) { int tmp = 0; for (int j = 0; j < N; j++) { int cnt = 0; for (int k = 0; k < 10; k++) { if ((i >> k & 1) == 1 && F[j, k] == 1) { cnt++; } } tmp += P[j, cnt]; } ans = Math.Max(ans, tmp); } Console.WriteLine(ans); Console.ReadLine(); }
public void Solve() { int N = sc.nextInt(); int K = sc.nextInt(); var l = sc.ArrayInt(N); WriteLine(l.OrderByDescending(i => i).Take(K).Sum()); }
bool calc2() { cin = new Scanner(); int N = cin.nextInt(); int[] a = cin.ArrayInt(N); Array.Sort(a); int min = a[0]; int max = a[N - 1]; if (max - min >= 2) { return(false); } if (min == max) { if (max == N - 1) { return(true); //???? } else { min--; //???????2?? } } int mincnt = 0; int maxcnt = 0; for (int i = 0; i < N; i++) { if (a[i] == min) { mincnt++; } else { maxcnt++; } } if (maxcnt == 1) { return(false); //2????????1???????? } int minPattern = mincnt + 1; //mincnt, maxcnt?????????????????????? int maxPattern = mincnt + maxcnt / 2; //??????? if (max >= minPattern && max <= maxPattern) { return(true); } else { return(false); } }
static void Main(string[] args) { int L = cin.Int(); int R = cin.Int(); var l = cin.ArrayInt(L); var r = cin.ArrayInt(R); var l_dic = new Dictionary <int, int>(); var r_dic = new Dictionary <int, int>(); for (int i = 0; i < L; i++) { if (l_dic.ContainsKey(l[i])) { l_dic[l[i]]++; } else { l_dic[l[i]] = 1; } } for (int i = 0; i < R; i++) { if (r_dic.ContainsKey(r[i])) { r_dic[r[i]]++; } else { r_dic[r[i]] = 1; } } int ans = 0; foreach (var key in l_dic.Keys) { if (r_dic.ContainsKey(key)) { ans += Math.Min(l_dic[key], r_dic[key]); } } Console.WriteLine(ans); Console.ReadLine(); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] t = cin.ArrayInt(N); int[] v = cin.ArrayInt(N); int sumT = t.Sum() * 2; double[] V = new double[sumT + 1]; int now = 0; for (int i = 1; i < sumT; i++) { V[i] = 99999999; } for (int i = 0; i < N; i++) { int next = now + t[i] * 2; for (int j = now; j <= next; j++) { V[j] = Math.Min(V[j], v[i]); } now = next; } for (int i = 1; i <= sumT; i++) { V[i] = Math.Min(V[i - 1] + 0.5, V[i]); } for (int i = sumT - 1; i >= 0; i--) { V[i] = Math.Min(V[i + 1] + 0.5, V[i]); } double ans = 0; for (int i = 0; i < sumT; i++) { ans += (V[i] + V[i + 1]) / 2; } Console.WriteLine(ans / 2); }
void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] A = cin.ArrayInt(N); int[] B = cin.ArrayInt(N); long result = (1L << C) - 1; for (int i = 0; i < N; i++) { if (!check(result, A[i], B[i])) { Console.WriteLine(-1); return; } } for (int i = C - 1; i >= 0; i--) { long next = result - (1L << i); bool flag = true; for (int j = 0; j < N; j++) { if (!check(next, A[j], B[j])) { flag = false; break; } } if (flag) { result = next; } } Console.WriteLine(result * 2); }
void calc() { cin = new Scanner(); int[] NMXY = cin.ArrayInt(4, 0); int[] x = cin.ArrayInt(NMXY[0], 0); int[] y = cin.ArrayInt(NMXY[1], 0); Array.Sort(x); Array.Sort(y); if (x[NMXY[0] - 1] >= y[0]) { Console.WriteLine("War"); } else if (y[0] <= NMXY[2] || x[NMXY[0] - 1] >= NMXY[3]) { Console.WriteLine("War"); } else { Console.WriteLine("No War"); } }
void calc() { cin = new Scanner(); int[] AB = cin.ArrayInt(2, 0); if (AB[0] % 3 == 0 || AB[1] % 3 == 0 || (AB[1] + AB[0]) % 3 == 0) { Console.WriteLine("Possible"); } else { Console.WriteLine("Impossible"); } }