akhauriyash/Code-Regression huggingface dataset:
#拡張ダイクストラ 
#weight,point
N,M,S=map(int,input().split())
graph={(i,j):[] for i in range(1,51) for j in range(2501)}
for _ in range(M):
    u,v,a,b=map(int,input().split())
    for i in range(a,2501):
        graph[(u,i)].append((b,(v,i-a)))
        graph[(v,i)].append((b,(u,i-a)))
for i in range(N):
    c,d=map(int,input().split())
    for j in range(2501):
        graph[(i+1,j)].append((d,(i+1,min(2500,j+c))))

dist={(i,j):float("inf")  for i in range(1,51) for j in range(2501)}

dist[(1,min(2500,S))]=0
pq=[]
import heapq
heapq.heapify(pq)
heapq.heappush(pq,(0,(1,min(2500,S))))
while pq:
    mini_dis,node=heapq.heappop(pq)
    if dist[node]<mini_dis: continue
    for w,point in graph[node]:
        if dist[point]<w:continue
        newlen=dist[node]+w
        if newlen<dist[point]:
            heapq.heappush(pq,(newlen,point))
            dist[point]=newlen
for i in range(2,N+1):
    ans=10**20
    for j in range(2501):
        ans=min(ans,dist[(i,j)])
    print(ans)


Measured memory_bytes: 162096
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f),(t)-(f)))
#define ALL(a) (a).begin(),(a).end()
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll a,b,c,d,e,n,ans = 0,cnt = 0;
  string s,ss,sss;
  cin>>s>>ss;
  for(int i=0;i<s.size();i++){
    if(s[i] !=ss[i]){
      cout<<"No"<<endl;
      return 0;
    }
  }
  cout<<"Yes"<<endl;
  return 0;
}

Measured memory_bytes: 3648
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

ll n, cnt, ans, a, b, c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k;
ld doua;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz");
char s[100][100];
bool fl;
struct edge
{
    int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> h >> w;
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> s[i][j];
        }
    }
    rep(i, h)
    {
        rep(j, w)
        {
            if (s[i][j] == '.')
                continue;
            int ok = 0;
            rep(k, 4)
            {
                int nx = j + dx[k], ny = i + dy[k];
                if (nx >= 0 && nx < w && ny >= 0 && ny < h && s[ny][nx] == '#')
                    ok = 1;
            }
            if (!ok)
            {
                Cout("No");
                return 0;
            }
        }
    }
    Cout("Yes");
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <cstdio>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define db(x) cerr << #x << " = " << x << endl;

typedef long long   ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll,  ll  > pll;

const ll   MOD       = 1000000007;
const ll   INF       = 1000000007ll*1000000007ll;
const ll   N         = 1000002;
const ll   logN      = 20;
const ld   PI        = acos(-1);
const ll   HashPrime = 31;

inline void No() {
    cout << "NO\n";
}
inline void Yes() {
    cout << "YES\n";
}

#define fi first
#define se second

int a[2][200];

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[0][i];
    if (i > 0) {
      a[0][i] += a[0][i-1];
    }
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[1][i];
    if (i > 0) {
      a[1][i] += max(a[0][i], a[1][i-1]);
    }
    else {
      a[1][i] += a[0][i];
    }
  }
  cout << a[1][n-1] << endl;
}

