akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif

  int n;
  scanf("%d", &n);
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; ++i)
    a[i] += a[i - 1];
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  for (int len = 2; len <= n; ++len)
    for (int l = 1, r = len; r <= n; ++l, ++r) {
      dp[l][r] = numeric_limits<ll>::max();
      for (int k = l; k < r; ++k)
        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
      dp[l][r] += a[r] - a[l - 1];
    }
  printf("%lld\n", dp[1][n]);

#ifdef LBT
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


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

int i, j, n, ans;
int flag[1000001];

int main()
{
    scanf("%d", &n);
    int a[n];
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a+n);
    for (i = 0; i < n; i++) {
        for (j = a[i]; j <= a[n-1]; j += a[i]) {
            flag[j]++;
        }
    }
    for (i = 0; i < n; i++) {
        if (flag[a[i]] == 1) ans++;
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 8360
use std::cmp::{self, Ordering};
use std::collections::*;
use std::io::{self, Read};

const MOD: usize = 1000000007;

fn main() {
    let stdin = io::stdin();
    let mut sc = Scanner::new(stdin.lock());

    let x: usize = sc.read().unwrap();
    let a: usize = sc.read().unwrap();

    if x < a {
        println!("0");
    } else {
        println!("10");
    }
}

struct Scanner<R: Read> {
    reader: R,
}

impl<R: Read> Scanner<R> {
    fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader }
    }

    fn read<T: std::str::FromStr>(&mut self) -> Option<T> {
        self.reader
            .by_ref()
            .bytes()
            .map(|b| b.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>()
            .parse()
            .ok()
    }

    fn read_vec<T: std::str::FromStr>(&mut self, n: usize) -> Option<Vec<T>> {
        (0..n).map(|_| self.read()).collect()
    }

    fn read_vec2d<T: std::str::FromStr>(&mut self, m: usize, n: usize) -> Option<Vec<Vec<T>>> {
        (0..m).map(|_| self.read_vec(n)).collect()
    }

    fn read_board(&mut self, n: usize) -> Option<Vec<Vec<char>>> {
        (0..n)
            .map(|_| {
                self.read::<String>()
                    .map(|s| s.chars().collect::<Vec<char>>())
            })
            .collect()
    }
}


