akhauriyash/Code-Regression huggingface dataset:
/*
[abc124] C - Coloring Colorfully
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

string S;

ll solve() {
    int ans = S.length();
    for (int p = 0; p < 2; p++) {
        int cnt = 0;
        for (int i = 0; i < (int)S.length(); i++) {
            cnt += ((S[i] - '0') ^ i ^ p) & 1;
        }
        ans = min(ans, cnt);
    }

    return ans;
}

int main() {
    cin >> S;

    cout << solve() << endl;

    return 0;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
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(){
   int a, b, c;
   cin >> a >> b >> c;

   int total = a + b + c;

   if(total >= 22) cout << "bust" << endl;
   else cout << "win" << endl;

    

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;

int main() {
    int a,b;
    cin >> a >> b;
    int answer = a + b;
    if (answer < a - b) {
        answer = a-b;
    }
    if (answer < a * b) {
        answer = a * b;
    }
    cout << answer;
    return 0;
}

Measured memory_bytes: 256
G=[];puts [*$<.map{|l|x=l.split.map &:to_i;x.each{|a|G[a]=*G[a],*x}},*1..$.].map{|z|*w=z;f=w.size;[G.index{z=*z,*u=w;w=[];f*=[1,*u.map{|v|w=*w,*b=G[v]-z;b.size}].max;!u[0]},f]}.min

Measured memory_bytes: 2172
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define REPS(i,n) for (ll i = 1; i<=(ll)(n); i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000000000
#define N 550

vector<vector<ll>> adj={{0,1},{1,0},{0,-1},{-1,0}};
vector<vector<ll>> seats(N,vector<ll>(N,-1));
vector<vector<ll>> soon(N,vector<ll>(N,0));

void dfs(ll x, ll y){
    if (soon[x][y]==1) seats[x][y]--;
    REP(i,4)if (seats[x+adj[i][0]][y+adj[i][1]]>seats[x][y]) dfs(x+adj[i][0],y+adj[i][1]);
    if (soon[x][y]==0) seats[x][y]--;
}

int main(){
    ll n, p, ind, ans; cin >> n;
    ind=1; ans = 0;
    while(ind<=n+1-ind){
        FOR(i,ind,n+1-ind)FOR(j,ind,n+1-ind) seats[i][j]=ind-1;
        ind++;
    }
    REP(i,n*n){
        cin >> p;
        ll h = (p-1)/n+1; ll w = (p-1)%n+1;
        ans+=seats[h][w];
        soon[h][w]=1;
        REP(j,4) if (seats[h+adj[j][0]][w+adj[j][1]]>seats[h][w]) dfs(h+adj[j][0],w+adj[j][1]);
    }
    cout << ans << endl;
}

Measured memory_bytes: 23640
#include<bits/stdc++.h>
#define ll long long 
#define mp make_pair 
#define f(i,n) for(int i=0;i<n;i++) 
#define F first 
#define S second 
#define pb push_back 

using namespace std;

void test(){
	ll n,k;
	cin>>n>>k;
	ll a[n];
	f(i,n)cin>>a[i];
	
	ll low = 1, high = 1; 
	f(i,n)high = max(high,a[i]);
	
	ll ans = high;
	while(low<=high){
		ll mid = low + (high-low)/2;
		ll cnt = 0LL;
		if(mid!=1){
			f(i,n){
				cnt = cnt + (a[i]/mid);
				if(a[i]%mid==0)cnt--;
			}
		}else{
			f(i,n)cnt = cnt + (a[i]-1);
		}
		if(cnt<=k){
			ans = mid;
			high = mid-1;
		}else
			low = mid+1;
	}
	cout<<ans<<"\n";
}

int main(){
	int tests=1;
	// cin>>tests;
	while(tests--){
		test();
	}
}


Measured memory_bytes: 5088
# Problem https://atcoder.jp/contests/abc086/tasks/abc086_a
# Ruby 3rd Try
even = "Even"
odd = "Odd"
a,b = gets.chomp('\n').split(' ').map(&:to_i)
abmod = (a * b).even?
if abmod
    puts(even)
else
    puts(odd)
end


Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Edge = pair<int, ll>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<Edge>>;
const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

const int CASES = 3;

void solve() {
    ll X, Y; cin >> X >> Y;
    ll A, B, C; cin >> A >> B >> C;
    vector<ll> p(A), q(B);
    priority_queue<ll> r;
    for(int i = 0; i < A; ++i)cin >> p[i];
    for(int i = 0; i < B; ++i)cin >> q[i];
    for(int i = 0; i < C; ++i){
        ll tmp; cin >> tmp;
        r.push(tmp);
    }
    sort(p.begin(), p.end(), greater<ll>());
    sort(q.begin(), q.end(), greater<ll>());

    priority_queue<ll,vector<ll>,greater<ll>> que;
    for(int i = 0; i < X; ++i){
        que.push(p[i]);
    }
    for(int i = 0; i < Y; ++i){
        que.push(q[i]);
    }
    for(int i = 0 ; i < C; ++i){
        ll q_ = que.top();
        ll r_ = r.top();
        if( q_ < r_){
            que.pop();
            que.push(r_);
            r.pop();
            r.push(q_);
        }
        else{
            break;
        }
    }
    ll ans = 0;
    for (int i = 0; i < X + Y; ++i) {
        ans += que.top();
        que.pop();
    }
    cout << ans << endl;
}

int main() {
#ifdef MYLOCAL
    vector<string> filenames {"input1.txt", "input2.txt", "input3.txt", "input4.txt", "input5.txt"};
    cout << "------------\n";
    for(int i = 0; i < CASES; ++i){
        ifstream in(filenames[i]);
        cin.rdbuf(in.rdbuf());
        solve();
        cout << "------------\n";
    }
#else

    solve();

#endif
    return 0;
}


Measured memory_bytes: 4724
//****************************//
/**********  main *************/
//****************************//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
 