int main() {
  ll t = 1;
//  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Measured memory_bytes: 256
import math
from decimal import Decimal

def main():
    a,b,c = map(int,input().split())
    if c-a-b <= 0:
        print('No')
        return
    l = 4 * a * b
    r = c*c + b*b + a*a + 2*a*b -2*a*c -2*b*c

    if l<r:
        print('Yes')
    else:
        print('No')

if __name__=='__main__':
    main()


Measured memory_bytes: 5332
#include <iostream>
#include <cmath>
#include <algorithm>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
using namespace std;

int main(){
    int a, b, t, ans = 0;
    cin >> a >> b >> t;
    ans = t /  a * b;
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 256
local function read_a(n,m)m=m or 1 r={}for i=1,m do r[i]={}end for i=1,n do for j=1,m do r[j][i]=io.read"*n"end end return unpack(r)end

N=io.read"*n"
A=read_a(N)

s=0
m=math.huge
for i=1,N do
	if(m>A[i])then s=s+1 m=A[i]end
end
print(s)

Measured memory_bytes: 2304
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll MOD=1000000007;
const double PI=3.14159265358979;
const ll INF= pow(10,18);
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n;
  cin >> n;
  vector<P> p(n);
  rep(i,n){
    cin >> p[i].first;
  }
  rep(i,n){
    cin >> p[i].second;
  }
  vl c(n);
  ll sum=0;
  ll sum2=0;
  ll cnt=0;
  rep(i,n){
    c[i]=p[i].first-p[i].second;
    sum +=c[i];
    if(c[i]<0){
      sum2+=c[i];
      cnt++;
    }
  }
  if(sum<0){
    cout << -1 << endl;
  }
  else{
    sort(c.begin(),c.end());
    reverse(c.begin(),c.end());
    ll i=0;
    while(sum2<0){
      sum2+=c[i];
      i++;
      cnt++;
    }
    cout << cnt << endl;
  }
}

Measured memory_bytes: 2560
from heapq import heapify, heappop, heappush
N,M = map(int,input().split())
A = [[] for _ in range(M+1)]
#HQ = []
for i in range(N):
  a,b = map(int,input().split())
  if a <= M:
    A[a].append(b)
#print(A)
ans = 0
L = []
for day in range(1,M+1):
  for t in A[day]:
    heappush(L,-t)
  #print(L)
  if not L:
    continue
  temp = heappop(L)
  ans += -temp
print(ans)  

Measured memory_bytes: 24124
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
using ll=long long;

const int N_MAX=100001;
ll prime[N_MAX],ct[N_MAX],Q,l,r;
bool is_prime[N_MAX];

ll sieve(ll n){
    int p=0;
    for(int i=0;i<=n;i++){
        is_prime[i]=true;
    }
    is_prime[0]=is_prime[1]=false;
    for(int i=2;i<=n;i++){
        if(is_prime[i]){
            prime[p++]=i;
            for(int j=i+i;j<=n;j+=i){
                is_prime[j]=false;
            }
        }
    }
    for(int i=1;i<=N_MAX;i+=2){
        if(is_prime[i]&&is_prime[(i+1)/2]){
            ct[i]=1;
        }
    }
    for(int i=1;i<=N_MAX;i++){
        ct[i]=ct[i]+ct[i-1];
    }
    return p;
}


int main(){
    sieve(N_MAX-1);
    cin>>Q;
    for(int i=0;i<Q;i++){
        cin>>l>>r;
        cout<<ct[r]-ct[l-1]<<endl;
    }
    return 0;
}


Measured memory_bytes: 1536
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <cctype>
#include <cstdlib>
#include <typeinfo>

#define rep(i, n) for(int i=0;i<(int)(n);++i)

using namespace std;

template<class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

void trace(int A[], int N) {
    for (int i = 0; i < N; ++i) {
        if (i > 0) cout << " ";
        cout << A[i];
    }
    cout << endl;
}

using ll = long long;
using P = pair<int, int>;

const unsigned long long INF = 1LL << 60;

////////////////////////////////////////////////////////////

int H1, M1, H2, M2, K;

int main() {
    cin >> H1 >> M1 >> H2 >> M2 >> K;
    int H3 = H2 - H1;
    int M3 = M2 - M1;
    int d = H3 * 60 + M3 - K;
    if (d > 0) {
        cout << d << endl;
    } else {
        cout << 0 << endl;
    }
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
bool vis[100005];
int dp[100005];
int dfs(int x)
{
	if(vis[x])return dp[x];
	
	int len=0;vis[x]=1;
	for(auto u: adj[x] )
	len=max(len,1+dfs(u));
	
	dp[x]=len;
	return dp[x];
	
}
int main()
{
    int n,m,u,v;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
    	cin>>u>>v;
    	adj[u].push_back(v);
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    ans=max(ans, dfs(i));
    cout<<ans;
    
    




}

Measured memory_bytes: 8192
def print_list(A):
    print(" ".join(list(map(str, A))))

def main():
    N = int(input())
    A = list(map(int, input().split()))

    print_list(A)

    for i in range(1, N):
        store_v = A[i]
        
        j = i - 1
        while j >= 0 and A[j] > store_v:
            A[j + 1] = A[j]
            j -= 1

        A[j+1] = store_v

        print_list(A)

main()


Measured memory_bytes: 5600
#include <bits/stdc++.h>

using namespace std;
#define int long long

signed main(void)
{
    int a, b, c, n; cin >> n;

    vector<pair<int, int>> adj[n + 1];
    for(int i = 1; i < n; i++)
    {
        cin >> a >> b >> c;
        adj[a].push_back(make_pair(b, c));
        adj[b].push_back(make_pair(a, c));
    }

    int tc, k; cin >> tc >> k;

    queue<pair<int, int>> q;
    int vis[n + 1] = {};
    int dist[n + 1] = {};

    q.push(make_pair(k, 0));
    vis[k] = 1;

    while(!q.empty())
    {
        int t = q.front().first;
        int d = q.front().second;
        dist[t] = d;
        q.pop();

        for(auto u : adj[t])
        {
            if(!vis[u.first])
            {
                q.push(make_pair(u.first, d + u.second));
                vis[u.first] = 1;
            }
        }
    }

    while(tc--)
    {
        cin >> a >> b;
        cout << dist[a] + dist[b] << endl;
    }
}


Measured memory_bytes: 11504
#include<bits/stdc++.h>
using namespace std;
pair<int,int> pii;
vector<int> vec;
#define LL long long
int main()
{
  LL a,b,ans;

  cin>>a>>b;
  ans=(a+b)/2;
  if((a+b)%2)cout<<"IMPOSSIBLE"<<'\n';
  else   cout<<ans<<'\n';


  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1e18L + 5

int main(){
	int n, w;
	cin >> n >> w;
	vector<ll> value(n), weight(n);
	ll sum_value = 0;
	for(int i = 0; i < n; i++){
		cin >> weight[i] >> value[i];
		sum_value += value[i];
	}
	vector<ll> dp(sum_value + 1, INF);
	dp[0] = 0;
	for(int i = 0; i < n; i++){
		for(int va = sum_value - value[i]; va >= 0; va--){
			dp[va + value[i]] = min(dp[va + value[i]], dp[va] + weight[i]);                
		}
	}
	ll ans = 0;
	for(ll i = 0; i <= sum_value; ++i){
		if(dp[i] <= w){
			ans = max(ans, i);
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1152
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return true; } return false; }

int main() {
	
	int n; cin >> n;
	vector<int> a(n);
	int sum = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		sum += a[i];
	}
	int tmp = 2147483647;
	int ans;
	for (int i = n - 1; i >= 0; i--) {
		if (chmin(tmp, abs(n * a[i] - sum))) {
			ans = i;
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3636
def main():
    time = [0] * 5
    re = []
    for i in range(5):
        time[i] = int(input())
        if time[i] % 10 != 0:
            re.append(time[i] % 10)

    ans = sum(time)
    if len(re) > 1:
        re.sort()
        for r in re[1:]:
            ans += 10 - r % 10

    print(ans)


if __name__ == '__main__':
    main()

Measured memory_bytes: 3064
#include <iostream>
#include <vector>
#include <string>

#include <cstdlib>

using namespace std;


vector<pair<char,int> > B;

vector<pair<char,int> > SelectionSort(vector<pair<char,int> > v){
    for(int i=0;i<v.size();i++){
        int mini=i;
        for(int j=i;j<v.size();j++){
            if(v[j].second<v[mini].second){
                mini=j;
            }
        }
        pair<char,int> tmp=v[i];
        v[i]=v[mini];
        v[mini]=tmp;
     }
     for(int i=0;i<v.size()-1;i++){
        cout<<v[i].first<<v[i].second<<" ";
     }
     cout<<v[v.size()-1].first<<v[v.size()-1].second<<endl;
     return v;
}


vector<pair<char,int> > BubbleSort(vector<pair<char,int> > A){
    for(int i=0;i<A.size();i++){
        for(int j=A.size()-1;j>i;j--){
            if(A[j].second<A[j-1].second){
                pair<char,int> tmp=A[j];
                A[j]=A[j-1];
                A[j-1]=tmp;
            }
        }
    }
    
    for(int k=0;k<A.size()-1;k++){
         cout<<A[k].first<<A[k].second<<" ";
    }
    cout<<A[A.size()-1].first<<A[A.size()-1].second<<endl;
    return A;
}


int main(){
    int n;
   vector<pair<char,int> > B;
   cin>>n;
   for(int i=0;i<n;i++){
       string str;
      cin>>str;
      pair<char,int> tmp;
      tmp.first=str[0];
      tmp.second=str[1]-'0';
      B.push_back(tmp);
   }
  
   vector<pair<char,int> > C=BubbleSort(B);
   cout<<"Stable"<<endl;
   vector<pair<char,int> > D=SelectionSort(B);

   bool flag=true;
   for(int i=0;i<n;i++){
       if(C[i].first!=D[i].first||
               C[i].second!=D[i].second){
           flag=false;
       }
   }
   if(flag){
       cout<<"Stable"<<endl;
   }else{
       cout<<"Not stable"<<endl;
   }

   return 0;
}

Measured memory_bytes: 1212
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef priority_queue<ll> pq;
ll n, ml, mr, ans, a[100005], b[100005];
pq l, r;

ll upd (pq &A, pq &B, ll G) {
	ans += G;
	A.push(A.top() + mr - ml);
	ll V = B.top(); B.pop();
	B.push(V - G);
	B.push(V - G);
	return V - B.top();
}

int main()
{
	scanf("%lld",&n);
	for(ll i=1;i<=n;i++) {
		scanf("%lld%lld",&a[i],&b[i]);
	}

	l.push(0); r.push(0);
	ml = b[1]; mr = b[1];

	for(ll i=2;i<=n;i++) {
		ml -= b[i-1] - a[i-1];
		mr += b[i] - a[i];
		if(ml <= b[i] && b[i] <= mr) {
			ll GL = b[i] - ml, GR = mr - b[i];
			l.push(l.top()+GL);
			r.push(r.top()+GR);
			ml = b[i]; mr = b[i];
		}
		else if(b[i] < ml) {
			ll V = upd(r, l, ml - b[i]);
			mr = ml; ml = mr - V;
		}
		else {
			ll V = upd(l, r, b[i] - mr);
			ml = mr; mr = ml + V;
		}
	}
	printf("%lld\n",ans);
}


Measured memory_bytes: 3572
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<(n);++i)
using namespace std;
using ll = long long;

int main() {
	double x;
	cin >> x;
	int ans = 1;
	for (int i = 2; i * i <= x; i++) {
		for (int j = i * i; j <= x; j *= i) {
			ans = max(ans, j);
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 256
#include <stdio.h>

#define N 50

int main() {
	static char s[N + 1];
	int n, i, ans;

	scanf("%d%s", &n, s);
	ans = 0;
	for (i = 0; i + 2 < n; i++)
		if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C')
			ans++;
	printf("%d\n", ans);
	return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;

int bubbleSort(int A[],int N){
    int so = 0;
    bool gh = 1;
    for(int i = 0;gh;i++){
        gh = 0;
        for(int j = N-1;j >= i + 1;j--){
            if(A[j] < A[j - 1]){
                swap(A[j],A[j - 1]);
                gh = 1;
                so++;
            }
        }
    }
    return so;
} 

int main(){
    int A[100],N,so;
    cin >> N;
    for(int i = 0;i < N;i++)cin >> A[i];
    so = bubbleSort(A,N);
    for(int i = 0;i < N;i++){
        if(i)cout << " ";
        cout << A[i];
    }
    cout << endl;
    cout << so << endl;
}


Measured memory_bytes: 3112
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#define int long long
using namespace std;
signed main() {
	string s; cin >> s;
	stack<char>st;
	for (int i = 0; i < s.size(); i++) {
		if (st.size() == 0 || st.top() == s[i])st.push(s[i]);
		else st.pop();
	}
	cout << s.size() - st.size() << endl;
}

Measured memory_bytes: 512
#include <cstdio>
#include <cstdint>
#include <vector>

int main() {
  size_t N, K;
  scanf("%zu %zu", &N, &K);

  std::vector<size_t> a(N);
  for (auto& ai: a) scanf("%zu", &ai);

  std::vector<size_t> dp(K+1, 0);
  for (size_t i = 0; i < K; ++i)
    for (size_t j = 0; j < N; ++j) {
      if (i+a[j] > K) break;
      if (dp[i] % 2 == 0 || dp[i+a[j]] % 2 == 0)
        dp[i+a[j]] = dp[i]+1;
    }

  // for (size_t i = 0; i <= K; ++i)
  //   fprintf(stderr, "%zu%c", dp[i], i<K? ' ':'\n');

  puts((dp[K] % 2 > 0)? "First":"Second");
}


Measured memory_bytes: 1024
# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = input()

if len(set(S)) == 1:
    No()
else:
    Yes()


Measured memory_bytes: 3440
// A.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>

using namespace std;

int main(int argc, char* argv[]) {
	int n;
	cin >> n;
	vector<int> a(n);
	map<int, int> m;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		m[a[i]] += 1;
	}
	bool ans = false;
	switch (m.size()) {
	case 1:
		ans = m.begin()->first == 0;
		break;
	case 2:
		ans = m.begin()->first == 0 && m.begin()->second == n / 3;
		break;
	case 3:
	{
		ans = true;
		int x = 0;
		for (auto y : m) {
			x ^= y.first;
			if (y.second != n / 3) {
				ans = false;
			}
		}
		ans = ans && x == 0;
		break;
	}
	}
	cout << (ans ? "Yes" : "No") << endl;
	return 0;
}


Measured memory_bytes: 4224
#include <math.h>
#include <iostream>
#include<string>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
#include <stdlib.h>
using namespace std;

int main()
{
int N,M,c;
int ans=0;
bool check=true;
cin >> N >> M;
vector<int> k(M);
vector<vector<int>> s(M,vector<int>(N));
vector<int> p(M);
for(int i=0;i<M;i++)
{
cin >> k.at(i);
for(int j=0;j<k.at(i);j++)
{
cin >> s.at(i).at(j);
}
}
for(int i=0;i<M;i++)
{
cin >> p.at(i); 
}
for(int bit=0;bit<(1 << N);bit++)
{
check=true;
for(int i=0;i<M;i++)
{
c=0;
for(int j=0;j<k.at(i);j++)
{
if(bit & (1 << s.at(i).at(j)-1))c++;
}
if(c%2!=p.at(i)){check=false;break;}
}
if(check)ans++;
}
cout << ans << endl;
}

Measured memory_bytes: 256
def tonum(a):
    if a == 'A':
        return 10
    elif a == 'B':
        return 11
    elif a == 'C':
        return 12
    elif a == 'D':
        return 13
    elif a == 'E':
        return 14
    elif a == 'F':
        return 15
    else:
        return int(a)

a, b = input().split()
a = tonum(a)
b = tonum(b)
if a < b:
    print('<')
elif a > b:
    print('>')
else:
    print('=')

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < int(n); i++)
#define rep1(i,n) for (int i = 1; i <= int(n); i++)
#define repR(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep1R(i,n) for (int i = int(n); i >= 1; i--)
#define all(x) (x).begin(), (x).end()
#define show(x) #x " = " << x
#define mp make_pair
#define quit(x) { cout << (x) << endl; return 0; }
template <class T> bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; }
template <class T> bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; }

template <class T> typename conditional<true, ostream&, decltype(declval<T>().begin())>::type operator<<(ostream& os, T const& v) { for (auto const& x : v) os << x << " "; return os; }
template <class T> typename conditional<true, istream&, decltype(declval<T>().begin())>::type operator>>(istream& is, T& v) { for (auto& x : v) is >> x; return is; }
template <class T, class S> ostream& operator<<(ostream& os, pair<T,S> const& p) { return os << "(" << p.fst << ", " << p.snd << ")"; }
template <class T, class S> istream& operator>>(istream& is, pair<T,S> const& p) { return is >> p.fst >> p.snd; }

int main() {
    cin.tie(nullptr); ios::sync_with_stdio(false);
    
    int n; cin >> n;
    int N = n*(n-1)/2;
    vector<vector<int> > g(N);
    vector<int> cnt(N);
    map<pair<int,int>, int> m;
    int c = 0;
    rep (i,n) rep (j,n) if (i < j) m[mp(i,j)] = c++;
    rep (i,n) rep (j,n) if (i > j) m[mp(i,j)] = m[mp(j,i)];

    rep (i,n) {
        vector<int> a(n-1);
        rep (j,n-1) cin >> a[j], a[j]--;
        rep (j,n-2) {
            int x = m[mp(i,a[j])];
            int y = m[mp(i,a[j+1])];
            g[x].push_back(y);
            cnt[y]++;
        }
    }

    deque<int> q;
    rep (i,N) if (cnt[i] == 0) q.push_back(i);
    vector<int> a;
    while (!q.empty()) {
        int x = q.front(); q.pop_front();
        a.push_back(x);
        for (int y : g[x]) if (--cnt[y] == 0) q.push_back(y);
    }

    if (a.size() != N) quit(-1);

    vector<int> d(N,1);
    rep (i,N) {
        int x = a[i];
        for (int y : g[x]) chmax(d[y],d[x]+1);
    }

    rep (i,N) for (int j : g[i]) if (d[i] >= d[j]) quit(-1);
    cout << *max_element(all(d)) << endl;
}


Measured memory_bytes: 96076
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll n, x, d;
    cin >> n >> x >> d;
    if (d == 0) {
        if (x == 0) {
            cout << 1 << endl;
            return 0;
        }
        cout << n + 1 << endl;
        return 0;
    }
    if (d < 0) {
        d *= -1;
        x *= -1;
    }
    ll ans = 0;
    vector<pair<ll, pair<ll, ll>>> v;
    for (ll k = 0; k <= n; k++) {
        ll a = k * x + k * (k - 1) / 2 * d;
        ll b = k * x + k * (2 * n - k - 1) / 2 * d;
        v.emplace_back((k * x % d + d) % d, make_pair(a, b));
    }
    sort(v.begin(), v.end());
    ll t = v[0].first;
    ll l = v[0].second.first, r = v[0].second.second;
    ans += (r - l) / d + 1;
    rep(i, 1, n + 1) {
        if (t == v[i].first) {
            if (v[i].second.first > r) {
                l = v[i].second.first;
                r = v[i].second.second;
                ans += (r - l) / d + 1;
            } else if (v[i].second.second > r) {
                ans += (v[i].second.second - r) / d;
                r = v[i].second.second;
            }
        } else {
            t = v[i].first;
            l = v[i].second.first;
            r = v[i].second.second;
            ans += (r - l) / d + 1;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 8432
#include<iostream>
#include<algorithm>

using namespace std;

#define RMAX 100

int main(){

	int n,S;
	int r[RMAX+1];
	int in;
	int ans;

	while(1){

		cin>>n>>S;
		if(n==0&&S==0) break;

		for(int i=1;i<=100;i++){
			r[i]=0;
		}

		for(int i=0;i<n;i++){
			cin>>in;
			r[in]++;
		}

		ans=0;
		for(int i=1;i<=100;i++){
			for(int j=i+1;j<=100;j++){
				if(i+j<=S) continue;
				ans+=r[i]*r[j];
			}
			if(i*2>S) ans+=r[i]*(r[i]-1)/2;
		}

		cout<<ans<<endl;
	}


	return 0;
}

Measured memory_bytes: 1164
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
 
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Measured memory_bytes: 7700
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int N = int.Parse(input[0]);
            int K = int.Parse(input[1]);
            int[] rod = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            List<int> sort_rod = new List<int>();

            sort_rod.AddRange(rod);
            sort_rod.Sort((a, b) => b - a);

            int ans = 0;

            for(int i = 0; i < K; i++)
            {
                ans += sort_rod[i];
            }

            Console.WriteLine(ans);
        }
    }
}


Measured memory_bytes: 13268
#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int,int>
#define mod 998244353

int lowbit(int x) {return x&(-x);}

int n;
ll C;
ll f[200010];
ll h[200010]; 
int q[200010];

double getk(int k,int j) {
	return (double)(f[k]+h[k]*h[k]-f[j]-h[j]*h[j])/(double)(2*h[k]-2*h[j]);
}

int main(){
	cin>>n>>C;
	for (int i=1;i<=n;i++) cin>>h[i];
	f[1]=0;
	q[0]=1;
	int l=0,r=0;
	for (int i=2;i<=n;i++) {
		while (l<r&&getk(q[l],q[l+1])<h[i]) l++;
		int cur=q[l];
		f[i]=f[cur]+(ll)(h[i]-h[cur])*(ll)(h[i]-h[cur])+C;
		while (l<r&&getk(q[r-1],q[r])>getk(q[r],i)) r--;
		q[++r]=i;
//		cout<<i<<" "<<f[i]<<endl;
	}
	cout<<f[n];
	return 0;
}


Measured memory_bytes: 4096
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int N; cin >> N;
    vector<int> s(N);
    int m = 1000;
    int total = 0;
    rep(i, N){
        cin >> s[i];
        if(s[i] % 10 != 0){
            m = min(m, s[i]);
        }
        total += s[i];
    }

    if(total % 10 != 0){
        cout << total << endl;
    } else {
        if(m == 1000){
            cout << 0 << endl;
        } else {
            cout << total - m << endl;
        }
    }


    return 0;
}

Measured memory_bytes: 3608
#include <stdio.h>

int main(){
  int i,j,S[10000],T[500],n,q,C=0;
  
  scanf("%d",&n);
  for(j=0;j<n;j++){
    scanf("%d",&S[j]);
  }
  
  scanf("%d",&q);
  for(j=0;j<q;j++){
    scanf("%d",&T[j]);
  }

  for(j=0;j<q;j++){
    i = 0;
    S[n] =  T[j];
    while(T[j] != S[i]){
      i++;
    }
    if(i < n) C++;
  }

  printf("%d\n",C);

  return 0;
}  



Measured memory_bytes: 2104
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    # https://atcoder.jp/contests/abc070/tasks/abc070_d
    # https://atcoder.jp/contests/abc070/submissions/12486326

    import sys
    from collections import deque

    def dijkstra(V, adj, root):
        que = deque()
        que.append(root)
        seen = [-1] * (N + 1)
        seen[root] = 0
        par = [0] * (N + 1)
        child = [[] for _ in range(N + 1)]
        seq = []
        while que:
            v = que.popleft()
            seq.append(v)
            for u, c in adj[v]:
                if seen[u] == -1:
                    seen[u] = seen[v] + c
                    par[u] = v
                    child[v].append(u)
                    que.append(u)
        seq.reverse()

        return seen

    N = int(input())
    V, E = N, N - 1  # 頂点の数、辺の数

    adj = [[] for _ in range(N + 1)]

    for _ in range(N - 1):
        a, b, c = map(int, input().split())  # 頂点aからbにコストc
        adj[a].append((b, c))
        adj[b].append((a, c))
    Q, root = map(int, input().split())  # rootはスタート地点

    d = dijkstra(V, adj, root)
    for _ in range(Q):
        x, y = map(int, input().split())
        print(d[x] + d[y])


resolve()

Measured memory_bytes: 79488
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int X = sc.nextInt();
		int Y = sc.nextInt();
		SortedSet<Integer> setX = new TreeSet<Integer>();
		SortedSet<Integer> setY = new TreeSet<Integer>();

		for (int i = 0; i < N; i++) {
			setX.add(sc.nextInt());
		}

		for (int i = 0; i < M; i++) {
			setY.add(sc.nextInt());
		}

		System.out.println(result(X, Y, setX, setY));

		sc.close();
	}

	private static String result(int X, int Y, SortedSet<Integer> setX,
			SortedSet<Integer> setY) {
		int maxX = setX.last();
		maxX = Math.max(X, maxX);

		int minY = setY.first();
		minY = Math.min(Y, minY);

		if (maxX + 1 <= minY) {
			return "No War";
		} else {
			return "War";
		}
	}
}

Measured memory_bytes: 23764
#include <bits/stdc++.h>

using namespace std;

#define MAX 21
typedef vector<int> Vec;
typedef vector<Vec> Mat;

int H, W;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int memo[MAX][MAX][MAX][MAX];

int grundy(int x1, int y1, int x2, int y2, Mat &f)
{
    if (x1 > x2 || y1 > y2) {
        return 0;
    }

    if (memo[y1][x1][y2][x2] != -1) {
        return memo[y1][x1][y2][x2];
    }
        
    unordered_set<int> st;
    for (int i = y1; i <= y2; i++) {
        for (int j = x1; j <= x2; j++) {
            if (f[i][j] == 0) {
                st.insert(grundy(x1, y1, j-1, i-1, f) ^
                          grundy(j+1, y1, x2, i-1, f) ^
                          grundy(x1, i+1, j-1, y2, f) ^
                          grundy(j+1, i+1, x2, y2, f));
            }
        }
    }   
    
    int res = 0;
    while (st.count(res)) res++;
    return (memo[y1][x1][y2][x2] = res);
}

int main()
{    
    cin >> H >> W;
    Mat f(H);
    for (int i = 0; i < H; i++) {
        f[i].resize(W);
        for (int j = 0; j < W; j++) {
            char ch;
            cin >> ch;
            f[i][j] = (ch == 'X');
        }
    }
    memset(memo, -1, sizeof(memo));
    cout << (grundy(0, 0, W-1, H-1, f) != 0 ? "First" : "Second") << endl;
    return 0;
}

Measured memory_bytes: 3864
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
const double PI = 3.14159265358979323846;
int main() {
  ll n, k;
  cin >> n >> k;
  ll x, y;
  x = n % k;
  y = k - (n % k);
  if (k == 1)
    cout << '0';
  else {
    if (x <= y) cout << x;
    if (x > y) cout << y;
  }
  return 0;
}

// g++ lo.cpp
// ./a.exe
// g++ -o つくりたいふぁいるめい　もとのふぁいる.cpp
/*
for (int i = 0; i < n - 2; i++) {
  for (int j = i + 1; j < n - 1; j++) {
    int k = 2 * j - i;*/
// vector<int>a(n);
// cin>>a.at(n);

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstdio>
using namespace std;

#define int long long
#define INF 1e18

signed main() {
    int x, y; cin >> x >> y;
    vector<string> s(x); for(int i = 0; i < x; i++) cin >> s[i];
    int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
    int dy[] = {1, -1, 0, 1, -1, 0, 1, -1};

    for(int i = 0; i < x; i++){
        for(int j = 0; j < y; j++) {
            if(s[i][j] == '.') {
                int cnt = 0;
                for(int k = 0; k < 8; k++) {
                    int tmpx = i + dx[k];
                    int tmpy = j + dy[k];
                    if(tmpx < 0 | tmpx >= x | tmpy < 0 | tmpy >= y) {
                        continue;
                    }
                    if(s[tmpx][tmpy] == '#') {
                        cnt++;
                    }
                }
                s[i][j] = '0' + cnt;
            }
        }
    }

    for(int i = 0; i < x; i++) {
        cout << s[i] << endl;
    }
}


Measured memory_bytes: 256
n, g = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]

