zccneil/codelingua_dataset huggingface dataset:
assistant: import queue
n, m = map(int, input().split())
vis, ci, cb, cc = [0] * (n + 1), 0, 0, 0
g = [[] for i in range(n + 1)]


def dfs(x):
    stk, flag = queue.LifoQueue(), True
    stk.put((x, 1))
    while not stk.empty():
        u, col = stk.get()
        if vis[u]:
            flag &= (vis[u] == col)
            continue
        vis[u] = col
        for i in g[u]:
            stk.put((i, 3 - col))
    return flag


for i in range(m):
    u, v = map(int, input().split())
    g[u] += [v]
    g[v] += [u]
for i in range(1, n + 1):
    if vis[i] == 0:
        if len(g[i]) == 0:
            ci += 1
        else:
            if dfs(i):
                cb += 1
            else:
                cc += 1
print(ci * ci + 2 * ci * (n - ci) + cc * cc + 2 * cb * cc + 2 * cb * cb)


user: import java.util.*;

class atcoder_AGC011_C implements Runnable {
    ArrayList<Integer>[] graph;
    boolean[] visited;
    int[] color;
    long one;
    long bipartite;
    long count;
    boolean mujun;

    int dfs(int a, int c) {
        if (visited[a]) {
            if (color[a] >= 0 && color[a] != c) mujun = true;
            return 0;
        }
        visited[a] = true;
        color[a] = c;
        int total = 1;
        for (int b : graph[a]) {
            total += dfs(b, 1 - c);
        }
        return total;
    }

    public void run() {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        this.graph = new ArrayList[n];
        for (int i = 0; i < n; ++i) this.graph[i] = new ArrayList<Integer>();
        for (int i = 0; i < m; ++i) {
            int u = scan.nextInt() - 1;
            int v = scan.nextInt() - 1;
            this.graph[u].add(v);
            this.graph[v].add(u);
        }
        visited = new boolean[n];
        color = new int[n];
        Arrays.fill(color, -1);
        one = 0;
        bipartite = 0;
        count = 0;
        for (int i = 0; i < n; ++i) {
            if (visited[i]) continue;
            count++;
            mujun = false;
            int kind = dfs(i, 0);
            if (kind == 1) one++;
            else if (!mujun) bipartite++;
        }
        long total = one * (2 * n - one);
        total += (count - one) * (count - one);
        total += bipartite * bipartite;
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Thread(null, new atcoder_AGC011_C(), "", 128 * 1024 * 1024).start();
    }
}
assistant: from statistics import median
from fractions import gcd
from itertools import combinations
from collections import deque
from collections import defaultdict
import bisect
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7


def readInts():
    return list(map(int, input().split()))


def main():
    n = int(input())
    A = readInts()
    A = sorted(A)
    ans = 0
    for i in range(len(A) - 2, len(A) - 2 * n - 1, - 2):
        ans += A[i]
    print(ans)


if __name__ == '__main__':
    main()


user: import static java.lang.Math.*;

import java.util.*;

public class atcoder_AGC012_A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] array = new int[3 * n];
        for (int i = 0; i < array.length; i++) array[i] = sc.nextInt();
        Arrays.sort(array);
        int head = array.length - 2;
        long res = 0;
        for (int i = 0; i < n; i++) {
            res += array[head];
            head -= 2;
        }
        System.out.println(res);
    }
}
assistant: import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import groupby, accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(* (MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
N = INT()
A = LIST()
A = [k for k, g in groupby(A)]
N = len(A)
i = 1
ans = 0
while i < N - 1:
    if A[i - 1] < A[i] > A[i + 1] or A[i - 1] > A[i] < A[i + 1]:
        ans += 1
        i += 1
    i += 1
print(ans + 1)


user: import java.util.*;

public class atcoder_AGC013_A {
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int N = stdIn.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = stdIn.nextInt();
        }
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (i == N - 1) count++;
            else if (A[i] == A[i + 1])
                ;
            else if (A[i] < A[i + 1]) {
                while (A[i] <= A[i + 1]) {
                    i++;
                    if (i == N - 1) break;
                }
                count++;
            } else {
                while (A[i] >= A[i + 1]) {
                    i++;
                    if (i == N - 1) break;
                }
                count++;
            }
        }
        System.out.println(count);
    }
}
assistant: import sys
from collections import deque
input = sys.stdin.buffer.readline
N = int(input())
adj = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    adj[a].append(b)
    adj[b].append(a)
que = deque()
que.append(1)
seen = [0] * (N + 1)
seen[1] = 1
par = [0] * (N + 1)
child_num = [0] * (N + 1)
while que:
    v = que.popleft()
    for u in adj[v]:
        if seen[u] == 0:
            seen[u] = 1
            par[u] = v
            child_num[v] += 1
            que.append(u)