using namespace std;
typedef long long ll;
 
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,a,b) for(i=a;i<b;i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
 
 
int main() {
 
	std::cin.tie(0);
 
    ll n ;
    ll i;
    cin >> n;
    ll sum=0;
 
    for(i=1;i<=n;i++){
        if(i%3==0 || i%5==0 || i%15==0){
            continue;
        }
        sum=sum+i;
    }
 
    cout << sum << endl;
 
    return 0;
}

Measured memory_bytes: 3632
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>
using namespace std;


const long long int mod=1000000007;
const long long int INF=99999999999999999;

long long int n,m,x,y,d,k,p[10],q[10];
string str;
int main() {
	cout<<setprecision(10);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p[i];
	}
	for(int i=0;i<n;i++){
		cin>>q[i];
	}
	long long int res=1;
	for(int i=1;i<n;i++){
		res*=i;
	}
	bool pmemo[10]={},qmemo[10]={};
	long long int a=0,b=0;
	for(int i=0;i<n;i++){
		long long int count=0;
		for(int j=1;j<=n;j++){
			if(pmemo[j]){}
			else if(p[i]==j){pmemo[j]=true;break;}
			else{count++;}
		}
		a+=res*count;
		count=0;
		for(int j=1;j<=n;j++){
			if(qmemo[j]){}
			else if(q[i]==j){qmemo[j]=true;break;}
			else{count++;}
		}
		b+=res*count;
		if(i!=n-1)res/=(n-i-1);
	}
	cout<<abs(a-b);
} 


Measured memory_bytes: 256
#include<stdio.h>
 
int main(){
  int n,a[100],i,j,tmp,sum=0;
 
  scanf("%d",&n);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
 
  for(i=0;i<n;i++)
    for(j=0;j<n-1;j++)
      if(a[j]>a[j+1]){
    tmp=a[j];
    a[j]=a[j+1];
    a[j+1]=tmp;
    sum++;
      }
 
  for(i=0;i<n;i++){
    printf("%d",a[i]);
    if(i!=n-1)printf(" ");
  }
  printf("\n%d\n",sum);
 
  return 0;
}

Measured memory_bytes: 604
#include <iostream>
using namespace std;
 
int main ()
{
  long long n;
  cin >> n;
	for(int i = 1;i <=1000000; ++i){
    	if (100 * i <= n && n <= 105 * i) {
        	cout << 1;
              return 0;
        }
    }
  cout << 0;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n; cin >> n;
  set<int> s;
  for(int i = 0; i < n; i++){
    //cout << s.size() << endl;
    int a; cin >> a;
    if(s.count(a))
       s.erase(a);
    else
       s.insert(a);
  }
  cout << s.size() << endl;
}


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

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; }

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
 
using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;
ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }
int main(){
    int h, w;
    cin >> h >> w;
    vector<vector<int>>board(10, vector<int>(10));
    rep(i, 0, 10)rep(j, 0, 10)cin >> board[i][j];
    rep(k, 0, 10){
        rep(i, 0, 10){
            rep(j, 0, 10){
                chmin(board[i][j], board[i][k]+board[k][j]);
            }
        }
    }
    int ans = 0;
    rep(i, 0, h*w){
        int a;
        cin >> a;
        if(a==-1)continue;
        ans += board[a][1];
    }
    cout << ans << endl;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    string w;

    cin >> w;

    map <char, int> mp;
    map <char, int> :: iterator it;

    set <char> s;

    for(int i=0; i<w.size(); i++)
    {
        mp[w[i]]++;
        s.insert(w[i]);
    }

    int count1 = 0;


    for(it=mp.begin(); it!=mp.end(); it++)
    {
        if(it->second > 0 && it->second%2 == 0)
        {
            count1++;
        }
    }

    if(count1 == s.size())
    {
        cout << "Yes";
    }

    else
    {
        cout << "No";
    }



}





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

int main() {
  int n; cin>>n;
  vector<string>blue(n);
  for(int i=0; i<n ;i++){
    cin>>blue[i];
  }
  int m; cin>>m;
  vector<string>red(m);
  for(int i=0; i<m ;i++){
    cin>>red[i];
  }
  vector<int>num(n);
  for(int i=0;i<n;i++){
    for(int t=0;t<n;t++){
      if(blue[t]==blue[i]){
        num[i]++;
      }
    }
  }
  for(int i=0;i<n;i++){
    for(int t=0; t<m;t++){
      if(red[t]==blue[i]){
        num[i]--;
      }
    }
  }
  sort(num.begin(),num.end());
  reverse(num.begin(),num.end());
  if(num[0]<0) cout<<0<<endl;
  else cout<<num[0]<<endl;
}
    
    


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9+7;
const int INF = 1<<30;
const ll LINF = 1LL<<60;

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    ll x;
    cin >> x;

    for (int a = -1000; a <= 1000; ++a) {
        for (int b = -1000; b <= 1000; ++b) {
            if (pow(a, 5) - pow(b, 5) == x) {
                cout << a << " " << b << endl;
                return 0;
            }
        }
    }


    return 0;
}

Measured memory_bytes: 4044
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

int main() {
    lli N, M;
    cin >> N >> M;
    if (N == 1 && M == 1) {
        cout << 1 << endl;
    } else if (N == 1 || M == 1) {
        cout << N * M  - 2 << endl;
    } else {
        cout << N * M - 2 * N - 2 * M + 4 << endl;
    }
    return 0;
}

Measured memory_bytes: 256
class UnionFind():
    def __init__(self, n):
        # 正==子: 根の頂点番号 / 負==根: 連結頂点数
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        #要素xが属するグループの根を返す
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        #要素xが属するグループと要素yが属するグループとを併合する
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        # 要素xが属するグループのサイズ（要素数）を返す
        return -self.parents[self.find(x)]

    def same(self, x, y):
        # 要素x, yが同じグループに属するかどうかを返す
        return self.find(x) == self.find(y)

    def members(self, x):
        # 要素xが属するグループに属する要素をリストで返す
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        # すべての根の要素をリストで返す
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        # グループの数を返す
        return len(self.roots())

    def all_group_members(self):
        # {ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