ans = float('inf')
for i in range(2 ** n):
    g_sum = 0
    count = 0
    rest_max = -1
    for j in range(n):
        if (i >> j) & 1:
            g_sum += 100 * (j + 1) * a[j][0] + a[j][1]
            count += a[j][0]
        else:
            rest_max = j

    if g_sum < g:
        s1 = 100 * (rest_max + 1)
        need = (g - g_sum + s1 - 1) // s1
        if need >= a[rest_max][0]:
            continue
        count += need
    ans = min(ans, count)

print(ans)

Measured memory_bytes: 41712
import numpy as np

N,M = list(map(int, input().split()))

ks = []
switch_list = [[0 for j in range(10)] for i in range(M)]
for i in range(M):
    ks.append(list(map(int, input().split())))
    for j in range(ks[i][0]):
        switch_list[i][10-ks[i][j+1]] = 1
p = list(map(int, input().split()))

ans = 0
for i in range(2**N):
    flg = True
    binary = format(i, 'b').zfill(10)
    binary = list(map(int, binary))
    for j in range(M):
        on = np.logical_and(switch_list[j], binary)
        total_on = sum(on)
        if total_on%2 != p[j]:
            flg = False
            break
    if flg:
        ans += 1
print(ans)

Measured memory_bytes: 14464
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
#define REP(i,n) for (int i = 0; i <(n); ++i)
//debug 
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }


int main(){
  //in
  int n, s;
  cin >> n >> s;
  vector<int> x(n);
  REP(i,n) cin >> x[i];
  x.push_back(s);
  // REP(i,n) {
  //   int xx;
  //   cin >> xx;
  //   //if(xx == s) continue; //xx != s が条件にあるからいらない
  //   x.push_back(xx);
  // }


  debug(x);
  sort(ALL(x));

  vector<int> len(x.size() - 1);
  
  REP(i,len.size()){
    len[i] = abs(x[i+1]- x[i]);
  }

  debug(len);
  //x座標と同じ値がなければ配列に追加
  int g = len[0];
  REP(i,len.size()){
    g = __gcd(g,len[i]);
  }

  cout << g << endl;

  return 0;
}


Measured memory_bytes: 3992
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> a = {1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51
    };
    int K ;
    cin >> K ;
    cout << a.at(K-1) << endl;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
int main(){
	int h,w,k;
	cin >> h >> w >> k;
	ll l[w+2]={},c[w+1]={},r[w+1]={};
	if(w==1){cout << 1 << endl;return 0;}
	for(int i=0;i<(1<<(w-1));i++){
		bool flag=true;
		for(int j=0;j<w-2;j++){
			if((i>>j)&(i>>(j+1))&1) flag=false;
		}
		if(!flag) continue;

		for(int j=0;j<w-1;j++){
			if((i>>j)&1){
				l[j+2]++;
				r[j+1]++;
			}else if(j==0){
				c[j+1]++;
			}else{
				if(((i>>(j-1))&1)==0) c[j+1]++;
				if(j==w-2) c[j+2]++;
			}
		}
	}
	ll dp[h+1][w+2]={};
	dp[0][1]=1;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			dp[i][j]=dp[i-1][j-1]*r[j-1]%mod+dp[i-1][j]*c[j]%mod+dp[i-1][j+1]*l[j+1]%mod;
			dp[i][j]%=mod;
		}
	}
	cout << dp[h][k] << endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;