seq = deque()
for i in range(1, N + 1):
    if child_num[i] == 0:
        seq.append(i)
while seq:
    c = seq.pop()
    seen[c] = 0
    if seen[par[c]] == 0:
        print('First')
        exit()
    seen[par[c]] = 0
    child_num[par[par[c]]] -= 1
    if child_num[par[par[c]]] == 0:
        seq.append(par[par[c]])
print('Second')


user: import java.io.*;
import java.util.*;

public class atcoder_AGC014_D {
    static ArrayList<Integer>[] g;

    static int maxMatching(int v, int p) {
        int a = 0;
        int o = 0;
        for (int w : g[v]) {
            if (w == p) continue;
            int r = maxMatching(w, v);
            a += r / 2;
            o += r % 2;
        }
        return 2 * a + Math.min(1, o) + 1;
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n = sc.nextInt();
        g = new ArrayList[n];
        Arrays.setAll(g, x -> new ArrayList<Integer>());
        for (int i = 0; i < n - 1; ++i) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            g[a].add(b);
            g[b].add(a);
        }
        int m = maxMatching(0, -1) / 2;
        out.println(2 * m == n ? "Second" : "First");
        out.close();
    }

    public static PrintWriter out;

    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
assistant: N, P = map(int, input().split())


def combi(N, K):
    a = 1
    for i in range(K):
        a *= N - i
    for j in range(K):
        a /= j + 1
    return a


ans = 0
lis = list(map(int, input().split()))
ls = []
for a in lis:
    ls.append(a % 2)
one = ls.count(1)
zero = ls.count(0)
pattern_a = 0
pattern_b = 0
for j in range(zero + 1):
    pattern_b += combi(zero, j)
time = 0
while time <= one:
    if time % 2 == P:
        pattern_a += combi(one, time)
    time += 1
print(int(pattern_a * pattern_b))


user: import java.math.*;
import java.util.*;

public class atcoder_AGC017_A {
    static BigInteger C(int n, int m) {
        BigInteger res = new BigInteger("1");
        for (int i = m - n + 1; i <= m; i++) res = res.multiply(BigInteger.valueOf(i));
        for (int i = 1; i <= n; i++) res = res.divide(BigInteger.valueOf(i));
        return res;
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n, p;
        int k0 = 0;
        int k1 = 0;
        n = sc.nextInt();
        p = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int x;
            x = sc.nextInt();
            if (x % 2 == 1) k1++;
            else k0++;
        }
        BigInteger ans = new BigInteger("1");
        for (int i = 0; i < k0; i++) ans = ans.multiply(BigInteger.valueOf(2));
        if (p == 0) {
            BigInteger add = new BigInteger("0");
            for (int i = 0; i <= k1; i += 2) add = add.add(C(i, k1));
            ans = ans.multiply(add);
            System.out.println(ans);
        } else {
            BigInteger add = new BigInteger("0");
            for (int i = 1; i <= k1; i += 2) add = add.add(C(i, k1));
            ans = ans.multiply(add);
            System.out.println(ans);
        }
    }
}
assistant: import sys
import re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5:
    from math import gcd
