classSolution { public: string maximumOddBinaryNumber(string s){ int n = s.size(), cnt = 0; for (char c : s) { if (c == '1') cnt++; } string ans; for (int i = 0; i < n - 1; i++) { if (i < cnt - 1) ans.push_back('1'); else ans.push_back('0'); } ans.push_back('1'); return ans; } };
static { np[0] = np[1] = true; for (inti=2; i <= N / i; i++) { if (!np[i]) { for (intj= i; j <= N / i; j++) { np[j * i] = true; } } } }
longans=0L;
publiclongcountPaths(int n, int[][] edges) { List<Integer>[] g = newList[n + 1]; Arrays.setAll(g, k -> newArrayList<>()); for (int[] e : edges) { g[e[0]].add(e[1]); g[e[1]].add(e[0]); } dfs(1, 0, g); return ans; }
privateint[] dfs(int x, int fa, List<Integer>[] g) { intzero=0, one = 0; if (np[x]) zero = 1; else one = 1;
for (int y : g[x]) { if (y == fa) continue; int[] t = dfs(y, x, g); ans += (long) zero * t[1] + (long) one * t[0]; if (np[x]) { zero += t[0]; one += t[1]; } else { one += t[0]; } } returnnewint[]{zero, one}; } }
publicstaticvoidsolve() { intn= io.nextInt(), x = io.nextInt(); intmin=101, max = -1, sum = 0; for (inti=0; i < n - 1; i++) { intt= io.nextInt(); sum += t; min = Math.min(min, t); max = Math.max(max, t); } intt= x - (sum - min - max); if (t > max) { io.println(-1); } else { io.println(t <= min ? 0 : t); } }
publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt(), p = io.nextInt(); int[] a = newint[n]; for (inti=0; i < n; i++) { a[i] = io.nextInt(); } int[] b = newint[m]; for (inti=0; i < m; i++) { b[i] = io.nextInt(); } Arrays.sort(b); long[] sum = newlong[m + 1]; for (inti=0; i < m; i++) { sum[i + 1] = sum[i] + b[i]; } longans=0L; for (inti=0; i < n; i++) { intx= p - a[i]; intlo=0, hi = m - 1; while (lo <= hi) { int mid= lo + (hi - lo) / 2; if (b[mid] <= x) lo = mid + 1; else hi = mid - 1; } ans += ((long) lo * a[i] + sum[lo]) + (long) (m - lo) * p; } io.println(ans); }