int main(){
          int h, w;
          int n;
          int i,j,k;
          while(1){
                    int ans=0;
                    int x=0, y=0; //座標
                    cin >> h >> w;
                    if(!h&&!w) break;
                    string keybord[55];
                    string s;
                    for(i=0;i<h;i++) cin >> keybord[i];
                    cin >> s;

                    for(i=0;i<s.length();i++){
                              for(j=0;j<h;j++){
                                     n=keybord[j].find(s[i]);
                                     if(n>=0){
                                        ans+=abs(x-n)+abs(y-j)+1;
                                        x=n;
                                        y=j;
                                        break;
                                     }
                              }   
                    } 
                    cout << ans << endl;
                       
          }
}


Measured memory_bytes: 3228
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // double
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using in6 = int64_t;
using ind = double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  ios::sync_with_stdio(false);
  double a;
  cin >> a;
  double pie = 3.141592653589;
  cout << fixed << setprecision(10);
  double ans1 = a * a * pie;
  double ans2 = 2 * a * pie;
  printf("%lf %lf\n", ans1, ans2);
}


Measured memory_bytes: 3312
#include<bits/stdc++.h>

using namespace std;

#define FastIO ios_base :: sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define read(in) freopen("Input.txt","r",stdin)
#define write(out) freopen("Ouput.txt","w",stdout)
#define pf printf
#define sf scanf
#define fr(i,n) for(i=1 ; i<=n ; i++)
#define fr1(i,x,n) for(i=x ; i<=n ; i++)
#define sf1(val) scanf("%lld",&val)
#define sf2(val1,val2) scanf("%lld %lld",&val1,&val2)
#define INF 999999999999999999
#define Pi 2*acos(0.0)
#define len 200000