################################################################################

def examD():
    N, K, L = LI()
    ans = []
    ufk = UnionFind(N)
    ufl = UnionFind(N)
    for i in range(K):
        p, q = LI()
        ufk.union(p-1,q-1)
    for i in range(L):
        r, s = LI()
        ufl.union(r-1,s-1)
    d = defaultdict(int)
    for i in range(N):
        #各グループの根の組み合わせで記録
        d[(ufk.find(i), ufl.find(i))] += 1
    for i in range(N):
        print(d[(ufk.find(i), ufl.find(i))])


import sys
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()



Measured memory_bytes: 108764
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

H,W = two_int()
N=one_int()
A=many_int()

lists = []
for i,a in enumerate(A):
    lists += [i+1]*a

output=[]
for i in range(1,H+1):
    output.append(lists[W*(i-1):W*i])

for i,o in enumerate(output):
    temp = list(map(str,o))
    if i%2==0:
        print(" ".join(temp))
    else:
        print(" ".join(temp[::-1]))

Measured memory_bytes: 39664
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll,ll> P;
#define pb push_back
#define mp make_pair
#define fr(i,n) for(int i=0;i<n;i++)
#define Fr(i,n) for(int i=0;i++<n;)
#define ifr(i,n) for(int i=n-1;i>=0;i--)
#define iFr(i,n) for(int i=n;i>0;i--)

int main(){
  ll n,a,c[3]={};
  cin>>n;
  fr(i,n){
    cin>>a;
    if(a%4==0) c[2]++;
    else if(a%2) c[0]++;
    else c[1]++;
  }
  if(c[1]) cout<<(c[2]<c[0]?"No":"Yes")<<endl;
  else cout<<(c[2]+1<c[0]?"No":"Yes")<<endl;
}

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

using namespace std;

#define ll long long 
#define IOS ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define SIZE 1005

int main()
{
	//IOS
	int a, b, c;
	
	cin >> a >> b >> c;
	if(a+b+c >= 22) cout << "bust\n";
	else cout << "win\n";
	return 0;
}


Measured memory_bytes: 764
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define rep(i, n)for(long long i=0; i<n; i++)

//n^r
long long power(long long n, long long r){
  if(r == 0)
    return 1;
  long long ans = 1;
  while(r>0){
    if(r&1)
      ans = ans*n;
    n = n*n;
    r>>=1;
  }
  return ans;
}


int main(){
  long long N;
  cin >> N;
  //入力終わり
  
  //文字数
  int size = 1;
  long long tmp = 26;

  //文字数を計算する
  while(tmp < N){
    tmp = tmp + power(26, size+1);
    size++;
  }

  //size文字の文字列のうち，Nはnum番目である
  long long num = N;
  rep(i, size-1){
    num -= power(26, i+1);
  }
  num--;

  //26進数を考えることで，num文字目の文字列を構成する
  char array[size];
  rep(i, size){
    array[i] = (char)(num%26+'a') ;
    num /= 26;
  }

  //解答
  rep(i, size)
    cout << array[size-i-1];
  cout << endl;
  
  return 0;
}


Measured memory_bytes: 3640
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <algorithm> 
using namespace std;
int main() {
	string a, b;
	cin>>a>>b;
	a+=b;
	int result=0;
	stringstream ss;
	ss<<a;
	ss>>result;
	if(result==(int)sqrt(result)*(int)sqrt(result)) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}

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

#define Fast ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define FO freopen("in.txt", "r", stdin)
#define FC freopen("out.txt", "w", stdout)
#define aise cout<<"aise"<<endl
#define pb push_back
#define mp make_pair
#define ll long long
#define mset(v, a) memset(v, a, sizeof(v))
#define pll pair< ll, ll >
#define pdd pair< double, double >
#define ff first
#define ss second
#define pi acos(-1.0)
#define mxN 2097162
#define inf 1e9
#define MOD 1000000007

ll dig(ll x)
{

    ll ret=0;
    while(1)
    {

        if(x==0) break;
        ret++;
        x/=10;
    }
    return ret;
}

int main()
{

    ll n;
    cin >> n;
    ll ans=0;
    for(ll i =1; i <= n; i++)
    {

        ll z = dig(i);
        if(z%2) ans++;
    }
    cout << ans;
}


Measured memory_bytes: 256
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String c1 = sc.next();
        String c2 = sc.next();
        StringBuffer cc = new StringBuffer(c1);
        c1 = cc.reverse().toString();
        if(c1.equals(c2)){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }
    }
}

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

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

    string T;
    string ans; 

    cin >> T;

    for (int i = 0;i < T.size();i++){
       if(T[i] == '?') T[i] = 'D';
    }

    cout << T << endl;

    return 0;

}

Measured memory_bytes: 3772
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#pragma warning(disable:4996)
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
#define MOD 1000000007
using namespace std;


int main(int argc, char* argv[])
{
    long n;
    scanf("%ld", &n);

    vector<vector<long> > zz(2);
    zz[0].push_back(1);
    zz[1].push_back(1);

    if(n==1) {
        printf("Yes\n");
        printf("2\n");
        printf("1 1\n");
        printf("1 1\n");
        return 0;
    }

    long m=0;
    long k;
    for(k=2; k<=n*2; k++) {
        m+=(k-1);
        if(m>n) {
            break;
        }
        if(m==n) {
            printf("Yes\n");
            long i,j;
            printf("%ld\n", (long)zz.size());
            for(i=0; i<zz.size(); i++) {
                printf("%ld ", zz[i].size());
                for(j=0; j<zz[i].size(); j++) {
                    printf("%ld ", zz[i][j]);
                }
                printf("\n");
            }            
            return 0;
        }
        long sz=(long)zz.size();
        zz.push_back(vector<long>());
        long i;
        for(i=0; i<sz; i++) {
            zz[i].push_back(m+i+1);
            zz[sz].push_back(m+i+1);
        }
    }
    printf("No\n");

    return 0;
}