else:
    from fractions import gcd
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits
def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(- (- a // b))
def round(x): return int((x * 2 + 1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD - 2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, initial=1)
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
q, h, s, d = MAP()
n = INT()
best1L = min(q * 4, h * 2, s)
best2L = min(d, best1L * 2)
if n % 2 == 0:
    print(best2L * (n // 2))
else:
    print(best2L * (n // 2) + best1L)


user: import static java.lang.System.*;

import java.util.*;

public class atcoder_AGC019_A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long Q = sc.nextLong();
        long H = sc.nextLong();
        long S = sc.nextLong();
        long D = sc.nextLong();
        long N = sc.nextLong();
        long onePrice = Math.min(Math.min(Q * 4, H * 2), S);
        if ((double) onePrice <= (double) D / 2) {
            out.println(N * onePrice);
        } else {
            out.println((N / 2) * D + (N % 2) * onePrice);
        }
    }
}
assistant: from math import atan2, pi
N, * XY = map(int, open(0).read().split())
XY = list(zip(* [iter(XY)] * 2))
for i, (x, y) in enumerate(XY):
    D = sorted(atan2(X - x, Y - y) for j, (X, Y) in enumerate(XY) if j != i)
    D.append(D[0] + 2 * pi)
    ans = 0
    for a, b in zip(D, D[1:]):
        if b - a >= pi:
            ans = (b - a) - pi
    print(ans / (2 * pi))


user: import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class atcoder_AGC021_B {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[] x = new int[N];
        int[] y = new int[N];
        for (int i = 0; i < N; ++i) {
            x[i] = scan.nextInt();
            y[i] = scan.nextInt();
        }
        for (int i = 0; i < N; ++i) {
            List<Double> thetas = new ArrayList<>();
            for (int j = 0; j < N; ++j) {
                if (i == j) continue;
                thetas.add(Math.atan2(y[j] - y[i], x[j] - x[i]));
            }
            thetas.sort(Comparator.naturalOrder());
            thetas.add(thetas.get(0) + 2 * Math.PI);
            double ans = 0;
            for (int k = 0; k < N - 1; ++k)
                ans = Math.max(ans, thetas.get(k + 1) - thetas.get(k) - Math.PI);
            System.out.println(ans / (Math.PI * 2));
        }
    }
}
assistant: def main():
    N = int(input())
    P = [int(input()) for _ in range(N)]
    editorial(N, P)


def editorial(N, P):
    if N == 1:
        print(0)
        return
    a = [0] * (N + 1)
    for i, p in enumerate(P):
        a[p] = i
    tmp = 1
    max_len = 1
    for i in range(1, N):
        if a[i] < a[i + 1]:
            tmp += 1
            max_len = max(max_len, tmp)
        else:
            tmp = 1
    ans = N - max_len
    print(ans)


def WA(N, P):
    tmp = 0
    ans = 0
    for i, p in enumerate(P):
        if i == 0 or P[i - 1] + 1 == p:
            tmp += 1
        else:
            ans = max(ans, tmp)
            tmp = 1
    print(N - ans)


if __name__ == '__main__':
    main()


user: import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class atcoder_AGC024_B {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[] P = new int[N];
        for (int i = 0; i < N; ++i) P[i] = scan.nextInt() - 1;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < N; ++i) map.put(P[i], i);
        List<Integer> nglist = new ArrayList<>();
        for (int i = 0; i < N - 1; ++i) if (map.get(i) > map.get(i + 1)) nglist.add(i);
        if (nglist.isEmpty()) {
            System.out.println(0);
            return;
        }
        int ans = Math.min(N - nglist.get(0) - 1, nglist.get(nglist.size() - 1) + 1);
        for (int i = 0; i < nglist.size() - 1; ++i) {
            ans = Math.min(nglist.get(i) + 1 + N - nglist.get(i + 1) - 1, ans);
        }
        System.out.println(ans);
    }
}
assistant: n = input()
a = ["10", "100", "1000", "10000", "100000"]
new = sum(list(map(int, n)))
if n in a:
    print(10)
else:
    print(new)


user: import java.util.Scanner;

public class atcoder_AGC025_A {
    public static int sum(int a) {
        int ans = 0;
        while (a >= 10) {
            ans += a % 10;
            a /= 10;
        }
        ans += a;
        return ans;
    }

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int N = stdIn.nextInt();
        int n = 0;
        if (N % 2 == 0) {
            n = N / 2;
        } else {
            n = (N + 1) / 2;
        }
        int ans = sum(1) + sum(N - 1);
        for (int A = 1; A <= n; A++) {
            if (sum(A) + sum(N - A) < ans) {
                ans = sum(A) + sum(N - A);
            }
        }
        System.out.println(ans);
    }
}
assistant: def na(): return list(map(int, input().split()))


n, x = na()
a = sorted(na())
ans = 0
if a[0] > x:
    print(0)
    exit()
for i in a:
    if i > x:
        x = 0
        break
    x -= i
    ans += 1
if x != 0:
    ans -= 1
print(ans)


user: import java.util.*;

public class atcoder_AGC027_A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long x = sc.nextInt();
        List<Long> sweet = new ArrayList<Long>();
        for (int i = 0; i < N; i++) {
            long a = sc.nextInt();
            sweet.add(a);
        }
        Collections.sort(sweet);
        int num = 0;
        for (int i = 0; i < N; i++) {
            if (x - sweet.get(num) >= 0) {
                x = x - sweet.get(num);
                num++;
            } else {
                break;
            }
        }
        if ((num == N) && (x > 0)) {
            num -= 1;
        }
        System.out.println(num);
    }
}
assistant: from itertools import accumulate
N, X, * A = map(int, open(0).read().split())
S = list(accumulate(A))
ans = float("inf")
for k, s in enumerate(S, 1):
    E = k * X + 2 * sum(S[j] for j in range(N - 2 * k - 1, - 1, - k))
    ans = min(ans, E)
print(ans + N * X + 5 * S[- 1])