typedef long long int lli;

lli a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,sum=0,total=0,res=0,ans=0;

//lli ar[len], dp[len];

/*lli frog1_dp(lli t)
{
    if(t<=0)    return 0;

    if(t==1)    return abs(ar[t]-ar[t-1]);

    if(dp[t]==-1)
    {
        vector<lli>br;
        for(lli j=1 ; j<=k ; j++)
        {
           br.push_back(abs(ar[t]-ar[t-j]) + frog1_dp(t-j));
        }
        sort(br.begin(),br.end());

        dp[t]=br[0];
    }
    return dp[t];
}*/

int main()
{
    //read(in);

    sf2(t,k);

    lli ar[t]={0};

    vector<lli>dp(t,INF);

    fr(i,t)    cin>>ar[i-1];

    n=t-1;

    dp[0]=0;

    fr(i,n)
    {
        fr(j,k)
        {
            if(i-j>=0)  dp[i]=min( dp[i] , dp[i-j] + abs( ar[i]-ar[i-j] ) );
        }
    }

    pf("%lld\n",dp[n]);
}

Measured memory_bytes: 1792
#include <iostream>
#include <fstream>
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string> 
#include <vector>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>

using namespace std;
using ll = long long;

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i, m, n) for(int i = int(m);i < int(n);i++)
#define ALL(obj) (obj).begin(),(obj).end()