Measured memory_bytes: 3200
#include "bits/stdc++.h"
#define hhh cerr<<"hhh"<<endl
#define see(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}

const int maxn = 3e5+7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;



int main() {
    ll a; cin>>a;
    long double b; cin>>b;
    ll c=(b+1e-5)*100;
    printf("%lld\n", a*c/100);
}

Measured memory_bytes: 3796
// https://atcoder.jp/contests/dp/tasks/dp_n

#include <iostream>
#include <vector>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <limits>

using namespace std;

void helper(const vector<long long>& slimes, vector<vector<long long>>& dp, int l, int r) {
	if (l > r) return;
	if (l == r) {
		dp[l][r] = 0;
		return;
	}
	if (dp[l][r] != numeric_limits<long long>::max()) return;
	if (r - l == 1) {
		dp[l][r] = slimes[l] + slimes[r];
	} else {
		long long sum = 0;
		for (int i = l; i <= r; i++) sum += slimes[i];
		for (int i = l; i < r; i++) {
			helper(slimes, dp, l, i);
			helper(slimes, dp, i + 1, r);
			dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + sum);
		}
	}
}

int main (int argc, char* argv[]) {
	int n;
	cin >> n;

	vector<long long> slimes(n);
	for (int i = 0; i < n; i++) {
		cin >> slimes[i];
	}

	vector<vector<long long>> dp(n, vector<long long>(n, numeric_limits<long long>::max()));
	helper(slimes, dp, 0, n - 1);
	cout << dp[0][n - 1] << endl;
}


Measured memory_bytes: 1536
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <array>
#include <cmath>
#include <random>
#include <functional>
#include <climits>
using namespace std;
using lint=long long;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  char st,ba;
  cin>>n;
  vector<string> v;
  for(int i=0;i<n;i++){
    string s;
    cin>>s;
    st=s.at(0);
    if(i!=0)
      if(ba!=st){
        cout<<"No"<<endl;
        return 0;
      }
    auto fi=find(v.begin(),v.end(),s);
    if(i!=0)
      if(fi!=v.end()){
        cout<<"No"<<endl;
        return 0;
      }
    v.push_back(s);
    ba=s.at(s.length()-1);
  }
  cout<<"Yes"<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
vector<int> G[100000];
bool visited[100000][2],vis[100000];
int cnt=0,a[100000],b[100000];
typedef vector<int> vi;
vector<vi> group(100000);
void dfs(int v,int x){
	visited[v][x]=true;
	rep(i,G[v].size()){
		if(visited[G[v][i]][1-x]) continue;
		dfs(G[v][i],1-x);
	}
}
void dfs2(int v){
	group[cnt].pb(v);
	vis[v]=true;
	rep(i,G[v].size()){
		if(vis[G[v][i]]) continue;
		dfs2(G[v][i]);
	}
}
int cnta[100001];
vector<int> vca,vcm;
int dp[100001];
int main(){
	int V,E;
	cin>>V>>E;
	rep(i,E){
		int a,b;
		scanf("%d%d",&a,&b);
		G[a-1].pb(b-1);
		G[b-1].pb(a-1);
	}
	rep(i,V) if(!(visited[i][0]||visited[i][1])) dfs(i,0);
	rep(i,V) if(visited[i][0]&&visited[i][1]){
		printf("-1\n");
		return 0;
	}
	rep(i,V) if(!vis[i]) dfs2(i),cnt++;
	int ssum=0;
	rep(i,cnt){
		rep(j,group[i].size()){
			if(visited[group[i][j]][0]) a[i]++;
			else b[i]++;
		}
		if(a[i]<b[i]) swap(a[i],b[i]);
		ssum+=b[i];
//		cout<<a[i]<<" "<<b[i]<<endl;
	}
	rep(i,cnt) cnta[a[i]-b[i]]++;
	rep1(i,100000) if(cnta[i]>0){
		vca.pb(i);
		vcm.pb(cnta[i]);
	}
	int n=vca.size();
	rep(i,100001) dp[i]=-1;
	dp[0]=0;
	rep(i,n){
		rep(j,100001){
			if(dp[j]>=0){
				dp[j]=vcm[i];
			}else if(j<vca[i]||dp[j-vca[i]]<=0){
				dp[j]=-1;
			}else{
				dp[j]=dp[j-vca[i]]-1;
			}
		}
	}
	long long ans=0;
	rep(i,100001) if(dp[i]>=0){
		long long si=ssum+i;
		ans=max(ans,si*(V-si));
	}
	cout<<ans-E<<endl;
}

Measured memory_bytes: 10952
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const ll BIG=1e9;

int main(){
    int N; cin>>N;
    double K=double(2*N);
    double ans=0.5;
    ans+=(1/K);
    if(N%2==0) cout<<0.5<<endl;
    else cout<<setprecision(10)<<ans<<endl;

}



Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<map>
#include<numeric>
#include<queue>
#include<functional>
#include<cassert>
#include<set>
#define rep(a,b) for(int a=0;a<b;++a)
#define REP(a,b,c,d) for(int a=b;a<c;a+=d)
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
#define mod 1000000007
using namespace std;
using ll = long long;
using p_i = pair<int, int>;
int main() {
	int H, W; cin >> H >> W;
	rep(i, H) {
		rep(j, W) {
			string str;
			cin >> str;
			if (str == "snuke") cout << (char)('A' + j) << i + 1 << endl;
		}
	}
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll int64_t
#define rep(i,n) for(int64_t i=0;i<n;++i)
#define P pair<ll,ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
constexpr int64_t INF=(1ll<<60);
constexpr int64_t mod=1000000007;
constexpr double pi=3.14159265358979323846;
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
    ll n,m;cin>>n>>m;
    cout<<n*(n-1)/2+m*(m-1)/2<<endl;
    return 0;
}