user: import java.util.*;

public class atcoder_AGC027_B {
    int INF = Integer.MAX_VALUE;

    public static void main(String[] args) {
        new atcoder_AGC027_B().solve();
    }

    void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long X = sc.nextLong();
        long[] x = new long[N];
        long[] xsum = new long[N + 1];
        for (int i = 0; i < N; i++) {
            x[i] = sc.nextInt();
            xsum[i + 1] = xsum[i] + x[i];
        }
        long ans = X * N + 5 * xsum[N];
        for (int i = 1; i < N; i++) {
            long cost = X * i + 5 * (xsum[N] - xsum[N - i]);
            for (int j = 5, k = N - i; k >= 0; j += 2, k -= i) {
                if (cost > ans) {
                    break;
                }
                cost += j * (xsum[k] - xsum[Math.max(k - i, 0)]);
            }
            ans = Math.min(ans, cost);
        }
        System.out.println(ans + N * X);
    }
}
assistant: N, M = map(int, input().split())
S = input()
T = input()
list_S = list(S)
list_T = list(T)
Number_i = [i for i in range(N)]
Number_iMN = []
for i in Number_i:
    Number_iMN.append(i * M / N)
Number_j = [j for j in range(M)]
Kaburi_j = list(set(Number_iMN) & set(Number_j))
Kaburi_i = []
for j in Kaburi_j:
    Kaburi_i.append(int(j * N / M))
counter = 0
Flag = 0
Kaburi_Size = len(Kaburi_i)
while counter <= Kaburi_Size - 1:
    if list_S[int(Kaburi_i[counter])] == list_T[int(Kaburi_j[counter])]:
        pass
    else:
        Flag = 1
        break
    counter += 1


def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


if Flag == 1:
    print(- 1)
else:
    print(int(N * M / gcd(N, M)))


user: import java.math.BigInteger;
import java.util.*;
import java.util.stream.*;

public class atcoder_AGC028_A {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] $) {
        int n = scanner.nextInt(),
                m = scanner.nextInt(),
                gcd = BigInteger.valueOf(n).gcd(BigInteger.valueOf(m)).intValue();
        String s = scanner.next(), t = scanner.next();
        System.out.println(
                IntStream.range(0, gcd)
                        .filter(k -> s.charAt(k * n / gcd) != t.charAt(k * m / gcd))
                        .mapToLong(k -> -1)
                        .findAny()
                        .orElse(
                                BigInteger.valueOf(n)
                                        .multiply(BigInteger.valueOf(m))
                                        .divide(BigInteger.valueOf(gcd))
                                        .longValue()));
    }
}
assistant: import numpy as np
import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
N = int(input())
M = 10 ** 6
A = np.array(input().split(), dtype=np.int64)
Nsq = 10 ** 3
fact = np.arange(M, dtype=np.int64).reshape(Nsq, Nsq)
fact[0, 0] = 1
for n in range(1, Nsq):
    fact[:, n] *= fact[:, n - 1]
    fact[:, n] %= MOD
for n in range(1, Nsq):
    fact[n] *= fact[n - 1, - 1]
    fact[n] %= MOD
fact = fact.ravel()
j = np.arange(N)
inv = np.array([pow(i + 1, MOD - 2, MOD) for i in range(N)])
inv = np.cumsum(inv) % MOD
temp = inv[j] + inv[N - 1 - j] - 1
ans = (A * temp % MOD).sum()
ans %= MOD
ans *= fact[N]
ans %= MOD
print(ans)


user: import java.io.BufferedReader;
import java.io.InputStreamReader;

public class atcoder_AGC028_B {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] sa = br.readLine().split(" ");
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(sa[i]);
        }
        br.close();
        int mod = 1000000007;
        long p = 1;
        for (int i = 2; i <= n; i++) {
            p = p * i % mod;
        }
        long[] q = new long[n];
        q[0] = 1;
        for (int i = 1; i < n; i++) {
            q[i] = (q[i - 1] + modinv(i + 1, mod)) % mod;
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            long val = q[i] + q[n - i - 1] - 1;
            val *= a[i];
            val %= mod;
            val *= p;
            val %= mod;
            ans += val;
            ans %= mod;
        }
        System.out.println(ans);
    }

    static long modinv(long a, int m) {
        long b = m;
        long u = 1;
        long v = 0;
        long tmp = 0;
        while (b > 0) {
            long t = a / b;
            a -= t * b;
            tmp = a;
            a = b;
            b = tmp;
            u -= t * v;
            tmp = u;
            u = v;
            v = tmp;
        }
        u %= m;
        if (u < 0) u += m;
        return u;
    }
}