const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

vector<int> dy = { 0,0,1,-1 };
vector<int> dx = { 1,-1,0,0 };

template<class T> void corner(bool flg, T hoge) {
	if (flg) {
		cout << hoge << endl;
		exit(0);
	}
	else return;
}

template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {
	o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;
}

template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {
	o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;
}

template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {
	o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;
}

template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {
	o << "{" << obj.first << ", " << obj.second << "}"; return o;
}

template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {
	o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;
}

void print(void) {
	cout << endl;
}

template <class Head> void print(Head&& head) {
	cout << head;
	print();
}

template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {
	cout << head << " ";
	print(forward<Tail>(tail)...);
}

void YN(bool flg) {
	cout << ((flg) ? "YES" : "NO") << endl;
}

void Yn(bool flg) {
	cout << ((flg) ? "Yes" : "No") << endl;
}

void yn(bool flg) {
	cout << ((flg) ? "yes" : "no") << endl;
}

int main() {
	int K, T; cin >> K >> T;
	V<int> a(T);
	REP(i, T) cin >> a[i];
	sort(ALL(a));
	int U = a[T - 1];
	int ans;
	if (U <= K / 2) ans = 0;
	else ans = U - 1 - (K - U);
	cout << ans << endl;
	
	return 0;
}



Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int N;
  bool info[100][100] = {{}};
  cin >> N;
  for(int i = 0; i < N; i++){
    int u, k;
    cin >> u >> k;
    while(k--){
      int v;
      cin >> v;
      info[u - 1][v - 1] = true;
    }
  }
  for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      cout << info[i][j] << (j + 1 == N ? '\n' : ' ');
    }
  }
}


Measured memory_bytes: 3108
#include <iostream>
#include <bits/stdc++.h>

using namespace std;
int n,h[100100],Ans = 0;

int main()
{
    cin >> n;
    for(int i = 0;i<n;i++)  cin >> h[i];
    int c = 0;
    for(int i = 0;i<n;i++){
        while(i + 1 < n && h[i] >= h[i + 1])  i++,c++;
        Ans = max(Ans , c);
        c = 0;
    }
    cout << Ans;
    return 0;
}


Measured memory_bytes: 640
A=[list(map(int,input().split())) for i in range(3)]
n=int(input())
for k in range(n):
  s=int(input())
  for i in range(3):
    for j in range(3):
      if s == A[i][j]:
        A[i][j]=0   
if A[0][0]==0 and A[1][0]==0 and A[2][0]==0:
  print("Yes")
   
elif A[0][1]==0 and A[1][1]==0 and A[2][1]==0:
  print("Yes")
elif A[0][2]==0 and A[1][2]==0 and A[2][2]==0:
  print("Yes")
elif [0,0,0] in A:
  print("Yes")
  
elif A[0][2]==0 and A[1][1]==0 and A[2][0]==0:
  print("Yes")
  
elif A[0][0]==0 and A[1][1]==0 and A[2][2]==0:
  print("Yes")
  
else:
  print("No")

Measured memory_bytes: 3064
def main():
    n = int(input())
    a = [c == 'E' for c in input()]

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *acc_e, = accumulate(a)

    ans = 1 << 30
    for i in range(n):
        ans = min(ans, (i - acc_e[i]) + (acc_e[n] - acc_e[i + 1]))

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 17616
#include <iostream>
#include <algorithm>
#include <vector>