Measured memory_bytes: 3532
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string W, L;
	int n = 0;

	getline(cin, W);

	for (unsigned int i = 0; i < W.size(); i++) {
		if (W[i] >= 'A' && W[i] <= 'Z')
			W[i] = tolower(W[i]);
	}

	W = " " + W + " ";

	getline(cin, L);

	while (L != "END_OF_TEXT") {
		for (unsigned int i = 0; i < L.size(); i++) {
			if (L[i] >= 'A' && L[i] <= 'Z')
				L[i] = tolower(L[i]);
		}

		L = " " + L + " ";

		unsigned int j = 0;

		j = L.find(W);

		while (j < L.size()) {
			n++;
			j = L.find(W, j + 1);
		}

		getline(cin, L);
	}

	cout << n << endl;

    return 0;
}

Measured memory_bytes: 3192
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define TRACE(x) cerr << #x << " = " << x << endl
#define REP(i, n) for (int i=0; i<n; i++)
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define _ << " " <<

typedef long long ll;
typedef pair<int, int> P;
#define X first
#define Y second

int main()
{
  int I, O, T, L, J, S, Z;
  cin >> I >> O >> T >> L >> J >> S >> Z;

  ll rje = 0;
  int kol = min(I, min(L, J));

  FOR(i, max(0, kol-5), kol+1) {    
    rje = max(rje, 6ll * i +
	      4ll * ((I - i) / 2) +
	      4ll * ((J - i) / 2) +
	      4ll * ((L - i) / 2));	      
  }

  rje += 2ll * O;
  printf("%lld\n", rje / 2);

  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<climits>
#include<vector>
#include<utility>
#include<cmath>
#include<cstring>
#include<map>
#include<sstream>
 
using namespace std;
 
int main(){
  int N,R,Ans;
    cin>>N>>R;

  if(N<10){
  	Ans = R + (100 * (10 - N));
  }else{
    Ans = R;
  }
  cout<<Ans<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define MAX_N 100010
#define rank dfasoifd
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
P par[MAX_N];
int rank[MAX_N];

void init(int n){
  for(int i=0;i<n;i++)par[i]=P(i,0),rank[i]=0;
}

P find(int x){
  if(par[x].f==x)return par[x];
  P r=find(par[x].f);
  return  par[x]=P(r.f,r.s+par[x].s);
}

void unite(int x,int y,ll c){
  P X=find(x),Y=find(y);
  x=X.f,y=Y.f;
  if(x==y)return;
  if(rank[x]<rank[y])par[x]=P(y,c+(-X.s+Y.s));
  else{
    par[y]=P(x,-c+(+X.s-Y.s));
    if(rank[x]==rank[y])rank[x]++;
  }
}

bool same(int x,int y){return find(x).f==find(y).f;}

int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(!n&&!m)break;
    init(n);  
    while(m--){
      char ch;
      int a,b,c;
      cin>>ch>>a>>b;a--,b--;
      if(ch=='!')cin>>c,unite(a,b,c);      
      else{
	if(same(a,b))printf("%lld\n",par[a].s-par[b].s);
	else printf("UNKNOWN\n");
      }
    }
  }
  return 0;
}

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

//typedef
typedef long long LL;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef set<int, greater<int> > setdownINT;
typedef set<long long, greater<long long> > setdownLL;
typedef priority_queue<int, vector<int>, greater<int> > priqueupINT;
typedef priority_queue<long long, vector<long long>, greater<long long> > priqueupLL;
//container utill
#define ALL(v) (v).begin(),(v).end()
#define sortdown(v) sort((v).begin(),(v).end()), reverse((v).begin(),(v).end())
#define PB(a) push_back(a)
#define MP make_pair
#define MT make_tuple
//constant
#define PI 3.141592653589793

int main(){
    //input
    string s;
    cin >> s;

    //exception
    if(s.length()<3){
        cout << 0 << endl;
        system("pause");
        return 0;
    }

    //calc
    LL ans=0;
    int num;
    LL BC;
    for(num=s.length()-2; num>=0; num--){
        if(s[num]=='B'&&s[num+1]=='C'){
            BC=1;
            num--;
            while(1){
                if(s[num]=='A'){
                    ans+=BC;
                    num--;
                }else if(num>=1){
                    if(s[num-1]=='B'&&s[num]=='C'){
                        BC++;
                        num--;
                        num--;
                    }else break;
                }else break;
            }   
        }
    }

    //output
    cout << ans << endl;
    system("pause");
    return 0;
}

Measured memory_bytes: 3800
#!/usr/bin/env python3
import sys


def solve(A: int, B: int):
    if 16 - 2 * min(A, B) >= 2 * abs(A - B):
        print("Yay!")
    else:
        print(":(")

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    solve(A, B)

if __name__ == '__main__':
    main()


Measured memory_bytes: 2940
N = int(input())
T = []
X = []
Y = []

for _ in range(N):
    t, x, y = map(int, input().split())
    T.append(t)
    X.append(x)
    Y.append(y)

current_t = 0
current_x = 0
current_y = 0

flag = True

for i in range(N):
    dist = (X[i] - current_x) + (Y[i] - current_y)
    time = T[i] - current_t
    if dist > time:
        flag = False
        break
    elif (time - dist)%2 != 0:
        flag = False
        break
    else:
        current_t = T[i]
        current_x = X[i]
        current_y = Y[i]
        
if flag == True:
    print("Yes")
else:
    print("No")

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

using namespace std;

#define sz(x) ((int) ((x).size()))
typedef long long ll;
typedef long double ld;

