input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
# -*- coding:utf-8 -*- import sys def search(x, y): x_length = len(x) y_length = len(y) memo = [[0] * (y_length + 1) for i in range(x_length + 1)] for i, ch_x in enumerate(x, 1): row = memo[i] pre_row = memo[i-1] for j, ch_y in enumerate(y, 1): if ch_x =...
# -*- coding:utf-8 -*- import sys def search(x, y): x_length = len(x) y_length = len(y) memo = [[0] * (y_length + 1) for i in range(x_length + 1)] for i, ch_x in enumerate(x, 1): row = memo[i] pre_row = memo[i-1] for j, ch_y in enumerate(y, 1): if ch_x =...
p02235
# 参考URL https://note.nkmk.me/python-union-find/ class UnionFind(): # 各要素の親要素の番号を格納するリスト # 要素が根(ルート)の場合は-(そのグループの要素数)を格納する def __init__(self, n): self.n = n self.parents = [-1] * n # 要素xが属するグループの根を返す def find(self, x): if self.parents[x] < 0: return x ...
# Reference: https://note.nkmk.me/python-union-find/ class UnionFind: # 各要素の親要素の番号を格納するリスト # 要素が根(ルート)の場合は-(そのグループの要素数)を格納する def __init__(self, n): self.n = n self.parents = [-1] * n # 要素xが属するグループの根を返す def find(self, x): history = [] while self.parents[x] ...
p02558
class DisjointSetUnion(): def __init__(self, n): self.n = n self.par_size = [-1] * n def merge(self, a, b): assert 0 <= a < self.n assert 0 <= b < self.n x = self.leader(a) y = self.leader(b) if x == y: return x if -self.par_size[x] < -...
class DisjointSetUnion(): def __init__(self, n): self.n = n self.par_size = [-1] * n def merge(self, a, b): #assert 0 <= a < self.n #assert 0 <= b < self.n x = self.leader(a) y = self.leader(b) if x == y: return x if -self.par_size[x] <...
p02558
from typing import Set class UnionFind: """Union-Find: O(α(N))""" __slots__ = ["_data_size", "_first_idx", "_parents"] def __init__(self, data_size: int, is_zero_origin: bool = True) -> None: self._data_size = data_size self._first_idx = 0 if is_zero_origin else 1 self...
class UnionFind: """Union-Find: O(α(N))""" __slots__ = ["_data_size", "_first_idx", "_parents"] def __init__(self, data_size: int, is_zero_origin: bool = True) -> None: self._data_size = data_size self._first_idx = 0 if is_zero_origin else 1 self._parents = [-1] * (data_siz...
p02558
class UnionFind: """Union-Find: O(α(N))""" __slots__ = ["_data_size", "_first_idx", "_parents"] def __init__(self, data_size: int, is_zero_origin: bool = True) -> None: self._data_size = data_size self._first_idx = 0 if is_zero_origin else 1 self._parents = [-1] * (data_siz...
class UnionFind: __slots__ = ["N", "root", "rank"] def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N def __repr__(self): ...
p02558
from typing import Set class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, N): self._data_size = N self._roots = [-1] * N def find(self, x): while self._roots[x] >= 0: x = self._roots[x] return x def unite(self, x, y): x = self.find(x) ...
p02558
from typing import List class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, N): self._data_size = N self._roots = [-1] * N def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x def unite(self, x, y): ...
p02558
from typing import List class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, N): self._data_size = N self._roots = [-1] * N def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x def unite(self, x, y): ...
p02558
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, N): self._data_size = N self._roots = [-1] * N def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x def unite(self, x, y): ...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, N): self._data_size = N self._roots = [-1] * N def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x def unite(self, x, y): ...
p02558
from typing import List class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
from typing import List class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
p02558
from typing import List class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] def __init__(self, data_s...
p02558
class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] def __init__(self, data_s...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, data_size: int) -> None: self._data_size = data_size self._roots = [-1] * data_size def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x ...
p02558
from typing import List class UnionFind: """Union Find (Disjoint Set): O(α(N)) References: https://github.com/atcoder/ac-library/blob/master/atcoder/dsu.hpp https://tumoiyorozu.github.io/single-file-ac-library/document_en/dsu.html """ __slots__ = ["_data_size", "_roots"] ...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, data_size: int) -> None: self._data_size = data_size self._roots = [-1] * data_size def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x ...
p02558
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, data_size: int) -> None: self._data_size = data_size self._roots = [-1] * data_size def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x ...
class UnionFind: __slots__ = ["_data_size", "_roots"] def __init__(self, data_size: int) -> None: self._data_size = data_size self._roots = [-1] * data_size def __getitem__(self, x: int) -> int: while self._roots[x] >= 0: x = self._roots[x] return x ...
p02558
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: li = [] while self.parents[x] >= 0: li.append(x) x = self.parent...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: li = [] while self.parents[x] >= 0: if self.parents[self.parents[x]] >= 0: ...
p02558
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: while self.parents[x] >= 0: if self.parents[self.parents[x]] >= 0: self.p...
class UnionFind(): def __init__(self, N): self.N = N self.parents = [-1] * N def find(self, x): if self.parents[x] < 0: return x else: st = [] while self.parents[x] >= 0: st.append(x) x = self.parent...
p02558
def main(): class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank =...
def main(): class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank =...
p02558
def main(): class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank =...
class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N def __repr__(self): return '\n'...
p02558
import sys input = sys.stdin.readline def solve(): N, q = list(map(int, input().split())) root = [-1] * N rank = [0] * N def find(x: int) -> int: if root[x] < 0: return x else: while root[x] >= 0: x = root[x] return x ...
import sys input = sys.stdin.readline def solve(): N, q = list(map(int, input().split())) root = [-1] * N def find(x: int) -> int: if root[x] < 0: return x else: while root[x] >= 0: x = root[x] return x def union(x: ...
p02558
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
p02558
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
p02558
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N def __repr__(self): return '\n'...
p02558
class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N def __repr__(self): return '\n'...
class UnionFind: def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N def __repr__(self): return '\n'...
p02558
class UnionFind: __slots__ = ["N", "root", "rank"] def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N ...
class UnionFind: __slots__ = ["N", "root", "rank"] def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N ...
p02558
class UnionFind: __slots__ = ["N", "root", "rank"] def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N self.rank = [0] * N ...
class UnionFind: __slots__ = ["N", "root", "rank"] def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. rank:ランク """ self.N = N self.root = [-1] * N def __repr__(self): ...
p02558
import typing class DSU: ''' Implement (union by size) + (path compression) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0): self._n = n self.parent_or_size = [-1]...
import typing class DSU: ''' Implement (union by size) + (path compression) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0): self._n = n self.parent_or_size = [-1]...
p02558
import typing class DSU: ''' Implement (union by size) + (path compression) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0): self._n = n self.parent_or_size = [-1]...
class UnionFind: __slots__ = ["N", "root"] def __init__(self, N): """ N:要素数 root:各要素の親要素の番号を格納するリスト. ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. """ self.N = N self.root = [-1] * N def __repr__(self): return '\n'.join('{}: {}'...
p02558
# UnionFind import sys def input(): return sys.stdin.readline().rstrip() sys.setrecursionlimit(10**9) from dataclasses import * @dataclass class UnionFind: def __init__(self, n): self.n = [-1]*n self.r = [0]*n self.siz = n def find_root(self, x): if self.n[x] < 0: return x ...
# UnionFind import sys def input(): return sys.stdin.readline().rstrip() sys.setrecursionlimit(1000000) class UnionFind: def __init__(self, n): self.n = [-1]*n self.r = [0]*n self.siz = n def find_root(self, x): if self.n[x] < 0: return x else: self.n[x] = self.find_ro...
p02558
# UnionFind import sys def input(): return sys.stdin.readline().rstrip() sys.setrecursionlimit(1000000) class UnionFind: def __init__(self, n): self.n = [-1]*n self.r = [0]*n self.siz = n def find_root(self, x): if self.n[x] < 0: return x else: self.n[x] = self.find_ro...
# UnionFind import sys input = lambda: sys.stdin.readline().rstrip() sys.setrecursionlimit(1000000) class UnionFind: def __init__(self, n): self.n = [-1]*n self.r = [0]*n self.siz = n def find_root(self, x): if self.n[x] < 0: return x else: self.n[x] = self.find_root(s...
p02558
# UnionFind import sys input = lambda: sys.stdin.readline().rstrip() sys.setrecursionlimit(1000000) class UnionFind: def __init__(self, n): self.n = [-1]*n self.r = [0]*n self.siz = n def find_root(self, x): if self.n[x] < 0: return x else: self.n[x] = self.find_root(s...
import sys input = lambda: sys.stdin.readline().rstrip() # UnionFind class UnionFind: def __init__(self,n): self.n=n self.a=[-1]*n self.siz=n def root_find(self,x): if self.a[x]<0: return x else: self.a[x]=self.root_find(self.a[x]) ...
p02558
# verify-helper: PROBLEM https://judge.yosupo.jp/problem/unionfind import sys input = sys.stdin.buffer.readline class UnionFindTree: """Disjoint-Set Data Structure Union-Find Tree complexity: - init: O(n) - find, unite, same: O(alpha(n)) """ def __init__(self, n...
# verify-helper: PROBLEM https://judge.yosupo.jp/problem/unionfind import sys input = sys.stdin.buffer.readline class UnionFindTree: """Disjoint-Set Data Structure Union-Find Tree complexity: - init: O(n) - find, unite, same: O(alpha(n)) """ def __init__(self, n...
p02558
# -*- coding: utf-8 -*- import bisect import heapq import math import random from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from fractions import Fraction from functools import lru_cache, reduce from itertools import combinations, combinations_with...
# -*- coding: utf-8 -*- import bisect import heapq import math import random from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from fractions import Fraction from functools import lru_cache, reduce from itertools import combinations, combinations_with...
p02558
#!usr/bin/env python3 from collections import defaultdict, deque, Counter, OrderedDict from bisect import bisect_left, bisect_right from functools import reduce, lru_cache from heapq import heappush, heappop, heapify import itertools import math, fractions import sys, copy def L(): return sys.stdin.readline...
#!usr/bin/env python3 from collections import defaultdict, deque, Counter, OrderedDict from bisect import bisect_left, bisect_right from functools import reduce, lru_cache from heapq import heappush, heappop, heapify import itertools import math, fractions import sys, copy def L(): return sys.stdin.readline...
p02558
#!usr/bin/env python3 from collections import defaultdict, deque, Counter, OrderedDict from bisect import bisect_left, bisect_right from functools import reduce, lru_cache from heapq import heappush, heappop, heapify import itertools import math, fractions import sys, copy def L(): return sys.stdin.readline...
#!usr/bin/env python3 from collections import defaultdict, deque, Counter, OrderedDict from bisect import bisect_left, bisect_right from functools import reduce, lru_cache from heapq import heappush, heappop, heapify import itertools import math, fractions import sys, copy def L(): return sys.stdin.readline...
p02558
class dsu: def __init__(self, n=0): self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: x = self.leader(a) y = self.leader(b) if x == y: return x if self.parent_or_size[x] > self.parent_or_size[y]: ...
class dsu: def __init__(self, n=0): self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: x = self.leader(a) y = self.leader(b) if x == y: return x if self.parent_or_size[x] > self.parent_or_size[y]: ...
p02558
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
p02558
from typing import List class DSU: def __init__(self, n: int) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a <= self._n assert 0 <= b <= self._n x, y = self.leader(a), self.leader(b) if x ==...
class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self...
p02558
class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self...
import sys input = sys.stdin.readline class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[...
p02558
import sys input = sys.stdin.readline class DSU: __slots__ = ["_n", "parent_or_size"] def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): x, y = self.leader(a), self.leader(b) if x == y: return x if -self...
import sys input = sys.stdin.readline class DSU: __slots__ = ["_n", "parent_or_size"] def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): x, y = self.leader(a), self.leader(b) if x == y: return x if self....
p02558
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict mod = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class UnionFind: def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [0] * (n ...
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict mod = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class UnionFind: def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [0] * (n ...
p02558
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y...
p02558
import types # from atcoder.dsu import DSU _atcoder_code = """ # Python port of AtCoder Library. """ atcoder = types.ModuleType('atcoder') exec(_atcoder_code, atcoder.__dict__) _atcoder_dsu_code = """ import typing class DSU: ''' Implement (union by size) + (path compression) Referen...
import types _atcoder_code = """ # Python port of AtCoder Library. __version__ = '0.0.1' """ atcoder = types.ModuleType('atcoder') exec(_atcoder_code, atcoder.__dict__) _atcoder_dsu_code = """ import typing class DSU: ''' Implement (union by size) + (path compression) Reference: ...
p02558
class UnionFind: def __init__(self, n: int) -> None: self.forest = [-1] * n def union(self, x: int, y: int) -> None: x = self.findRoot(x) y = self.findRoot(y) if x == y: return if self.forest[x] > self.forest[y]: x, y = y, x sel...
import sys class UnionFind: def __init__(self, n: int) -> None: self.forest = [-1] * n def union(self, x: int, y: int) -> None: x = self.findRoot(x) y = self.findRoot(y) if x == y: return if self.forest[x] > self.forest[y]: x, y = y...
p02558
#!/usr/bin/env python n, q = list(map(int ,input().split())) par = [-1 for _ in range(n)] rank = [-1 for _ in range(n)] def init(n): for i in range(n): par[i] = i rank[i] = 0 def root(x): if par[x] == x: return x # ここをしっかり理解する else: par[x] = root(pa...
#!/usr/bin/env python class UnionFind(): def __init__(self, n): self.n = n self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.sizes = [1 for _ in range(n)] def root(self, x): if self.par[x] == x: return x else: ...
p02558
s,k = input().split() mod = 998244353 k = int(k) k = min(200,k) raw = [0] l = len(s) for i in range(l): if s[i] == '0': raw.append(0) else: raw[-1] += 1 #print(raw) now = [[0 for _ in range(k+1)] for _ in range(k+1)] l = len(raw) raw.reverse() #print(raw) now[0][0] = 1 for x in raw: last...
s,k = input().split() mod = 998244353 k = int(k) k = min(300,k) raw = [0] l = len(s) for i in range(l): if s[i] == '0': raw.append(0) else: raw[-1] += 1 #print(raw) now = [[0 for _ in range(k+1)] for _ in range(k+1)] l = len(raw) raw.reverse() #print(raw) now[0][0] = 1 for x in raw: last...
p02635
from functools import lru_cache import sys sys.setrecursionlimit(10 ** 5) MOD = 998244353 # TLE def solve(S, K): numZeroes = S.count("0") # Count number of ones before each zero. This is a unique representation of the string ones = [0 for i in range(numZeroes + 1)] j = 0 for x in S: ...
from functools import lru_cache import sys sys.setrecursionlimit(10 ** 5) MOD = 998244353 # TLE def solve(S, K): numZeroes = S.count("0") # Count number of ones before each zero. This is a unique representation of the string ones = [0 for i in range(numZeroes + 1)] j = 0 for x in S: ...
p02635
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from copy import deepcopy as dcp from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from co...
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from copy import deepcopy as dcp from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from co...
p02635
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) dp=[[0 for i in range(K+1)] for j in range(K+1)] for j in range(K+1): dp[j][j]=1 f...
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) dp=[[[0 for i in range(K+1)] for j in range(K+1)] for k in range(m+1)] fo...
p02635
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) dp=[[[0 for i in range(K+1)] for j in range(K+1)] for k in range(m+1)] fo...
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) dp=[[[0 for i in range(K+1)] for j in range(K+1)] for k in range(m+1)] fo...
p02635
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) dp=[[[0 for i in range(K+1)] for j in range(K+1)] for k in range(m+1)] fo...
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) dp=[[0 for i in range(K+1)] for j in range(K+1)] for j in range(K+1): ...
p02635
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) dp=[[0 for i in range(K+1)] for j in range(K+1)] for j in range(K+1): ...
S,K=input().split() K=int(K) K=min(K,300) mod=998244353 a=[] val=0 for i in range(len(S)): if S[i]=="0": a.append(val) val=0 else: val+=1 if val!=0: a.append(val) m=len(a) K=min(sum(a),K) b=[a[i] for i in range(m)] c=[a[i] for i in range(m)] for i in range(1,m):...
p02635
def main(): mod = 998244353 s, k = input().split() k = int(k) n = len(s) one = s.count("1") cnt = 0 zero_list = [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 z = 0 dp =...
def main(): mod = 998244353 s, k = input().split() k = int(k) n = len(s) one = s.count("1") cnt = 0 zero_list = [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 z = 0 mm = m...
p02635
def main(): mod = 998244353 s, k = input().split() k = int(k) n = len(s) one = s.count("1") cnt = 0 zero_list = [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 z = 0 mm = m...
def main(): mod = 998244353 s, k = input().split() k, n, one, cnt, z, zero_list = int(k), len(s), s.count("1")+1, 0, 0, [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 mm = min(one-1, k) dp = [[0]...
p02635
def main(): mod = 998244353 s, k = input().split() k, n, one, cnt, z, zero_list = int(k), len(s), s.count("1")+1, 0, 0, [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 mm = min(one-1, k) ans = 0 ...
def main(): mod = 998244353 s, k = input().split() k, n, one, cnt, z, zero_list = int(k), len(s), s.count("1")+1, 0, 0, [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 mm = min(one-1, k) dp = [[0]...
p02635
def main(): mod = 998244353 s, k = input().split() k, n, one, cnt, z, zero_list = int(k), len(s), s.count("1")+1, 0, 0, [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 mm = min(one-1, k) dp = [[0]...
def main(): mod = 998244353 s, k = input().split() k, n, o, cnt, z, zero_list = int(k), len(s), s.count("1")+1, 0, 0, [] for i in range(n): if s[i] == "0": zero_list.append(cnt) cnt = 0 else: cnt += 1 m = min(o, k+1) dp = [[0]*m fo...
p02635
# coding: utf-8 # Your code here! import sys read = sys.stdin.read readline = sys.stdin.readline #a,b,c,d = map(int,readline().split()) s,k = readline().split() a = [len(i) for i in s.split("0")] while a and a[-1] == 0: a.pop() if not a: print((1)) exit() MOD = 998244353 M = sum(a)+1 k...
# coding: utf-8 # Your code here! import sys read = sys.stdin.read readline = sys.stdin.readline #a,b,c,d = map(int,readline().split()) s,k = readline().split() a = [len(i) for i in s.split("0")] while a and a[-1] == 0: a.pop() if not a: print((1)) exit() MOD = 998244353 M = sum(a)+1 k...
p02635
s,k = input().split() K = int(k) if s[-1] == "1": s += "0" n = len(s) ls = [] cnt = 0 for i in range(n): if s[i] == "0": ls.append(cnt) cnt = 0 else: cnt += 1 l = len(ls) sm = sum(ls) mod = 998244353 dp = [[[0 for i in range(sm+1)] for j in range(sm+1)] for k in range(l+1)] dp[0][0][0...
s,k = input().split() K = int(k) if s[-1] == "1": s += "0" n = len(s) ls = [] cnt = 0 for i in range(n): if s[i] == "0": ls.append(cnt) cnt = 0 else: cnt += 1 l = len(ls) sm = sum(ls) mod = 998244353 dp = [[[0 for i in range(sm+1)] for j in range(sm+1)] for k in range(l+1)] dp[0][0][0...
p02635
import sys a, b, c, d = list(map(int, sys.stdin.readline().split())) def main(): return 'Yes' if abs(c - a) <= d or abs(b - a) <= d and abs(c - b) <= d else 'No' if __name__ == '__main__': ans = main() print(ans)
import sys a, b, c, d = list(map(int, sys.stdin.readline().split())) def main(): ans = 'No' if abs(c - a) <= d: ans = 'Yes' elif abs(b - a) <= d and abs(c - b) <= d: ans = 'Yes' print(ans) if __name__ == '__main__': main()
p03351
a,b,c,d = list(map(int,input().split())) if abs(a-c) <= d or (abs(a-b) <= d and abs(c-b) <= d): print("Yes") else: print("No")
A,B,C,D = list(map(int,input().split())) print(("Yes" if abs(A-C)<=D or abs(A-B)<=D and abs(B-C)<=D else "No"))
p03351
a,b,c,d=list(map(int, input().split())) if (abs(a-c)<=d) or (abs(a-b)<=d) and (abs(b-c)<=d): print('Yes') else: print('No')
a,b,c,d=list(map(int, input().split())) print(("Yes" if (abs(a-c)<=d) or (abs(a-b)<=d) and (abs(b-c)<=d) else "No"))
p03351
a,b,c,d = list(map(int, input().split())) if (a-c)**2<=d**2 or ((a-b)**2<=d**2 and (b-c)**2<=d**2): print("Yes") else: print("No")
a,b,c,d=list(map(int, input().split())) print(("Yes" if abs(a-c)<=d or (abs(a-b)<=d and abs(b-c)<=d) else "No"))
p03351
a, b, c, d = list(map(int, input().split())) if abs(b - a) <= d and abs(c - b) <= d: print("Yes") elif abs(c - a) <= d: print("Yes") else: print("No")
a, b, c, d = list(map(int, input().split())) if abs(a-b) <= d and abs(b-c) <= d: print("Yes") elif abs(a-c) <= d: print("Yes") else: print("No")
p03351
a, b, c, d = list(map(int, input().split())) print(('Yes' if abs(c - a) <= d or abs(b - a) <= d and abs(c - b) <= d else 'No'))
a, b, c, d = list(map(int, input().split())) if abs(c - a) <= d or abs(b - a) <= d and abs(c - b) <= d: print('Yes') else: print('No')
p03351
a,b,c,d=list(map(int,input().split())) print(("Yes" if abs(c-a)<=d or (abs(b-a)<=d and abs(c-b)<=d) else "No"))
a,b,c,d=list(map(int,input().split())) print(("Yes" if abs(c-a)<=d or abs(b-a)<=d and abs(c-b)<=d else "No"))
p03351
a, b, c, d = list(map(int,input().split())) # aとcの距離の絶対値ACを定義 if (a - c) >= 0: AC = (a-c) else: AC = (c-a) # BがAとCの間に入る場合のみ、AB間、BC間を定義 if a < b < c: AB = (b - a) BC = (c - b) if c < b < a: AB = (a-b) BC = (b-c) # AとCの距離がd以下ならYes! if AC <= d: print('Yes') # AB間、BC間ともにd以下...
a, b, c, d = list(map(int,input().split())) # aとcの距離ACを定義 if (a - c) >= 0: AC = (a-c) else: AC = (c-a) # BがAとCの間に入る場合のみ、AB間、BC間を定義 if a < b < c: AB = (b - a) BC = (c - b) if c < b < a: AB = (a-b) BC = (b-c) # AとCの距離がd以下ならYes! if AC <= d: print('Yes') # AB間、BC間ともにd以下ならYe...
p03351
a, b, c, d = list(map(int, input().split())) print(("Yes" if abs(a-c) <= d or (abs(a-b) <= d and abs(b-c) <= d) else "No"))
a, b, c, d = list(map(int, input().split())) if abs(a-c)<=d: print('Yes') elif not ((a<=b<=c)or(c<=b<=a)): print('No') else: if (abs(a-b)<=d)and(abs(b-c)<=d): print('Yes') else: print('No')
p03351
a,b,c,d = list(map(int,input().split())) if (abs(b-a)<=d and abs(c-b)<=d) or abs(c-a)<=d: print("Yes") else: print("No")
a,b,c,d = list(map(int,input().split())) print(("YNeos"[(abs(a-b)>d or abs(b-c)>d) and abs(a-c)>d::2]))
p03351
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, ...
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, ...
p03174
import sys def input(): return sys.stdin.readline().strip() def resolve(): n=int(eval(input())) l=list(map(int,input().split())) heikin=sum(l)/n samunenum=100 samune=10**18 for i in range(n): x=abs(heikin-l[i]) if x<samune: samunenum=i samune...
import sys def input(): return sys.stdin.readline().strip() def resolve(): n=int(eval(input())) l=list(map(int,input().split())) samunenum=100 samune=10**18 for i in range(n): x=abs(sum(l)-n*l[i]) if x<samune: samunenum=i samune=x print(samun...
p03214
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] avg = sum(A)/N ans = 0 mi = abs(A[0] - avg) for i, a in enumerate(A[1:], start=1): if abs(a - avg) < mi: ans = i mi = abs(a - avg) print(ans) if __name__ == '__main__': ...
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] avg = sum(A)/N mi = abs(A[0] - avg) idx = 0 for i, a in enumerate(A[1:], start=1): if abs(a - avg) < mi: mi = abs(a - avg) idx = i print(idx) if __name__ == '__main__': ...
p03214
def main(): n = int(eval(input())) a_list = list(map(int, input().split())) mean_a = sum(a_list) / n diff_list = [abs(a - mean_a) for a in a_list] min_a = min(diff_list) print((diff_list.index(min_a))) if __name__ == "__main__": main()
def main(): n = int(eval(input())) a_list = list(map(int, input().split())) sum_a = sum(a_list) min_val = 100 min_index = 0 for i, a in enumerate(a_list): d = abs(n * a - sum_a) if d < min_val: min_val = d min_index = i print(min_index) ...
p03214
n=int(eval(input())) *a,=list(map(int,input().split())) avg=sum(a)/n c=[(abs(j-avg),i) for i,j in enumerate(a)] c.sort(key=lambda x:(x[0],x[1])) print((c[0][1]))
n=int(eval(input())) *a,=list(map(int,input().split())) avg=sum(a)/n ans,m=0,1e22 for i,j in enumerate(a): if abs(j-avg)<m: ans=i m=abs(j-avg) print(ans)
p03214
n = int(eval(input())) w = list(map(int,input().split())) # s1とs2の差の絶対値の最小値が最小になる部分を中心として考えれば良い # wの最大値と最小値を順々に足したものを比較すれば良い s1 = [] s2 = [] while len(w)>0: if sum(s1) <= sum(s2): s1.append(w.pop(0)) else: s2.append(w.pop(-1)) print((abs(sum(s1)-sum(s2))))
n = int(eval(input())) w = list(map(int, input().split())) z = [] for i in range(n): x = sum(w[:i]) y = sum(w[i:]) z.append(abs(x-y)) print((min(z)))
p03012
l = int(eval(input())) ws = list(map(int, input().split())) c = 9999 for i in range(l): c = min([c, abs(sum(ws[i:])- sum(ws[:i]))]) print(c)
l = int(eval(input())) ws = list(map(int, input().split())) print((min([abs(sum(ws[i:]) - sum(ws[:i])) for i in range(l)])))
p03012
n = int(eval(input())) w = list(map(int, input().split())) a = 10**19 for i in range(1, n-1): a = min(abs(sum(w[:i+1]) - sum(w[i+1:])), a) print(a)
# 問題が少しわかりずらい。 n = int(eval(input())) w = list(map(int, input().split())) a = 10**19 for i in range(1, n-1): # 重りを二つに分け、差異の最小値を判定する。 a = min(abs(sum(w[:i+1]) - sum(w[i+1:])), a) print(a)
p03012
n = int(eval(input())) l = list(map(int, input().split())) print((min(abs(sum(l[:i])-sum(l[i:])) for i in range(1,n))))
eval(input()) l=list(map(int,input().split())) s=sum(l) a,t=s,0 for i in l: t+=i; a=min(a,abs(s-t*2)) print(a)
p03012
# B - Balance n = int(eval(input())) w = list(int(x) for x in input().split()) ans = 10**6 for i in range(1, n): ans = min(ans, abs(sum(w[:i]) - sum(w[i:]))) print(ans)
# B - Balance n = int(eval(input())) w = list(int(x) for x in input().split()) wsum = [0] for i in range(n): wsum.append(wsum[i] + w[i]) ans = 10**6 for i in range(1, n): ans = min(ans, abs(wsum[n]-wsum[i]-wsum[i])) print(ans)
p03012
N = int(eval(input())) W = list(map(int, input().split())) l = [] for T in range(N): sum1 = sum(W[:T+1]) sum2 = sum(W[T+1:]) d = abs(sum1-sum2) l.append(d) print((min(l)))
N = int(eval(input())) W = list(map(int, input().split())) l = [] for T in range(N): sum1 = sum(W[:T]) sum2 = sum(W[T:]) d = abs(sum1-sum2) l.append(d) print((min(l)))
p03012
N = int(eval(input())) W = list(map(int, input().split())) ww = [] for i in range(N): ww.append(sum(W[0:i+1])) ans = [abs((2 * q) - ww[N-1]) for q in ww] print((min(ans)))
N = int(eval(input())) W = list(map(int, input().split())) ans = [abs(sum(W[0:i+1]) - sum(W[i+1:])) for i in range(N-1)] print((min(ans)))
p03012
# 129 B n = int(eval(input())) l = list(map(int, input().split())) diff = (100 ** 100) for t in range(len(l)): s1 = 0 s2 = 0 for i in range(len(l)): if i <= t: s1 += l[i] else: s2 += l[i] x = abs(s2 - s1) if x <= diff: diff = x...
n = int(eval(input())) w = list(map(int, input().split())) p = [0 for i in range(n)] p[0] = w[0] for i in range(1, n): p[i] = p[i-1] + w[i] diff = float('inf') for j in range(n): cd = abs(p[n-1] - (2*p[j])) diff = min(diff, cd) print(diff)
p03012
n = int(eval(input())) w = list(map(int, input().split())) ans = 10000000000 for t in range(1, n): left, right = 0, 0 for i in range(t): left += w[i] for i in range(t, n): right += w[i] ans = min(ans, abs(left - right)) print(ans)
n = int(eval(input())) w = list(map(int, input().split())) ans = 10000000000 for t in range(1, n): left = sum(w[:t]) right = sum(w[t:]) ans = min(ans, abs(left - right)) print(ans)
p03012
N = int(eval(input())) W = list(map(int, input().split())) # O(N^2)で解いても間に合う m = 10**10 for i in range(1, N): a, b = 0, 0 for j in range(i): a += W[j] for j in range(i, N): b += W[j] m = min(m, abs(a-b)) print(m)
N = int(eval(input())) W = list(map(int, input().split())) # 累積和 => O(N) S = [0] * N S[0] = W[0] for i in range(N-1): S[i+1] = S[i] + W[i+1] # S[0] = W[0] # S[1] = W[0] + W[1] # S[2] = W[0] + W[1] + W[2] # S[N-1] = W[0] + W[1] + ... + W[N-1] # S[i] = W[0] + ... + W[i] # S[N-1] - S[i] = W[i+1] + .....
p03012
n = int(eval(input())) w = list(map(int, input().split())) ans = 10 ** 9 for t in range(n): cnt = 0 for i in range(n): a = 1 if i > t: a = -1 cnt += a * w[i] ans = min(ans, abs(cnt)) print(ans)
n = int(eval(input())) w = list(map(int, input().split())) ans = 10 ** 9 now = sum(w) for i in range(n): now -= 2 * w[i] ans = min(ans, abs(now)) print(ans)
p03012
n = int(eval(input())) lst = list(map(int,input().split())) lst2 = [0]*n all_sum = 0 for e in lst: all_sum += e for i in range(n): for j in range(i+1): lst2[i] += lst[j] for before,now in zip(lst2,lst2[1:]): if abs(abs(now-all_sum)-abs(now))<abs(abs(before-all_sum)-abs(before)): ...
n = int(eval(input())) lst = list(map(int,input().split())) ans = None for t in range(n-1): left = [x for x in lst[:t+1]]#t+1未満まで right = [x for x in lst[t+1:]]#t+1以上 sum_left=0 for e in left: sum_left+=e sum_right=0 for e in right: sum_right+=e if ans==...
p03012
n = int(eval(input())) w = list(map(int, input().split())) l =[] for i in range(n): l.append(abs(sum(w[:i]) - sum(w[i:]))) print((min(l)))
n = int(eval(input())) w = list(map(int, input().split())) ans = 10000 for i in range(n): s = abs(sum(w[:i]) - sum(w[i:])) ans = min(s,ans) print(ans)
p03012
import sys stdin = sys.stdin mod = 1000000007 inf = 1 << 60 ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().rstrip() nas = lambda: stdin.readline().split() n = ni() w = na() ans = inf d = [0] * (n + 1) b = [0] * (n + 1) for i in range(1, ...
import sys stdin = sys.stdin mod = 1000000007 inf = 1 << 60 ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().rstrip() nas = lambda: stdin.readline().split() n = ni() w = na() ans = inf s = sum(w) p = 0 for i in range(n): p += w[i] ans ...
p03012
n=int(eval(input())) a=list(map(int,input().split())) mini=float('inf') sum1=0 sum2=sum(a) t=0 while t<10**7: t+=1 for i in range(n): sum1=sum1+a[i] sum2=sum2-a[i] mini=min(mini,abs(sum1-sum2)) print(mini)
n=int(eval(input())) a=list(map(int,input().split())) mini=float('inf') sum1=0 sum2=sum(a) t=0 while t<n: sum1=sum1+a[t] sum2=sum2-a[t] mini=min(mini,abs(sum1-sum2)) t+=1 print(mini)
p03012
n = int(eval(input())) arr = list(map(int, input().split())) dic = {} for i in range(1,n+1): S1 = sum(arr[:i]) S2 = sum(arr[i:]) dic[i] = abs(S1-S2) ans = sorted(list(dic.items()), key=lambda x:x[1]) print((ans[0][1]))
N = int(eval(input())) A = list(map(int, input().split())) ans = 1<<60 for t in range(1, N): L = sum(A[:t]) R = sum(A[t:]) ans = min(ans, abs(L-R)) print(ans)
p03012
x = 10**9 n = int(eval(input())) w = list(map(int,input().split())) for i in range(n): g,t = 0,0 for j in range(i): g+=w[j] for k in range(n-i): t+=w[n-k-1] x = min(x,abs(g-t)) print(x)
n = int(eval(input())) a = list(map(int,input().split())) s,g = sum(a),0 ans = s for i in a: g+=i s-=i ans = min(ans,abs(s-g)) print(ans)
p03012
n = int(eval(input())) lst_W = list(map(int, input().split())) ans = 999 for t in range(n): low = sum(lst_W[:t]) high = sum(lst_W[t:]) if ans > abs(high - low): ans = abs(high - low) print(ans)
n = int(eval(input())) w_lst = list(map(int, input().split())) min = 1000000 for i in range(1, n): s1 = sum(w_lst[:i]) s2 = sum(w_lst[i:]) if abs(s1 - s2) < min: min = abs(s1 - s2) print(min)
p03012
n = int(eval(input())) lst = [int(i) for i in input().split()] for i in range(n): s1=0 for j in range(i): s1+=lst[j] s2=sum(lst)-s1 if i==0: ans=abs(s1-s2) elif abs(s1-s2)<ans: ans=abs(s1-s2) print(ans)
n = int(eval(input())) lst = list(map(int,input().split())) ans = 10**9 for i in range(n-1): ans = min(ans,abs(sum(lst[:i+1])-sum(lst[i+1:]))) print(ans)
p03012
# coding: utf-8 # Your code here! N = int(eval(input())) W = list(map(int,input().split())) S = sum(W) ans = float('inf') for i in range(1,N): l = sum(W[:i+1]) r = S - l ans = min(ans, abs(l-r)) print(ans)
# coding: utf-8 # Your code here! N = int(eval(input())) W = list(map(int,input().split())) S = sum(W) ans = float('inf') for i in range(N): l = sum(W[:i+1]) r = S - l ans = min(ans, abs(l-r)) print(ans)
p03012
# -*- coding: utf-8 -*- n=int(eval(input())) W=list(map(int,input().split())) t=0 while sum(W[:t])<sum(W[t:]) and t<n: t=t+1 print((min(abs(sum(W[:t])-sum(W[t:])),abs(sum(W[:t-1])-sum(W[t-1:])))))
# -*- coding: utf-8 -*- n=int(eval(input())) W=list(map(int,input().split())) for i in range(n): if sum(W[:i])>sum(W[i:]) or i==n-1: print((min(sum(W[i-1:])-sum(W[:i-1]),sum(W[:i])-sum(W[i:])))) break
p03012
from itertools import accumulate N = int(eval(input())) W = list(accumulate([int(x) for x in input().split()])) ans = 10**9 for i in range(N - 1): ans = min(ans, abs(W[N - 1] - W[i]*2)) print(ans)
N = int(eval(input())) W = [int(x) for x in input().split()] s1 = 0 s2 = sum(W) ans = 10**9 for w in W: s1 += w s2 -= w ans = min(ans, abs(s1 -s2)) print(ans)
p03012
from collections import deque n = int(eval(input())) w = list(map(int,input().split())) d = deque(w) s1 = d.popleft() s2 = d.pop() for _ in range(n-2): if s1 < s2: s1 += d.popleft() else: s2 += d.pop() print((abs(s1 - s2)))
n = int(eval(input())) w = list(map(int,input().split())) ans = [] for i in range(1,n): ans.append(abs(sum(w[:i])-sum(w[i:]))) print((min(ans)))
p03012
N = int(eval(input())) W = list(map(int, input().split())) ans = float('inf') for T in range(1, N): ans = min(ans, abs(sum(W[T:])-sum(W[:T]))) print(ans)
n = int(eval(input())) W = list(map(int, input().split())) ans = float('inf') for T in range(1, n): ans = min(ans, abs(sum(W[:T])-sum(W[T:]))) print(ans)
p03012
n = int(eval(input())) w = list(map(int, input().split())) min_diff = 1000 for i in range(n): diff = abs(sum(w[0:i]) - sum(w[i:])) if diff < min_diff: min_diff = diff print(min_diff)
N = int(eval(input())) W = list(map(int, input().split())) ans = 1000 for i in range(N): mi = abs(sum(W[0:i]) - sum(W[i:])) if mi < ans: ans = mi print(ans)
p03012
n = int(eval(input())) l = list(map(int, list(input().split()))) s = sum(l) temp = 0 if n == 2: print((abs(l[-1] - l[0]))) exit(0) for i in range(n): if temp >= s - temp: print(( min(abs(2*temp-s), abs(2*(temp-l[i-1])-s)) )) exit(0) temp += l[i] pr...
l=int(eval(input())) b=[int(i) for i in input().split()] print((sorted(abs(sum(b[i:])-sum(b[:i])) for i in range(0,l))[0]))
p03012
n = int(eval(input())) x = list(map(int, input().split())) left = 0 right = 0 for i in x: right += i reco = [] i = 0 while right > left: left += x[i] right -= x[i] i += 1 reco.append(right - left) reco[-1] = 0-reco[-1] print((min(reco)))
n = int(eval(input())) x = list(map(int, input().split())) left = 0 right = 0 for i in x: right += i reco = 0 i = 0 while right > left: left += x[i] right -= x[i] i += 1 if right <= left: print((min(reco, 0-(right - left)))) break else: reco = right - left...
p03012
N = int(eval(input())) W = list(map(int, input().split())) ttl = sum(W) s = [] min = ttl for i in range(len(W)): s.append(W[i]) s2 = ttl - sum(s) if abs(sum(s) - s2) < min: min = abs(sum(s) - s2) print(min)
N = int(eval(input())) W = list(map(int, input().split())) ttl = sum(W) for i in range(len(W)): ttl = min(ttl, abs(sum(W[:i]) - sum(W[i:]))) print(ttl)
p03012