#define MAX_N 100
#define MAX_T 1000

int c[MAX_N], t[MAX_N];

std::vector<std::pair<int, int>> ct;

int main() {
    int N, T;
    std::cin >> N >> T;

    for (int i=0; i<N; i++) {
        // std::cin >> c[i] >> t[i];
        int a, b;
        std::cin >> a >> b;
        if (b <= T) ct.push_back(std::make_pair(a, b));
    }


    // std::sort(c, c+N);
    std::sort(ct.begin(), ct.end());
    if (ct.empty() || ct[0].second > T) {
        std::cout << "TLE" << std::endl;
    } else {
        std::cout << ct[0].first << std::endl;
    }


    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

int main(void){
	int N;
	cin>>N;
	vector<int> h(N);
	for(int i=0;i<N;i++)cin >> h[i];
	
	//高さが全部0になるまでやる
	int res = 0;
	while(true){
		//最高高さが0だったらおしまい
		if(*max_element(h.begin(),h.end()) == 0)break;
		int i=0;
		while(i<N){
			if(h[i]==0){
				i++;
			}else{
				++res;
				while(i<N&&h[i]>0){
					--h[i];
					++i;
				}
			}
		}
	}
	cout << res << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long

int x, sol = INT_MAX;
void solve(vector<vector<int>>& v, int n, int m, vector<int>&ans, int amt) {
  if(n == 0) {
    for(int i = 0; i < m; i++)
      if(ans[i] < x)
        return;
    sol = min(sol, amt);
    return;
  }
  
  solve(v, n-1, m, ans, amt);
  for(int i = 1; i <= m; i++)  ans[i-1] += v[n-1][i];
  amt += v[n-1][0];
  solve(v, n-1, m, ans, amt);
  for(int i = 1; i <= m; i++)  ans[i-1] -= v[n-1][i];
  amt -= v[n-1][0];
}

signed main() {
  int n, m, i, val, j, amt = 0;
  cin>>n>>m>>x;
  vector<int>v1, ans(m, 0);
  vector<vector<int> >v(n, v1);
  for(i = 0; i < n; i++) {
    for(j = 0; j <= m; j++) {
      cin>>val;
      v[i].push_back(val);
    }
  }
  
  solve(v, n, m, ans, amt);
  if(sol == INT_MAX)
    cout<<"-1\n";
  else
    cout<<sol<<"\n";
  return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
using pii = pair<int,int>;
#define ALL(x) (x).begin(),(x).end()

int main(){
	ll N, K;
	cin >> N >> K;
	ll l = N % K, r = -(N % K - K);
	cout << ((l < r) ? l : r) << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  int ans = 0;
  rep(is, 1 << h) rep(js, 1 << w) {
    int count = 0;
    rep(i, h) rep(j, w) {
      if (is >> i & 1) continue;
      if (js >> j & 1) continue;
      if (v[i][j] == '#') count++;
    }
    if (count == k) ans++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3608
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string.h>

using namespace std;
typedef long long ll;

int main()
{
	int N, K;
	int H[200000];
	
	scanf("%d %d", &N, &K);
	for(int i = 0; i < N; i++){
		scanf("%d", &H[i]);
	}
	
	sort(H, H+N);
	
	ll count = 0;
	for(int i = 0; i < N; i++){
		if(i < N-K){
			count += H[i];
		}
		else{
			
		}
	}
	
	printf("%lld\n", count);
	
	return 0;
}

Measured memory_bytes: 896
#include<bits/stdc++.h>
using namespace std;
char x[2000];
int main()
{
	cin>>x;
	int l=strlen(x);
	for(int i=0;i<l;i++)
	{
		if(x[i]=='C')
		{
			for(int j=i;j<l;j++)
			{
				if(x[j]=='F')
				{
					cout<<"Yes"<<endl;
					return 0;
				}
			}
		}
	}
	cout<<"No"<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n, a[100008];

int main(){
	cin >> n;
	
	for(ll i = 1; i <= n; i++){cin >> a[i];}
	sort(a+1, a+n+1);
	
	ll max1 = 0;
	ll max2 = 0;
	for(ll i = n; i >= 2; i--){
		if(max1 != 0 && max2 == 0 && a[i] == a[i-1]){
			max2 = a[i];
			break;
		}
		if(max1 == 0 && a[i] == a[i-1]){
			max1 = a[i];
			i--;
		}
	}
	
	ll ans = max1*max2;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1024
s = [input() for _ in range(3)]
c = [len(i) for i in s]
turn = lambda x: "ABC".index(x)
t = "A"
while True:
  bt = t
  if c[turn(bt)] == 0:
    print(bt)
    break
  t = s[turn(bt)][0].upper()
  s[turn(bt)] = s[turn(bt)][1:]
  c[turn(bt)] -= 1

Measured memory_bytes: 3064
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	long long n, m;
	scanf("%lld%lld", &n, &m);
	if (n == 1 || m == 1) printf("1");
	else printf("%lld", (n * m + 1) / 2);
	return 0;
}

Measured memory_bytes: 128
#include<stdio.h>
#define MAX 1000001
int head=0,tail=0,n;


typedef struct{
  char name[100];
  int t;
}POINT;


void enqueue(POINT);
POINT dequeue();
POINT p[MAX];
int main(){
  int i,q,sum=0;
  POINT a;
  scanf("%d%d",&n,&q);
  for(i=0;i<n;i++)scanf("%s%d",p[i].name,&p[i].t);
  //printf("\n");
  head = 0;
  tail = n;
  while(head!=tail){
	  a = dequeue();
	  if(a.t<=q){
		  sum+=a.t;
		  printf("%s %d\n",a.name,sum);
	  }
		else {
	  sum+=q;
	  a.t-=q;
	  enqueue(a);
  }
  }
  return 0;
}

void enqueue(POINT x){
  p[tail++]=x;
}

POINT dequeue(){
	POINT x = p[head++];
	return x;
  	}





Measured memory_bytes: 21928