int n, k, s[3000], dp[3000][3000], sum[3000][3000];
vector<int> e[3000];

void dfs(int i = 0, int p = -1) {
  s[i] = 1;
  for (int j : e[i]) {
    if (j == p)
      continue;
    dfs(j, i);
    s[i] += s[j];
    for (int l = 0; l < k; l++)
      sum[i][l + 1] += dp[j][l];
  }
  sum[i][0] = s[i] - 1;
  for (int l = 0; 2 * l <= k; l++)
    dp[i][l] = sum[i][l];
  for (int l = k; 2 * l > k; l--)
    dp[i][l] = (s[i] == 1 ? 0 : n);
  for (int j : e[i]) {
    if (j == p)
      continue;
    for (int l = k; 2 * l > k; l--)
      dp[i][l] = min(dp[i][l], dp[j][l - 1] + sum[i][k - l] - (l == k ? s[j] : dp[j][k - l - 1]));
  }
  for (int l = 1; l <= k; l++)
    dp[i][l] = min(dp[i][l], dp[i][l - 1]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> n >> k;
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b, a--, b--;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  dfs();
  // for (int i = 0; i < n; i++)
  //   for (int j = 0; j <= k; j++)
  //     cout << i << " " << j << ": " << dp[i][j] << "\n";
  int r = n;
  for (int i = 0; i < n; i++)
    r = min(r, dp[i][k] + n - s[i]);
  cout << r << "\n";
}


Measured memory_bytes: 49664
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int lmax=sc.nextInt();
		int rmin=sc.nextInt();
		int l, r;
		for(int i=1;i<m;i++) {
			l=sc.nextInt();
			r=sc.nextInt();
			if(lmax<l)
				lmax=l;
			if(rmin>r)
				rmin=r;
		}
		if(rmin-lmax+1<0)
			System.out.print(0);
		else
			System.out.print(rmin-lmax+1);
		
		

	}

}

Measured memory_bytes: 63724
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class Main{
	static final int MAX = 10000;
	static final int INF = 1 << 21;
	static final int WHITE = 0;
	static final int GRAY = 1;
	static final int BLACK = 2;
	static int n;
	// 重み付き隣接リスト
	static  Map<Integer, List<int[]>> adj = new HashMap<Integer, List<int[]>>();
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();
		for(int i = 0; i < n; i++) {
			int u = scan.nextInt();
			int k = scan.nextInt();
			List<int[]> list = new ArrayList<int[]>();
			for(int j = 0; j < k; j++) {
				int v = scan.nextInt();
				int c = scan.nextInt();
				int []pair = {v, c};
				list.add(pair);
			}
			adj.put(u, list);
		}
		scan.close();
		dijkstra();
	}
	static void dijkstra() {
		Queue<int[]> pq =
				new PriorityQueue<int[]>(new MyComparator ());
		int[]color = new int[MAX];
		int[]d = new int[MAX];
		for(int i = 0; i < n; i++) {
			d[i] = INF;
			color[i] = WHITE;
		}
		d[0] = 0;
		int[]n0 = {0, 0};
		pq.add(n0);
		color[0] = 0;
		while(!pq.isEmpty()) {
			int[]f = pq.poll();
			int u = f[1];
			color[u] = BLACK;
			// 最小値を取り出し、それが最短でなければ無視
			if(d[u] < f[0]) continue;

			for(int j = 0; j < adj.get(u).size(); j++) {
				int[]t = adj.get(u).get(j);
				int v = t[0];
				if(color[v] == BLACK) continue;
				if(d[v] > d[u] + t[1]) {
					d[v] = d[u] + t[1];
					int[]p = {d[v], v};
					pq.add(p);
					color[v] = GRAY;
				}
			}
		}
		for(int i = 0; i < n; i++) {
			if(d[i] == INF) {
				System.out.println(i + " " + -1);
			}else {
				System.out.println(i + " " + d[i]);
			}
		}
	}
	static class MyComparator implements Comparator<int[]> {
	    @Override
	    public int compare (int[] arg0, int[] arg1) {
	        int x = arg0[0];
	        int y = arg1[0];

	        if (x < y) {
	            return -1;
	        } else if (x > y) {
	            return 1;
	        } else{
	            return 0;
	        }
	    }
	}
}


Measured memory_bytes: 288276
from collections import deque
def check(n, k, w_master, p):
    w = w_master.copy()
    for _ in range(k):
        weight_rem = p #残り積載可能重量
        while (len(w) > 0) and (weight_rem >= w[0]):
            weight_rem -= w.popleft()
    
        if len(w) == 0:
            return True

    return False

def main(n, k, w_master):
    amin=0
    amax= 10_000 *  100_000

    while amin+1!=amax:
        amid=(amin+amax)//2
        if(check(n, k, w_master, amid)):
            amax=amid
        else:
            amin=amid

    return amax 

if __name__ == "__main__":
    #input
    n, k = map(int, input().split())
    w_master = deque()
    for _ in range(n):
        w_master.append(int(input()))

    print(main(n, k, w_master))


Measured memory_bytes: 10720
from functools import*
@lru_cache(None)
def solve(n,m):
  if not n:return 1
  n%=m
  return-~solve(n,bin(n).count('1'))
n=int(input())
s=input()
a=int(s,2)
*x,=map(int,s)
h=sum(x)
try:a,b=a%(h-1),a%(h+1)
except:b=a%(h+1)
for i in range(n):
  if x[i]:
    if not h-1:
      print(0)
      continue
    x[i]=0
    t=(a-pow(2,~i+n,h-1))%(h-1)
  else:
    x[i]=1
    t=(b+pow(2,~i+n,h+1))%(h+1)
  r=solve(t,bin(t).count('1'))
  x[i]^=1
  print(r)

Measured memory_bytes: 15284
import sys