Measured memory_bytes: 4352
/*
	Name:
	Author: xiaruize
	Date:
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define form(i,j,n) for(int i=int(j);i<=int(n);i++)
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define pis pair<int,string>
#define sec second
#define ll long long
#define fir first
#define sz(a) int((a).size())

signed main()
{
 	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int a,b,c;
	cin>>a>>b>>c;
	int k;
	cin>>k;
	while(b<=a)
	{
		b*=2;
		k--;
		if(k==0)
		{
			cout<<"No"<<endl;
			return 0;
		}
	}
	while(k--)
	{
		c*=2;
	}
	if(c>b&&b>a)
	{
		cout<<"Yes"<<endl;
	}
	else
	{
		cout<<"No"<<endl;
	}
	return 0;
}


Measured memory_bytes: 3652
#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 1 << 30;

int f( int x, int y){
  return x * x * x + y * y * y;
}

int main(){
  int z;
  while(cin >> z, z){
    int ret = INF;
    int a, b, zzz = z * z * z;
    for(int x = 1; x < z; x++){
      a = 1, b = 1110;
      while(a + 1 < b){
        int mid = a + b >> 1;
        if( f(x,mid) <= zzz ) a = mid;
        else b = mid;
      }
      ret = min( ret, zzz - f( x, a));
    }
    cout << ret << endl;
  }
}

Measured memory_bytes: 1160
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip> 
#include <bits/stdc++.h>
using namespace std;
double pi=3.14159265358979323846264338;


bool issqt(long long k){
	if(k<=3)return true;
	long long s=(long long)sqrt(k);
	//cout<<k<<" "<<s<<endl;
	for(long long i=2;i<=s;i++)
		if(k%i==0)return false;
	return true;
}
 
 
int main() {
	int x,y;
	cin>>x>>y;
	if(y<=4*x&&y>=2*x&&y%2==0){
		cout<<"Yes"<<endl;
	}
	else cout<<"No"<<endl;
  return 0;
}




Measured memory_bytes: 3676
#include<bits/stdc++.h>
using namespace std;
vector<int> edges[200005];
int visited[200005];
void dfs(int s,int count)
{
    visited[s]=count;
    for(int x: edges[s])
    {
        if(visited[x]==0)
        {
            dfs(x,count);
        }
            
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    while(m--)
    {
        int x,y;
        cin>>x>>y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    int count=0;
    for(int i=1;i<=n;i++)
    {
        if(visited[i]==0)
        {
            count++;
            dfs(i,count);
        }
    }
    map<int,int> d;
    for(int i=1;i<=n;i++)
    {  d[visited[i]]++;
      //  cout<<visited[i]<<" ";
    }
    //cout<<endl;
    int Max=0;
    for(auto x: d)
    {
        Max=max(Max,x.second);
    }
    cout<<Max<<endl;
  //  cout<<count<<endl;
}

Measured memory_bytes: 24508
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int N, A, B, C, ans = 0;
	cin >> N >> A >> B >> C;
	vector<int> D(N);
	for (int& i : D) cin >> i;
	sort(D.begin(), D.end(), greater<int>());
	for (int i = 1; i < N; ++i) D[i] += D[i - 1];
	for (int i = 0; i < N; ++i) ans = max((C + D[i]) / ((1 + i) * B + A), ans);
	cout << ans << endl;
}


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

typedef long long ll;
typedef pair<int,int> P;

const int INF = 1e9;
const int mod = 1e9+7;

int main()
{
    int n;
    ll k; 
    cin >> n >> k;
    vector<P> num(n);
    for(int i = 0; i < n; i++){
        int a,b;
        cin >> a >> b;
        num[i].first = a;
        num[i].second = b;
    }

    sort(num.begin(),num.end());

    for(int i = 0; i < n; i++){
        if(num[i].second >= k){
            cout << num[i].first << endl;
            return 0;
        }
        k -= num[i].second;
    }
}

Measured memory_bytes: 1024
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N,M = map(int,input().split())
A = list(map(int,input().split()))

# 各数字に必要なマッチの本数
num = [0,2,5,5,4,5,6,3,7,6]

# dp : i本のマッチで作ることが可能な最大の桁数
dp = [-float("inf")]*(N+1)
# dp初期化 
#   - 0本 -> 0桁
#   - 各数字を1つ作る場合は1桁
dp[0] = 0
for a in A:
    if num[a] <= N:
        dp[num[a]] = 1

# dp更新
for i in range(1,N+1):
    for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元
A.sort(reverse=True)
ans = ""
for i in range(dp[N]):
    for a in A:
        if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break

print(ans)

Measured memory_bytes: 3348
import java.util.Scanner;                      

class Main {                                  
public static void main(String[] args) {   
Scanner scanner = new Scanner(System.in);      
 
		
		
		while(true){
			
			String x = scanner.next();
			
			if(x.equals("0")){
				break;//終了条件
			}

			char[] a; 
			a=new char[x.length()];//配列の最大値は桁数＝文字数
			int sum=0;
			for(int i=0;i < x.length();i++){//桁数分くりかえす
				
				a[i]=x.charAt(i);
				
			    sum=sum+Character.getNumericValue(a[i]);//charの数字を対応するint型に変換し加算
			    
			}

			System.out.println(sum);
		
			}

		scanner.close();

	}


	}



Measured memory_bytes: 26340
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<deque>
using namespace std;

using ll = long long;
const int INF = 1001001001;

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

int main()
{
  int n;
  cin >> n;
  string s;
  cin >> s;

  ll r=0, g=0, b=0;
  rep(i,n) {
    if (s[i] == 'R') {
      r++;
    } else if (s[i] == 'G') {
      g++;
    } else {
      b++;
    }
  }

  ll count = r*g*b;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int k = 2*j - i;
      if (k >= n || k <= j) continue;
      if (s[i] != s[j] && s[j] != s[k] && s[i] != s[k]) {
        count--;
      }
    }
  }

  cout << count << endl;
}


Measured memory_bytes: 3604
import java.util.*;
class Main{
    int dx[]={-1,0,1,0};
    int dy[]={0,-1,0,+1};//x,y,sys
    void run(){
	Scanner sc=new Scanner(System.in);
	while(true){
	    int N=sc.nextInt();
	    if(N==0)break;
	    P.init();
	    P[] p=new P[N];
	    p[0]=new P(0,0);
	    for(int i=1;i<N;i++){
		int n=sc.nextInt();
		int d=sc.nextInt();
		p[i]=new P(p[n].x+dx[d],p[n].y+dy[d]);
		
	    }
	    System.out.println(P.getX()+" "+P.getY());
	}
    }
    public static void main(String[] args){
	new Main().run();
    }
}

class P{
    static int maxX,maxY,minX,minY;
    int x,y;
    P(int x,int y){
	this.x=x;this.y=y;
	maxX=Math.max(maxX,x);
	maxY=Math.max(maxY,y);
	minX=Math.min(minX,x);
	minY=Math.min(minY,y);
    }
    static void init(){
	maxX=0;maxY=0;minX=0;minY=0;
    }
    static int getX(){
	return (maxX-minX+1);
    }
    static int getY(){
	return (maxY-minY+1);
    }
}

Measured memory_bytes: 12000
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, z, w;
	cin >> n >> z >> w;
	vector<int> a(n);
	rep(i,n) cin >> a[i];
	int ans = abs(a[n-1] - w);
	if(n >= 2){
		int cmp = abs(a[n-1] - a[n-2]);
		chmax(ans, cmp);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
n,h=map(int,input().split())
a=[]
b=[]
for i in range(n):
    x,y=map(int,input().split())
    a.append(x)
    b.append(y)
a.sort()
b.sort()
chk=max(a)
ans=0
for i in b[::-1]:
    if (h<=0 or i<chk): break
    ans+=1
    h-=i
if h>0:
    ans+=(h+chk-1)//chk

print(ans)


Measured memory_bytes: 62296
n=int(input(""))
aa=input("").split(" ")
s=0
a=[]
for i in aa:
    a+=[int(i)]
for i in a:
    if(i%2==0):
        if(i%3!=0 and i%5!=0):
            s=1
if(s==0):
    print("APPROVED")
else:
    print("DENIED")

        

Measured memory_bytes: 9048
import numpy as np
from itertools import combinations
n,m = map(int,input().split())
card =[[0]*n for _ in range(m)]
for i in range(m):
    switch = list(map(int,input().split()))[1:]
    for k in switch:
        card[i][k-1] = 1
##どこに繋がっているかを配列で表現

#print(card)


ans =list(map(int,input().split()))
cnt=0
from itertools import combinations
##全通りを調べる
for epoch in range(1,n+1):
    
    for j in combinations(range(1,n+1),epoch):
        sco =[]
        for lisa in card:##一つずつの照明について調べる
            score =0
            for l in j:
                score += lisa[l-1]
                
            sco.append(score)
        #print(sco)
        if list(np.array(sco)%2) == ans:
            cnt+=1
    

if ans == [0]*m:
    cnt+=1
        
print(cnt)

Measured memory_bytes: 27148
local n,m=io.read("n","n")
local w,v={},{}
for i=1,n do
    w[i],v[i]=io.read("n","n")
end

local INF=10^13
local dp={}
for i=1,n+1 do
    dp[i]={}
    for j=0,10^5 do
        dp[i][j]=INF
    end
end
dp[1][0]=0

for i=1,n do
    for j=0,10^5 do
        if j-v[i]>=0 then
            dp[i+1][j]=math.min(dp[i+1][j],dp[i][j-v[i]]+w[i])
        end
        dp[i+1][j]=math.min(dp[i+1][j],dp[i][j])
    end
end

local max=0
for i=0,10^5 do
    if dp[n+1][i]<=m then
        max=i
    end
end
print(max)

Measured memory_bytes: 106516
#include "iostream"
#include "vector"

using namespace std;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long N, result = 0;
    cin >> N;
    N--;

    if(N % 2 == 1){
        result += N;
        N--;
    }

    result += (N + 1) * (N / 2);

    cout << result << "\n";
    return 0;
}

Measured memory_bytes: 384
#include <stdio.h>

bool isPrime(const int N) {
    if(N == 2) return true;
    if(N < 2 || N % 2 == 0) return false;
    for(int i = 3; i * i <= N; i += 2) {
        if(N % i == 0) return false;
    }
    return true;
}

int main() {
    int n;
    scanf("%d", &n);
    int cnt = 0;
    for(int i = 0; i < n; ++i) {
        int a;
        scanf("%d", &a);
        isPrime(a) && cnt++;
    }
    printf("%d\n", cnt);
    return 0;
}



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

int main()
{
    int a,i,j,n,t,b,c;
    string s,s1,s2;
    cin>>a>>b>>c;
    if(a%b==0)
        t=a/b;
    else
        t=(a/b)+1;
    cout<<t*c<<"\n";


    return 0;
}


Measured memory_bytes: 3588
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
#define chmax(x, y) do { x = max(x, y); } while(0)
#define chmin(x, y) do { x = min(x, y); } while(0)
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, ) (__VA_ARGS__)  // NOLINT
template<typename T>
using reversed_priority_queue = \
                                std::priority_queue<T, std::vector<T>, std::greater<T> >;

ll N, M, X,ans;
ll c[20];
ll a[20][20];

signed main() {
  ans = 1e18;
  cin >> N >> M >> X;
  rep(i, N) {
    cin >> c[i];
    rep(j, M) {
      cin >> a[i][j];
    }
  }
  rep(bit, 1 << N) {
    ll t = 0;
    vector<ll> v(M, 0);
    rep(i, N) {
      if ((bit & (1 << i)) == 0) continue;
      t += c[i];
      rep(j, M) {
        v[j] += a[i][j];
      }
    }

    bool ok = true;
    rep(j, M)
      if (v[j] < X)
        ok = false;
    if (ok && t < ans)
      ans = t;
  }
  if (ans == 1e18)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3564
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
    int key;
    struct node *prev, *next;
} Node;

Node *nil;

void init(void);
void insert(int key);
Node *listSearch(int key);
void deleteNode(Node *t);
void deleteFirst(void);
void deleteLast(void);
void deleteKey(int key);
void printList(void);

int main(void)
{
    int i, n, num;
    char command[16];

    scanf("%d", &n);

    init();

    for (i = 0; i < n; i++) {
        scanf("%s%d", command, &num);
        if (command[0] == 'i') {
            insert(num);
        } else if (command[6] == 'F') {
                deleteFirst();
        } else if (command[6] == 'L') {
                deleteLast();
        } else {
                deleteKey(num);
        }
    }

    printList();

    return 0;
}

void init(void)
{
    nil = (Node *)malloc(sizeof(Node));
    nil->next = nil;
    nil->prev = nil;
}

void insert(int key)
{
    Node *x = (Node *)malloc(sizeof(Node));
    x->key = key;
    x->next = nil->next;
    nil->next->prev = x;
    nil->next = x;
    x->prev = nil;
}

Node *listSearch(int key)
{
    Node *cur = nil->next;
    while (cur != nil && cur->key != key) {
        cur = cur->next;
    }

    return cur;
}

void deleteNode(Node *t)
{
    if (t == nil) return;
    t->prev->next = t->next;
    t->next->prev = t->prev;
    free(t);
}

void deleteFirst(void)
{
    deleteNode(nil->next);
}

void deleteLast(void)
{
    deleteNode(nil->prev);
}

void deleteKey(int key)
{
    deleteNode(listSearch(key));
}

void printList(void)
{
    Node *cur = nil->next;
    int isf = 0;
    while (1) {
        if (cur == nil) break;
        if (isf++ > 0 ) printf(" ");
        printf("%d", cur->key);
        cur = cur->next;
    }
    printf("\n");
}



Measured memory_bytes: 32992
#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int n,m; cin >> n >> m;
  vector<set<int>> num(n);
  rep(i,m){
    int s,c; cin >> s >> c;
    num[--s].insert(c);
  }
  rep(i,n){
    if(num[i].empty()){
      if(n == 1) num[i].insert(0);
      else{
        if(!i) num[i].insert(1);
        else num[i].insert(0);
      }
    }else if(2 <= num[i].size()){
      cout << -1;
      return 0;
    }
  }
  if(n == 1){
    cout << *num[0].begin();
    return 0;
  }else if(n == 2){
    for(int i = 10; i < 100; i++){
      bool ok = true;
      if(*num[0].begin() != i/10) ok = false;
      if(*num[1].begin() != i%10) ok = false;
      if(ok){
        cout << i;
        return 0;
      }
    }
  }else if(n == 3){
    for(int i = 100; i < 1000; i++){
      bool ok = true;
      if(*num[0].begin() != i/100) ok = false;
      if(*num[1].begin() != i/10%10) ok = false;
      if(*num[2].begin() != i%10) ok = false;
      if(ok){
        cout << i;
        return 0;
      }
    }
  }
  cout << -1;
  return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
#include <math.h>
#include <cmath>

using namespace std;
using ll =long long;
using vi = vector<int>; 
using vvi = vector<vi>; 
using vl = vector<ll>; 
using vvl = vector<vl>; 
using vs = vector<string>;
using vvs = vector<vs>; 
using vc = vector<char>;
using vvc = vector<vc>; 
using vb = vector<bool>;
using vvb = vector<vb>; 

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; }
const long long INF = 1LL << 60;
typedef pair<int, int> PR;

#define rep(i,n) ;for( int i =0; i < n ; i ++)
#define all(a) a.begin(),a.end()
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define fr(i,a,b) for(int i=a;i<b;i++)
#define pri(a)  printf("%.14lf\n",a);
#define MOD 1000000007
bool is_int_lround(double x){
  return std::lround(x)==x;
}
ll keta(ll x){
  ll n=0;
  while(x>0){
    x /=10 ;
    n ++;
  }
  return n;
}

ll conbi(int n,int m){
  
  cin>>n>>m;
  vector<ll> a(100);
  a[0] =1;
  for(int i=0;i<14;i++){
    a[i+1]=a[i]*(i+1);
  }
  return a[n] /(a[m] *a[n-m]);
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;//繰り返し二乗法
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
ll kaijo(ll x){
  ll z=1;
  if(x==0){
    return 1;
  }
  while(x>0){
    z *=x;
    z %=MOD;
    x--;
  }
  return z;
}
ll yakusu_num(ll n){
  vl yakusu(n+1,1);
  for(ll i=2;i*i<=n;i++){ 
    while(n%i==0){
      n /=i;
      yakusu[i]++;
    }
  }
  if(n!=1)yakusu[n]++;
  ll num=1;
  for(ll i=0;i <=n;i++){
    num*=yakusu[i];
  }
  return num;
}
 

//cout<<""<<endl;
int main(){
  int K;cin>>K;
  bool ok=false;
  int cnt=1;
  int co=7;
  co%=K;
  while(1){
    
    if(co==0){
      ok=true;
      break;
    }
    if(cnt>=1000000)break;
    co*=10;
    co+=7;
    co%=K;
    cnt++;
  }
  if(ok){
    cout<<cnt<<endl;
  }
  else cout<<-1<<endl;
}






Measured memory_bytes: 3616
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Int32;
using static System.Math;

class Program
{
    struct Point {
        public int X;
        public int Y;
    }

    static void Main(string[] args)
    {
        label:
        var sp = ReadLine().Split();
        int h = Parse(sp[0]);
        int w = Parse(sp[1]);
        int d = Parse(sp[2]);
        var array = new Point[h * w + 1];
        for(int row = 0; row < h; ++row)
        {
            sp = ReadLine().Split();
            for(int col = 0; col < sp.Length; ++col)
            {
                int num = Parse(sp[col]);
                array[num].X = col;
                array[num].Y = row;
            }
        }
        var mpArray = new int[array.Length];
        for(int i = 0; i < mpArray.Length; ++i)
        {
            if (i <= d) continue;
            mpArray[i] = mpArray[i - d] + Abs(array[i].X - array[i - d].X) + Abs(array[i].Y - array[i - d].Y);
        }
        var results = new int[Parse(ReadLine())];
        for(int i = 0; i < results.Length; ++i)
        {
            sp = ReadLine().Split();
            results[i] = mpArray[Parse(sp[1])] - mpArray[Parse(sp[0])];
        }
        foreach(int n in results)
        {
            WriteLine(n);
        }
        //goto label;
    }
}


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

using namespace std;

#define ll long long
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef set<int> si;
typedef set<vi> svi;
typedef set<ll> sll;
typedef set<vll> svll;
typedef set<pii> spii;
typedef set<vpii> svpii;
typedef set<pll> spll;
typedef set<vpll> svpll;
typedef vector<si> vsi;
typedef vector<sll> vsll;
typedef vector<spii> vspii;
typedef vector<spll> vspll;
#define ft first
#define sc second
#define pb push_back
#define ins insert
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(v) v.begin(),v.end()
#define Sort(v) sort(all(v))
#define sortarr(a,n) sort(a,a+n)
#define tt ll tc; cin >> tc; while(tc--)
#define rep(var,start,end,interval) for(ll var=start;(interval>0 and var<end) or (interval<0 and var>end);var+=interval)
#define each(x,v) for(auto x : v)
#define takearr0(a,n) rep(z,0,n,1) cin >> a[z];
#define takearr1(a,n) rep(z,1,n+1,1) cin >> a[z];

const ll max_n = 2e5 + 5;

ll n,k;
vll a;

bool can(ll val)
{
	ll cntr = 0;
	each(x,a)
	{
		cntr += x / val;
		if(x%val == 0)
			--cntr;
	}
	if(cntr > k)
		return false;
	return true;
}

int main()
{
	io;
	cin >> n >> k;
	a.resize(n);
	takearr0(a,n);
	ll mx = 0;
	each(x,a) mx = max(mx,x);
	ll l = 1,r = mx;
	ll mid = (l+r)/2;
	while(l < r)
	{
		if(can(mid))
			r = mid;
		else
			l = mid+1;
		mid = (l+r)/2;
	}
	cout << l << endl;
	return 0;
}

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

int main(void) {
    int N, A, B, product;
    cin >> N >> A >> B;
    product = N * A;

    if (product > B) {
        cout << B << endl;
    } else if (product == B) {
        cout << product << endl;
    } else {
        cout << product << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
 int n ;
 cin >> n;
 vector<int> l(n);
   for(int i=0; i<n; i++){
     cin >> l.at(i) ;
   }

sort(l.begin(),l.end());
 
 int result = 0;
  for(int i=0; i<n-2;i++){
    for(int j=i+1; j<n-1;j++){
        int a = l.at(i);
        int b = l.at(j);
        int maxc_index = lower_bound(l.begin(),l.end(),a+b)-l.begin();
        result += maxc_index -(j+1);
    }
  }
 cout << result << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const long long mod = pow(10, 9) + 7;
const long long mod2 = 998244353;
const long long INF = 1LL << 60;
const int inf = pow(10, 9) + 7;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
template <class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template <class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
int divCeil(int A, int B) {return (A + (B - 1)) / B;}
long long divCeil(long long A, int B) {return (A + (B - 1)) / B;}
long long divCeil(int A, long long B) {return (A + (B - 1)) / B;}
long long divCeil(long long A, long long B) {return (A + (B - 1)) / B;}

int main()
{
  long long N, K;
  cin >> N >> K;
  
  vector<long long> num(N + 10, 1);
  vector<long long> den(N + 10, N);
  
  for (long long i = 1; i <= N; i++)
  {
    long long j = i;
    long long d = 1;
    while (true)
    {
      if (j >= K) break;
      j *= 2;
      d *= 2;
    }
    den.at(i) *= d;
  }
  
  long long NUM = 1;

  for (int i = 2; i <= N; i++)
  {
    num.at(i) *= den.at(1) / den.at(i);
    NUM += num.at(i);
  }

  double ans = (double)NUM / (double)den.at(1);

  cout << setprecision(20) << ans << endl;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9+7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge{
    ll to, weight;
    Edge(ll t, ll w) : to(t), weight(w){}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;


ll gcd(ll a,ll b){
    if(b == 0){
        return a;
    }else{
        return gcd(b, a%b);
    }
}

ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll keta(ll N){
    int tmp{};
    while( N > 0 ){
        tmp += ( N % 10 );
        N /= 10;
    }
    N = tmp;
    return N;
}

// 回文
bool kai(string S){
    bool flag = true;
    for(ll i=0;i<S.size()/2;++i){
        if(S[i] != S[S.size()-i-1]){
            flag = false;
            break;
        }
    }
    return flag;
}
// ---------------------------------------------

int main(){
    ll n;
    cin>>n;
    ll ans = n;
    for(ll i=0;i<=n;++i){
        ll flag = 0;
        ll x = i;
        while(x > 0){
            flag += x % 6;
            x /= 6;
        }
        x = n - i;
        while(x > 0){
            flag += x % 9;
            x /= 9;
        }
        ans = min(ans, flag);
    }
    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 256
a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())

memo = [a,b,c,d,e]


m = 10
cnt = 0
for i in memo:
    if i % 10 == 0:
        cnt += int(i/10)
    else:
        cnt += int(i/10)+1

    if i%10 < m and i%10 != 0:
        m = i%10

print(cnt*10-(10-m))



Measured memory_bytes: 3064
import sys
sys.setrecursionlimit(10**9)

N, Q = map(int ,input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	at,bt = map(int ,input().split())
	E[at].append(bt)
	E[bt].append(at)
cnt = [0 for _ in range(N+1)]
for _ in range(Q):
	pt,xt = map(int, input().split())
	cnt[pt] += xt

flg = [0 for _ in range(N+1)]
q = [[1,-1]]
while q:
	cp, root = q.pop()
	for np in E[cp]:
		if np == root:
			continue
		cnt[np] += cnt[cp]
		q.append([np,cp])

cnt.pop(0)
for c in cnt:
	print(c, end = " ")
print()

Measured memory_bytes: 64560
// _______  _____  _______  _____  _______ _______ _____ _______ _____ _    _ _______ 
// |       |     | |  |  | |_____] |______    |      |      |      |    \  /  |______ 
// |_____  |_____| |  |  | |       |______    |    __|__    |    __|__   \/   |______ 
//
//  _____   ______  _____   ______  ______ _______ _______ _______ _____ __   _  ______   /
// |_____] |_____/ |     | |  ____ |_____/ |_____| |  |  | |  |  |   |   | \  | |  ____  / 
// |       |    \_ |_____| |_____| |    \_ |     | |  |  | |  |  | __|__ |  \_| |_____| .  
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define FOR(I,A,B) for(int I = int(A); I < int(B); ++I)
#define FORR(I,A,B) for(int I = int((B)-1); I >= int(A); --I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu z
const ll INF=1e18+7;
const ll MOD=1e9+7;



int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll a,b,c;
	cin >> a >> b >> c;

	ll ans = b;
	ans += min(a+b+1,c);

	cout << ans << endl;

}

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

int main(){
  int count=0;
  string S,T;
  cin >> S;
  T=S;
  reverse(T.begin(),T.end());
  for(int i=0;i<S.size();i++){
    if(S.at(i)!=T.at(i)){
      count++;
    }
  }
  cout << count/2 << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define pb              push_back
#define ll              long long
#define scd(a)          scanf("%d",&a)
#define mp(a,b)         make_pair(a,b)
#define scl(w)          scanf("%lld",&w)
#define scdd(a,b)       scanf("%d %d",&a,&b)
#define srt(a)          sort(a.begin(),a.end())
#define rsrt(a)         sort(a.rbegin(),a.rend())
#define scll(a,b)       scanf("%lld %lld",&a,&b)
///4direction ->        int del_x[]={-1,0,1,0},del_y[]={0,1,0,-1};
#define input(v,n)      for (int i=0;i<n;i++){int a;cin >> a;v.push_back(a);}
#define inputll(v,n)    for (ll i=0;i<n;i++){ll a;cin >> a;v.push_back(a);}
#define vii             vector < int >
#define vll             vector < ll >
#define pii             pair < int , int >
#define pll             pair < ll , ll >
#define mk              make_pair
#define inf8            100000008

using namespace std;

vector < pll > v;

int main()
{
    ll n;
    scl(n);

    for (int i=0;i<n;i++){
        ll a,b;
        scll(a,b);

        v.pb({b,a});
    }
    srt(v);
    ll time = 0 , cnt = 0;

    for (int i=0 ; i<n; i++){
        time+=v[i].second;
        if (time<=v[i].first)cnt++;
    }
    cout << (cnt==n?"Yes":"No") << endl;

    return 0;
}



Measured memory_bytes: 6384
#include<stdio.h>

#define max(a,b) ((a)>(b))?(a):(b);
#define min(a,b) ((a)>(b))?(b):(a);

int main(void)
{
	int n, a, sum = 0;
	int b[99];
	scanf("%d", &n);
	for (size_t i = 0; i < n - 1; i++)
	{
		scanf("%d", b + i);
	}
	sum = b[0] + b[n - 2];
	for (size_t i = 0; i < n - 2; i++)
	{
		sum += min(b[i], b[i + 1]);
	}

	printf("%d", sum);

	return 0;
}

Measured memory_bytes: 128
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
typedef long long ll;
using namespace std;
int main(){
	
	ll r,b,g;
	cin>>r>>b>>g;
	
	if((b*10 + g)%4 == 0){
		
		cout << "YES";
		
	}else{
		
		cout << "NO";
	}
	
	return 0;
	
}

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

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define hash sodnf9843bsd
#define ll long long
#define INF (1<<29)
#define INFF 0x7fffffff
#define LINF (1ll<<60)
#define LINFF 0x7fffffffffffffff
#define PI 3.14159265359
#define _fi(n) for(int i = 0;i < n;i++)
#define _fj(n) for(int j = 0;j < n;j++)
#define _fk(n) for(int k = 0;k < n;k++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define YESNO(ans) cout << (ans?"YES":"NO") << endl
#define prec(x) cout << fixed << setprecision(x)
#define pqueue priority_queue
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define endl '\n'

const ll maxn = 100;
const ll maxv = 100000;
ll iw[maxn+7];
ll dp[maxn+7][maxv+7] = {};
int iv[maxn+7];
int n, w;

ll solve(int i, int j){
    if (j == 0) return 0;
    if (j < 0 || i == n) return LINF;

    if (dp[i][j] == 0){
        dp[i][j] = min(solve(i+1, j-iv[i])+iw[i], solve(i+1, j));
    }
    return dp[i][j];
}

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

    cin>>n>>w;
    _fi(n) cin>>iw[i]>>iv[i];

    int ans = 0;
    for(int i = 0;i <= maxv;i++){
        if (solve(0, i) <= w) ans = i;
    }
    cout << ans << endl;


    return 0;
}


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

const int DIM = 1e5 + 5;
const int MOD = 1e9 + 7;

unsigned long long psm[DIM];

int main( void ) {
    
    int n, m;
    cin >> n >> m;
    
    unsigned long long ans1 = 0;
    for( int i = 1; i <= n; i ++ ) {
        int x;
        cin >> x;
        
        psm[i] = x + 1e9;
        ans1 += psm[i] * (i - 1) - psm[i - 1];
        psm[i] += psm[i - 1];
    }
    
    unsigned long long ans2 = 0;
    for( int i = 1; i <= m; i ++ ) {
        int x;
        cin >> x;
        
        psm[i] = x + 1e9;
        ans2 += psm[i] * (i - 1) - psm[i - 1];
        psm[i] += psm[i - 1];
    }
    
    cout << ( (ans1 % MOD) * (ans2 % MOD) ) % MOD << endl;
    return 0;
}

Measured memory_bytes: 1024
// https://atcoder.jp/contests/abc125/tasks/abc125_c

package main

import "fmt"
import "bufio"
import "os"
import "strconv"

var _sw = bufio.NewScanner(os.Stdin)

func main() {
	N := nextInt()
	A := make([]int, N)
	for i := range A {
		A[i] = nextInt()
	}

	gcdL := make([]int, N)
	gcdR := make([]int, N)
	gcdL[0] = A[0]
	gcdR[len(A)-1] = A[len(A)-1]

	for i := 1; i < len(A); i++ {
		gcdL[i] = gcd(A[i], gcdL[i-1])
	}
	for i := len(A) - 2; i >= 0; i-- {
		gcdR[i] = gcd(A[i], gcdR[i+1])
	}

	max := 0
	for i := 0; i < len(A); i++ {
		var v int
		if i == 0 {
			v = gcdR[i+1]
		} else if i == len(A)-1 {
			v = gcdL[i-1]
		} else {
			v = gcd(gcdL[i-1], gcdR[i+1])
		}
		if max < v {
			max = v
		}
	}
	fmt.Println(max)
}

func init() {
	_sw.Split(bufio.ScanWords)
}

func nextInt() int {
	if _sw.Scan() == false {
		panic(_sw.Err())
	}
	i, e := strconv.Atoi(_sw.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextString() string {
	if _sw.Scan() == false {
		panic(_sw.Err())
	}
	return _sw.Text()
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func gcd(x, y int) int {
	if y == 0 {
		return x
	}

	return gcd(y, x%y)
}

func maxN(xs ...int) int {
	if len(xs) == 1 {
		return xs[0]
	}

	z := xs[0]
	for _, x := range xs[1:] {
		z = max(x, z)
	}
	return z
}

func gcdN(xs ...int) int {
	if len(xs) == 1 {
		return xs[0]
	}

	z := xs[0]
	for _, x := range xs[1:] {
		z = gcd(x, z)
	}
	return z
}


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

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	ll h;
	while(cin>>h, h!=0){
		vector<vll> field(5, vll(h));
		for(int i=h-1; i>=0; i--)rep(j, 5)
			cin >> field[j][i];

		ll ans=0;
		bool loop = true;
		while(loop){
			loop = false;
			for(int i=h-1; i>=0; i--){
				ll left=0, right=0;
				rep(j, 4){
					if (i>=field[j].size() || i>=field[j+1].size()){
						if (right-left>=2) break;
						left=j+1;
						continue;
					}
					if (field[j][i]==field[j+1][i]) right=j+1;
					if (field[j][i]!=field[j+1][i]){
						if (right-left>=2) break;
						left=j+1;
					}
					if (right-left>=2) loop = true;
				}
				if (right-left>=2){
					for(int k=left; k<=right; k++){
						ans += field[k][i];
						field[k].erase(field[k].begin()+i);
					}
				}
			}
		}
		cout << ans << "\n";
	}
	return 0;
}


Measured memory_bytes: 3088
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <cmath>
#include <queue>
#include <set>
#include <stack>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for(ll i = n - 1; i >= 0; i--)
#define repone(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for(auto i:mp)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

ll n, m, Q;
vector<ll> l, r;
vector<ll> p, q;

void solve() {
    ll d[n + 1][n + 1];
    fill(d[0], d[n + 1], 0);
    rep(i, m) d[l[i]][r[i]]++;
    ll dp[n + 1][n + 1];
    fill(dp[0], dp[n + 1], 0);
    repone(i, n) {
        repone(j, n) {
            dp[i][j] = dp[i][j - 1] + d[i][j];
        }
    }
    rep(i, Q) {
        ll ans = 0;
        for (ll j = p[i]; j <= q[i]; ++j) ans += dp[j][q[i]] - dp[j][p[i] - 1];
        cout << ans << endl;
    }
}

int main() {
    cin >> n >> m >> Q;
    l.resize(m), r.resize(m);
    p.resize(Q), q.resize(Q);
    rep(i, m) cin >> l[i] >> r[i];
    rep(i, Q) cin >> p[i] >> q[i];
    solve();
    return 0;
}

Measured memory_bytes: 9600
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
    bool done_something = false;
    stringstream res;
    res << "[";
    for (; begin_iter != end_iter and counter; ++begin_iter) {
        done_something = true;
        counter--;
        res << *begin_iter << ", ";
    }
    string str = res.str();
    if (done_something) {
        str.pop_back();
        str.pop_back();
    }
    str += "]";
    return str;
}

vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
    vector<int> ord(size);
    for (int i = 0; i < size; i++) ord[i] = i;
    sort(ord.begin(), ord.end(), compare);
    return ord;
}

template <typename T>
bool MinPlace(T& a, const T& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool MaxPlace(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename Container>
int SZ(const Container& S) { return S.size(); }

template <typename S, typename T>
ostream& operator <<(ostream& out, const pair<S, T>& p) {
    out << "{" << p.first << ", " << p.second << "}";
    return out;
}

template <typename T>
ostream& operator <<(ostream& out, const vector<T>& v) {
    out << "[";
    for (int i = 0; i < (int)v.size(); i++) {
        out << v[i];
        if (i != (int)v.size()-1) out << ", ";
    }
    out << "]";
    return out;
}

#if DEBUG && !ONLINE_JUDGE
    ifstream input_from_file("input.txt");
    #define cin input_from_file

    #define dbg_var(x) clog << #x  << ": " << x << endl;
    #define dbg_array(x, len) clog << #x << ": " << print_iterable(x, x+len, -1) << endl;
#else
    #define dbg_var(x)
    #define dbg_array(x, len)
#endif

///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////

const int MAXN = 80010; 

bool done[2][2*MAXN];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); // Remove in problems with online queries!
    int N;
    cin >> N;
    vector<int> a(N);
    int S = 0;
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        S += a[i];
    }
    
    int zero = MAXN;
    done[0][zero] = true;
    random_shuffle(a.begin(), a.end());
    bool it = 0;
    for (int x : a) {
        for (int i = 0; i < 2*MAXN; i++) done[it^1][i] = 0;
        for (int i = 0; i < 2*MAXN; i++) {
            if (i + x < 2*MAXN) done[it^1][i+x] |= done[it][i];
            if (i - x >= 0) done[it^1][i-x] |= done[it][i];
        }
        it ^= 1;
    }
    for (int i = 0; i < MAXN; i++) {
        if (done[it][zero+i]) {
            cout  << (S+i)/2 << "\n";
            break;
        }
    }
}



Measured memory_bytes: 640
#include<stdio.h>
#include<string.h>

int main(void){
  int n;
  scanf("%d",&n);
  
  char s[n+1][11] , temp;
  int len;
  
   scanf("%s",s[0]);
   len = strlen(s[0]);
   temp = s[0][len-1];
  
  for(int i = 1;i < n;i ++){
    scanf("%s",s[i]);
    if(s[i][0]!=temp){
      printf("No");
      return 0;
    }
    len = strlen(s[i]);
    temp = s[i][len-1];
  }
  
  for(int i = 0;i < n-1;i ++){
    for(int j = i+1;j < n;j ++){
      if((strcmp(s[i],s[j]))==0){
        printf("No");
        return 0;
      }
    }
  }
  
  
  printf("Yes");

  return 0;
}


Measured memory_bytes: 128
#include <cstdio>
#include <algorithm>
using namespace std;

int a[10];

bool dfs(int k, int l, int r) {
	if (k == 10)
		return true;
	if (a[k] > l && dfs(k + 1, a[k], r))
		return true;
	if (a[k] > r && dfs(k + 1, l, a[k]))
		return true;
	return false;
}

int main() {
	int N;
	scanf("%d", &N);
	for (; N > 0; N--) {
		for (int i = 0; i < 10; i++)
			scanf("%d", &a[i]);
		printf(dfs(0, 0, 0) ? "YES\n" : "NO\n");
	}
}

Measured memory_bytes: 1008
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(X,Y) ((X)>(Y)?X=(Y),true:false)
#define chmax(X,Y) ((X)<(Y)?X=(Y),true:false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}

#define N 100000
int n, m;
vector<P> e[N];
int w[N];

void dfs(int u, int d, ll *s, bool &f, ll &es){
	w[u] = d;
	s[d]++;
	es += e[u].size();
	for(P x: e[u]){
		int v = x.fst, d2 = (d+x.snd)%3;
		if(w[v]<0) dfs(v, d2, s, f, es);
		else f &= (w[v]==d2);
	}
}

int main(){
	cin>>n>>m;
	rep(i, m){
		int a, b;
		cin>>a>>b;
		a--;
		b--;
		e[a].pb(P(b, 1));
		e[b].pb(P(a, 2));
	}
	mset(w, -1);
	ll res = 0;
	rep(i, n){
		ll s[3] = {};
		bool f = true;
		ll es = 0;
		if(w[i]<0){
			dfs(i, 0, s, f, es);
			if(f){
				if(s[0]&&s[1]&&s[2]) res += s[0]*s[1]+s[1]*s[2]+s[2]*s[0];
				else res += es/2;
			} else {
				ll t = s[0]+s[1]+s[2];
				res += t*t;
			}
		}
	}
	cout<<res<<endl;
	return 0;
}


Measured memory_bytes: 15488
n = int(input())
a = list(map(int, input().split()))

ans = [0]*(len(a) + 1)
for i, name in enumerate(a):
    if i == 0:
        ans[i] = name
        ans[i+1] = name
    else:
        ans[i] = min(name, a[i - 1])
        ans[i+1] = name

print(sum(ans))

Measured memory_bytes: 3060
#include <iostream>
#include <map>
#include <unordered_map>
#include <algorithm>
#define llint int

using namespace std;

int n;
int a[5005];
int nx[5005][5005];
int dp[5005][5005];
unordered_map<int, int> mp;
map<int, int> mp2;

int main(void)
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	sort(a+1, a+n+1);
	
	if(n <= 2){
		cout << n << endl;
		return 0;
	}
	
	//
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= 1; j++){
			if(i >= j) continue;
			mp2[a[j]-a[i]]++;
		}
	}
	llint aaa = 0;
	for(auto it = mp2.begin(); it != mp2.end(); it++){
		aaa = max(aaa, it->second);
	}
	//
	
	for(int i = 1; i <= n; i++) mp[a[i]] = i;
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(j >= i) continue;
			if(mp.count(a[i] + a[i]-a[j])) nx[i][j] = mp[a[i]+a[i]-a[j]];
		}
	}
	
	for(int i = 2; i <= n; i++){
		for(int j = 1; j < i; j++){
			if(nx[i][j]) dp[nx[i][j]][i] = dp[i][j] + 1;
		}
	}
	
	int ans = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j < i; j++){
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans+2 << endl;
	return 0;
}


Measured memory_bytes: 92116
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main() {
  int a, b, c, d; cin >> a >> b >> c >> d;
  if (abs(a - c) <= d || (abs(a - b) <= d && abs(b - c) <= d)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;

int n;
char data[500];

vector<pair<int, char> > cnt(26);

int length;

void input(){
	scanf("%d\n", &n);
	gets(data);
	for (int i = 0; i < n; i+=2){
		cnt[data[i] - 'a'].first++;
	}
	sort(cnt.begin(), cnt.end(), greater<pair<int, char> >());
	cnt.push_back(make_pair<int, char>(-1, 0));
}

void process(){
	int curcnt = 0;
	int i;
	for (i = 0; i < 26; ++i) {
		if (cnt[i].first <= 1) break;
		++curcnt;
		if (cnt[i].first != cnt[i + 1].first) {
			if (curcnt >= 3) {
				length += curcnt * 2 + 4;
			}
			else {
				length += curcnt * 4;
			}
			curcnt = 0;
		}
	}
	for (; i < 26; ++i) {
		if (cnt[i].first == 1) {
			length += 2;
		}
		else {
			break;
		}
	}
	length--;
}

void output(){
	printf("%d\n", length);
}

int main() {
	input();
	process();
	output();
	return 0;
}

Measured memory_bytes: 1036
n,m = map(int,input().split())
AB = sorted([list(map(int,input().split())) for i in range(n)])
bottle = 0
yen = 0

for i in range(n):
    bottle += AB[i][1]
    yen += AB[i][0] * AB[i][1]
    
    if bottle >= m:
        yen -= (bottle - m) * AB[i][0]
        break
        
print(yen)

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

typedef long long ll;
typedef pair<ll, ll> P;


#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD 
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);


int main() {
    ll n, ans = 0; cin >> n;
    rep(i, n) {
        ll a; cin >> a;
        if((i+1)%2 == 1 && (a%2) == 1) ans++;
    }
    cout << ans << endl;
    return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) { 
    if(n == 0) return 1;
    
    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count));
}

ll my_gcd(ll a, ll b) {
    if(b == 0) return a;
    return my_gcd(b, a%b);
}

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}


Measured memory_bytes: 3592
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <stdlib.h>

typedef long double ld;
typedef long long ll;

using namespace std;

int main() {
	ll n, k;
	cin >> n >> k;
	ll mod = n % k;
	cout << min(mod, k - mod) << endl;

	return 0;
}


Measured memory_bytes: 3592