N=input()
A=map(int, sys.stdin.readline().split())
A.sort()
min_A=A[0]
max_A=A[-1]

L=[]
for i in range(1,N-1):
	v=A[i]
	if v<0:
		L.append( (max_A, v) )
		max_A-=v
	else:
		L.append( (min_A, v) )
		min_A-=v
else:
	L.append( (max_A,min_A) )
	print max_A-min_A
	for x,y in L:
		print x,y


Measured memory_bytes: 49452
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_ARRAY_MAX		200005									// 最大配列数

// 内部変数
static FILE *szpFpI;											// 入力
static int si1Array[D_ARRAY_MAX];								// 配列
static int siACnt;												// 配列数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// １行出力
int
fOutLine(
	char *pcpLine				// <I> １行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 実行メイン
int
fMain(
)
{
	int i;
	char lc1Buf[1024];

	// データ - 初期化
	memset(si1Array, 0, sizeof(si1Array));

	// 配列数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siACnt);

	// 配列 - 取得
	for (i = 0; i < siACnt - 1; i++) {
		int liVal;
		fscanf(szpFpI, "%d", &liVal);
		si1Array[liVal]++;
	}
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);

	// 出力
	for (i = 1; i <= siACnt; i++) {
		sprintf(lc1Buf, "%d\n", si1Array[i]);
		fOutLine(lc1Buf);
	}

	return 0;
}

// １回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	liRet = fMain();

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}



Measured memory_bytes: 2500
#include<bits/stdc++.h>
#define Rint register int
using namespace std;
typedef long long LL;
const int N = 603, mod = 1e9 + 7;
inline void add(int &a, int b){a += b; if(a >= mod) a -= mod;}
int n, cur, tmp, t, a[N], c[N], f[2][303][303];
int cnt[N];
int main(){
	scanf("%d", &n); n <<= 1;
	for(Rint i = 1;i <= n;i ++)
		scanf("%d", a + i);
	for(Rint i = 1;i <= n;i += 2){
		if(a[i] != -1 && a[i + 1] != -1) cnt[a[i]] = cnt[a[i + 1]] = 2;
		else if(a[i] == -1 && a[i + 1] == -1) ++ tmp;
		else if(a[i] != -1) cnt[a[i]] = 1;
		else cnt[a[i + 1]] = 1;
	}
	for(Rint i = 1;i <= n;i ++)
		if(cnt[i] == 1) c[++ t] = 1;
		else if(cnt[i] == 0) c[++ t] = 0;
	f[0][0][0] = 1;
	for(Rint i = t;i;i --){
		cur ^= 1;
		memset(f[cur], 0, sizeof f[cur]);
		for(Rint j = 0;j <= (n >> 1) && j <= t;j ++)
			for(Rint k = 0;k <= (n >> 1) && k <= t;k ++){
				if(c[i] == 1){
					add(f[cur][j][k + 1], f[cur ^ 1][j][k]);
					if(j) add(f[cur][j - 1][k], f[cur ^ 1][j][k]);
				} else if(c[i] == 0){
					add(f[cur][j + 1][k], f[cur ^ 1][j][k]);
					if(j) add(f[cur][j - 1][k], f[cur ^ 1][j][k]);
					if(k) add(f[cur][j][k - 1], (LL) f[cur ^ 1][j][k] * k % mod);
				}
			}
	}
	int ans = f[cur][0][0];
	for(Rint i = 1;i <= tmp;i ++)
		ans = (LL) ans * i % mod;
	printf("%d\n", ans);
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  rep(i, 5) {
    int a;
    cin >> a;
    if (a == 0) {
      cout << i+1 << endl;
      break;
    }
  }
  return 0;
}

Measured memory_bytes: 3692
#include<bits/stdc++.h>
using namespace std;
#define   vpll   vector<pll >
#define   all(x) x.begin(),x.end()
#define   present(c,x)    ((c).find(x) != (c).end())
#define   dbg(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){
  std::cerr << name << " = " << arg1 << "\n";
}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    std::cerr.write(names, comma - names) << " = " << arg1;
    ZZ(comma, args...);
}
#define   sz(a)  a.size()
#define   vll vector<int>
#define   pll pair<int,int>
#define   ff  first
#define   ss  second
#define   br  cout<<"\n";
#define   ld  long double
#define   int  long long int
const int  INF = 1e18+7;
const int  MAX = 1234567;
const int  MOD = 1000000007;



void solve(){
 	int n , k ;
 	cin >> k >> n ;
 	vll arr ;
 	int first ;
 	for ( int i = 0 ; i < n ; i++ ){
 		int dist ;
 		cin >> dist ;
 		arr.push_back(dist) ;
 	}   
 	vll offdist ;
 	for ( int i = 1 ; i < n ; i++ )
 		offdist.push_back ( arr[i] - arr[i-1] ) ;
 	int finaldist = k - arr[n-1] + arr[0] ;
 	offdist.push_back ( finaldist );
 	// for ( auto i : offdist )
 	// 	dbg(i) ;
 	sort ( all ( offdist) ) ;
 	int ans = 0 ;
 	for ( int i = 0 ; i < sz(offdist) - 1 ; i++ ) 
 		ans += offdist[i] ;
 	cout << ans ;
}
int32_t main(){
    int t;
    t=1;
    cin.sync_with_stdio(0), cin.tie(0);
    //cin>>t;
    while(t--){
        solve();
        br;
    }
    return 0;
}
 
 
 
 
 
 

Measured memory_bytes: 4848
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <numeric>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<((x))<<endl
#define fi first
#define se second

#define INF 2147483600

struct edge{
  int to, dist, cost;
  bool operator <(const edge &r) const {
    if(dist == r.dist) return cost > r.cost;
    else return dist > r.dist;
  }
};

int main(){
  int n,m;
  while(cin>>n>>m, n|m){
    vector<vector<edge> > vec(n, vector<edge>());
    vector<int> edge2cost(m);
    rep(i,m){
      int u,v,d,c;
      scanf("%d %d %d %d", &u, &v, &d, &c);
      u--;v--;
      vec[u].pb((edge{v,d,c}));
      vec[v].pb((edge{u,d,c}));
    }

    vector<int> d(n, INF);
    vector<int> c(n, INF);
    vector<int> prev(n);

    // 0????????????????????¢????????????
    // ?????????????????????????????¢?????¨????????°????????????????????????cost????°?????????????????????????
    priority_queue<edge> pq;
    pq.push(edge{0, 0, 0});
    d[0]=0;
    c[0]=0;
    while(!pq.empty()){
      edge e = pq.top(); pq.pop();
      if(d[e.to] < e.dist) continue;
      for(int i=0; i<vec[e.to].size(); i++){
        int nxt = vec[e.to][i].to;
        int nxtd = e.dist + vec[e.to][i].dist;
        int nxtc = vec[e.to][i].cost;
        if(nxtd > d[nxt]) continue;
        if(nxtd == d[nxt] && nxtc >= c[nxt]) continue;
        d[nxt] = nxtd;
        c[nxt] = nxtc;
        prev[nxt] = e.to;
        pq.push(edge{nxt, nxtd, nxtc});
      }
    }

    // ??¨????????????????????????????????????????????????????????????????????????
    vector<bool> checked(n, false);
    checked[0]=true;
    long res=0;
    repl(i,1,n){
      int idx=i;
      while(!checked[idx]){
        checked[idx]=true;
        res += c[idx];
        idx = prev[idx];
      }
    }
    cout << res << endl;
  }

  return 0;
}

Measured memory_bytes: 4316
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define ST string
#define BS bitset
#define PA pair<LL,LL>
#define VE vector
#define VL vector<LL>
#define VP vector<pair<LL,LL>>
#define VVL vector<vector<LL>>
#define PQ priority_queue
#define PQS priority_queue<LL,vector<LL>,greater<LL>>
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define EB emplace_back
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define BPL __builtin_popcountll
#define FOR(i,a,n) for(i=a;i<n;i++)
#define FORR(i,a,n) for(i=n-1;i>=a;i--)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) FOR(i,0,n)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a,n) *upper_bound(ALL(a),n)
#define LB(a,n) *lower_bound(ALL(a),n)
#define INF 1145141919810364364
#define PI 3.14159265358979
#define MOD 1000000007
//#define MOD 998244353
#define ERR 0.00000001
//#define ERR 0.00000000000001
#define NUM 200010
#define FAST cin.tie(0);ios::sync_with_stdio(false)

void Yn(LL a){if(a)printf("Yes\n");else printf("No\n");}
void YN(LL a){if(a)printf("YES\n");else printf("NO\n");}
LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;}
LL LBn(VL &v,LL a){LL mx=v.size(),mn=-1,md;while(mx-mn>1){md=(mx+mn)/2;if(v[md]<a)mn=md;else mx=md;}return mx;}
LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
LL LCM(LL a,LL b){return a*b/GCD(a,b);}
int lcmp(const void* a,const void* b){if(*(LL*)a>*(LL*)b)return 1;if(*(LL*)a<*(LL*)b)return -1;return 0;}
int lcmpr(const void* a,const void* b){if(*(LL*)a>*(LL*)b)return -1;if(*(LL*)a<*(LL*)b)return 1;return 0;}
int ccmp(const void* a,const void* b){return *(char*)a-*(char*)b;}
int ccmpr(const void* a,const void* b){return *(char*)b-*(char*)a;}
int scmp(const void* a,const void* b){return strcmp((char*)a,(char*)b);}
int scmpr(const void* a,const void* b){return strcmp((char*)b,(char*)a);}
LL mod(LL a,LL m){if(a<0)return a%m+m;else return a%m;}
LL DIV(LL a,LL d){LL m=MOD,x=1,y=0,k;while(m){k=d/m;d-=k*m;swap(m,d);x-=k*y;swap(x,y);}return mod(a*mod(x,MOD),MOD);}
LL POW(LL a,LL n){LL ans=1;while(n>0){if(n&1)ans=ans*a%MOD;a=a*a%MOD;n>>=1;}return ans;}
LL fact[NUM],finv[NUM],inv[NUM];
void comi(){LL i;fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[1]=1;FOR(i,2,NUM){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;}
bool cmps(pair<LL,LL> a,pair<LL,LL> b){if(a.second!=b.second)return a.second<b.second;return a.first<b.first;}
template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}

int main(){
  FAST;
  LL i,ans=0,N;
  cin>>N;
  FOR(i,1,N+1)if(i%3!=0&&i%5!=0)ans+=i;

  cout<<ans<<endl;
}


Measured memory_bytes: 3656
#include<iostream>
#include<string>

using namespace std;

int main()
{
	string S;
	string T;

	cin >> S >> T;

	if(S == T.substr(0,S.size()))
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}

	return 0;
}

Measured memory_bytes: 3636
N, M = map(int,input().split())
buy=0
pay=0

a = [list(map(int,input().split())) for i in range(N)]


a.sort()

for i in range(N):
	buy += a[i][1]
	pay += a[i][0]*a[i][1]
	if buy >= M:
		pay -= (buy-M)*a[i][0]
		break
print(pay)


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

int main(){
  int64_t N;
  cin>>N;
  
  vector<int64_t> A(N);
  for(int i=0;i<N;i++){
    cin>>A.at(i);
    if(A.at(i)==0){
      cout<<0<<endl;
      return 0;
    }
  }
  
  int64_t prod=1;  
  for(int i=0;i<N;i++){
    if(A.at(i)<=(int64_t)1e18/prod){
      prod *= A.at(i);
    }
    else{
      prod=-1;
      break;
    }
  }
  cout<<prod<<endl;
}

Measured memory_bytes: 4032