akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using v2 = vector<vector<int>>;

int D;
ll G;
ll M;

int main(){
	cin >> D >> G;
	M = 100000000000;
	v2 pc(D, vector<int>(3));
	rep(i, D){
		cin >> pc.at(i).at(0);
		cin >> pc.at(i).at(1);
	}

	rep(bit, (1<<D)){
		rep(i, D){
				pc.at(i).at(2) =0;
		}
		ll sum = 0;
		ll m = 0;
		rep(i, D){
			if(bit & (1<<i)){
				sum += (100*(i+1)*pc.at(i).at(0) + pc.at(i).at(1));
				m += pc.at(i).at(0);
				pc.at(i).at(2) = 1;
			}
		}
		if(sum >= G){
			M = min(M, m);
		}else{
			for(int i = D-1; i>=0; --i){
				if(pc.at(i).at(2) == 0){
					int k = pc.at(i).at(0);
					for(int j = 0; j < k; j++){
						sum += 100*(i+1);
						m++;
						if(sum >= G){
							M = min(M, m);
							break;
						}
					}
					if(sum >= G) break;
				}
			}
		}
	}
	cout << M << endl;

}


Measured memory_bytes: 256
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int num = 1;
    num <<= 10;
    vvint f(n,vint(10));
    REP(i,n){
        REP(j,10){
            cin >> f[i][j];
        }
    }
    vvint p(n,vint(11));
    REP(i,n){
        REP(j,11){
            cin >> p[i][j];
        }
    }
    int ans = -(INF)-1;
    for(int i = 1;i<num;++i){
        int now = i;
        vint op(10);
        REP(j,10){
            op[j]=now%2;
            now >>= 1;
        }
        int tmp = 0;
        REP(j,n){
            int cnt = 0;
            REP(k,10){
                if(op[k]==1&&f[j][k]==1) cnt++;
            }
            tmp+=p[j][cnt];
        }
        ans = max(ans,tmp);
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
A, B, C = map(int, input().split())

a, b, c = A//2, B//2, C//2
r1, r2, r3  = a*B*C, A*b*C, A*B*c
b1, b2, b3 = (A-a)*B*C, A*(B-b)*C, A*B*(C-c)

ans = min(abs(r1-b1), abs(r2-b2), abs(r3-b3))
print(ans)

Measured memory_bytes: 3060
def bridge_break(reqs):
    reqs.sort()
    ps = 0
    out = 0

    for req in reqs:
        if req[1] >= ps:
            out += 1
            ps = req[0]

    return out


n, m = map(int, raw_input().split())
reqs = []
for _ in range(m):
    st, end = map(int, raw_input().split())
    reqs.append((end, st))

print bridge_break(reqs)

Measured memory_bytes: 18436
h,w = map(int,input().split())
if h%3 == 0:
    m1 = 0
else:
    m1 = w
if w%3 == 0:
    m2 = 0
else:
    m2 = h
m3 = m4 = h*w
for i in range(1,h):
    m3 = min(m3, max(i*w, (h-i)*(w//2), (h-i)*(w-w//2)) - min(i*w, (h-i)*(w//2), (h-i)*(w-w//2)))
for i in range(1,w):
    m4 = min(m4, max(i*h, (w-i)*(h//2), (w-i)*(h-h//2)) - min(i*h, (w-i)*(h//2), (w-i)*(h-h//2)))
print(min(m1, m2, m3, m4))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
int getScore(char m, int R, int S, int P){
  if(m=='r') return R;
  else if(m=='s') return S;
  else if(m=='p') return P;
  else return 0;
}
char getMyHand(int i, char t, string T, string M, int K){
  if(i>=K && T[i]==T[i-K] && M[i-K]!='a') return 'a';
  if(t=='r') return 'p';
  else if(t=='s') return 'r';
  else return 's';
}
int main(){
  int N, K, R, S, P; cin >> N >> K >> R >> S >> P;
  string T; cin >> T;
  int score=0;
  string M;
  for(int i=0; i<N; i++){
    char t=T[i];
    M+=getMyHand(i, t, T, M, K);
    score+=getScore(M[i], R, S, P);
  }
  cout << score;
  return 0;
}

Measured memory_bytes: 816
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll a[100010];  
ll d[100010];  

int main(){
    ll n;
    cin >> n;
    ll tot = 0;
    rep(i, n) {
        cin >> a[i];
        tot += a[i];
    }
    if (tot % (n*(n+1)/2) != 0) {
        cout << "NO" << endl;
        return 0;
    }

    ll k = tot / (n*(n+1)/2);

    rep(i, n-1) {
        d[i] = a[i+1] - a[i];
        d[i] -= k;
    }
    d[n-1] = a[0] - a[n-1];
    d[n-1] -= k;

    bool f = 1;
    ll tmp = k;
    rep(i, n) f &= (abs(d[i])%n == 0), tmp -= abs(d[i])/n;
    f &= (tmp == 0);


    if (f) cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


Measured memory_bytes: 5188
#include<stdio.h>
int main(void){
char kazu[1001];
int kazu2[1001];
	int t,wa=0;
	for(;;){
		scanf("%s",kazu);
		if(kazu[0]=='0'){
			break;
		}
		for(t=0;kazu[t]!='\n'&&kazu[t]!='\0';t++){
			wa+=kazu[t]-'0';
			
		}

		printf("%d\n",wa);
		wa=0;
	}
	return 0;
}

Measured memory_bytes: 2756
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using boost::rational;
using boost::multiprecision::cpp_int;
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
#define fs first
#define sc second

const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
 
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
 

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}


// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;
 
int64_t N, K;
vector<ll> A;
vector<ll> F;
 
bool check(int64_t X){
    ll sum = 0;
    for(int i=0; i<N; i++){
        ll ok = X/F[i];
        if(ok < A[i]) sum += A[i] - ok;
    }
    return (sum <= K);
}
 
int main(){
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cin >> N >> K;
    A.resize(N);
    F.resize(N);
    for(int i=0; i<N; i++) cin >> A[i];
    for(int i=0; i<N; i++) cin >> F[i];
    sort(all(A));
    sort(rall(F));
    int64_t ok = 1e13, ng = -1;
    while(ok-ng>1){
        int64_t mid = (ok+ng)/2;
        (check(mid) ? ok : ng) = mid;
    }
    cout << ok << endl;
}


Measured memory_bytes: 3456
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

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

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

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }


int main() {
    int k, a, b;
    cin >> k >> a >> b;
    repr(i, a, b + 1) {
        if (i % k == 0) {
            cout << "OK" << endl;
            return 0;
        }
    }
    cout << "NG" << endl;
}

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

typedef long long ll;

int main()
{
  int n,a,b;
  scanf("%d%d%d",&n,&a,&b);
  a--;
  b--;
  static ll X[100000],Y[100000];
  for(int i=0;i<n;i++){
    ll x,y;
    scanf("%lld%lld",&x,&y);
    X[i]=x+y;
    Y[i]=x-y;
  }
  typedef set<pair<pair<ll,ll>,int> > pset;
  pset S1,S2;
  for(int i=0;i<n;i++){
    if(i==a||i==b){
      continue;
    }
    S1.insert(make_pair(make_pair(X[i],Y[i]),i));
    S2.insert(make_pair(make_pair(Y[i],X[i]),i));
  }
  long long d=max(abs(X[a]-X[b]),abs(Y[a]-Y[b]));
  queue<int> Q;
  Q.push(a);
  Q.push(b);
  static bool B[100000]={0};
  B[a]=B[b]=1;
  while(!Q.empty()){
    int i=Q.front();
    Q.pop();
    for(pset::iterator it=S1.lower_bound(make_pair(make_pair(X[i]-d,Y[i]-d),-1));it!=S1.end()&&it->first<=make_pair(X[i]-d,Y[i]+d);){
      int j=it->second;
      if(!B[j]){
	B[j]=1;
	Q.push(j);
      }
      pset::iterator temp=it;
      it++;
      S1.erase(temp);
    }
    for(pset::iterator it=S1.lower_bound(make_pair(make_pair(X[i]+d,Y[i]-d),-1));it!=S1.end()&&it->first<=make_pair(X[i]+d,Y[i]+d);){
      int j=it->second;
      if(!B[j]){
	B[j]=1;
	Q.push(j);
      }
      pset::iterator temp=it;
      it++;
      S1.erase(temp);
    }
    for(pset::iterator it=S2.lower_bound(make_pair(make_pair(Y[i]+d,X[i]-d),-1));it!=S2.end()&&it->first<=make_pair(Y[i]+d,X[i]+d);){
      int j=it->second;
      if(!B[j]){
	B[j]=1;
	Q.push(j);
      }
      pset::iterator temp=it;
      it++;
      S2.erase(temp);
    }
    for(pset::iterator it=S2.lower_bound(make_pair(make_pair(Y[i]-d,X[i]-d),-1));it!=S2.end()&&it->first<=make_pair(Y[i]-d,X[i]+d);){
      int j=it->second;
      if(!B[j]){
	B[j]=1;
	Q.push(j);
      }
      pset::iterator temp=it;
      it++;
      S2.erase(temp);
    }
  }
  static pair<ll,ll> P1[100000],P2[100000];
  for(int i=0;i<n;i++){
    P1[i]=make_pair(X[i],Y[i]);
    P2[i]=make_pair(Y[i],X[i]);
  }
  sort(P1,P1+n);
  sort(P2,P2+n);
  ll ans=0;
  for(int i=0;i<n;i++){
    if(!B[i]){
      continue;
    }
    ans+=upper_bound(P1,P1+n,make_pair(X[i]-d,Y[i]+d))-lower_bound(P1,P1+n,make_pair(X[i]-d,Y[i]-d));
    ans+=upper_bound(P1,P1+n,make_pair(X[i]+d,Y[i]+d))-lower_bound(P1,P1+n,make_pair(X[i]+d,Y[i]-d));
    ans+=upper_bound(P2,P2+n,make_pair(Y[i]-d,X[i]+d-1))-lower_bound(P2,P2+n,make_pair(Y[i]-d,X[i]-d+1));
    ans+=upper_bound(P2,P2+n,make_pair(Y[i]+d,X[i]+d-1))-lower_bound(P2,P2+n,make_pair(Y[i]+d,X[i]-d+1));
  }
  printf("%lld\n",ans/2);
  return 0;
}


Measured memory_bytes: 17920
<?php
fscanf(STDIN, "%d %d", $n, $k);
$ps=explode(' ',trim(fgets(STDIN)));
$max=0;
$ex=[];
$sum=0;
for($i=0;$i<$n;$i++){
  $x = $ps[$i];
  if (!isset($ex[$x])) $ex[$x] = (1 + $x) /2;
  $sum += $ex[$x];
  if ($i>=$k) {
    $sum -= $ex[$ps[$i-$k]];
    if ($max<$sum) $max=$sum;
  } else {
    $max=$sum;
  }
}
echo $max;

Measured memory_bytes: 18932
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *
from copy import *

N, M = map(int, input().split())
num, red = [1] * (N+1), [0]+[1]+[0]*(N-1)
for i in range(M):
    x, y = map(int, input().split())
    red[y] |= red[x]
    num[x] -= 1
    num[y] += 1
    if num[x] == 0:
        red[x] = 0
print(sum(red))


Measured memory_bytes: 5876
import sys
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9 + 7
N = int(input())
S = input().rstrip()

dp = np.ones(N,dtype=np.int64) # 下側で余っているものの個数

for s in S:
    incr = dp.cumsum()[:-1] % MOD
    if s == '<':
        dp = incr
        continue
    decr = (dp.sum() - incr) % MOD
    dp = decr

answer = dp[0]
print(answer)

Measured memory_bytes: 21528
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::HashMap;
#[allow(unused_imports)]
use std::collections::VecDeque;
#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::mem::*;
#[allow(unused_imports)]
use std::str::*;
#[allow(unused_imports)]
use std::usize;

#[allow(unused_macros)]
macro_rules! read_cols {
    ($($t:ty),+) => {{
        let mut line = String::new();
        stdin().read_line(&mut line).unwrap();

        let mut it = line.trim()
            .split_whitespace();

        ($(
            it.next().unwrap().parse::<$t>().ok().unwrap()
        ),+)
    }}
}

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let mut line = String::new();
    stdin().read_line(&mut line).unwrap();
    line.trim().to_string().parse().ok().unwrap()
}

#[allow(dead_code)]
fn read_vec<T: FromStr>() -> Vec<T> {
    let mut line = String::new();
    stdin().read_line(&mut line).unwrap();

    line.trim()
        .split_whitespace()
        .map(|s| s.parse().ok().unwrap())
        .collect()
}

const M: i64 = 998244353;

fn main() {
    let (n, k) = read_cols!(usize, usize);

    let lr = (0..k).map(|_| read_cols!(usize, usize)).collect::<Vec<_>>();

    let mut d = vec![0; n];

    let mut c = 0;
    d[0] = 1;
    d[1] = M - 1;
    for i in 0..n {
        c += d[i];
        c %= M;
        for (l, r) in &lr {
            if i + l >= n {
                continue;
            }

            d[i + l] += c;
            d[i + l] %= M;
            if i + r + 1 < n {
                d[i + r + 1] += M - c;
                d[i + r + 1] %= M;
            }
        }
    }

    println!("{}", c);
}


Measured memory_bytes: 3564
N = int(input())
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())

xs = [A, B, C, D, E]
min_value = min(xs)
min_index = xs.index(min_value)

print(4 + (N + min_value - 1) // min_value)

Measured memory_bytes: 38384
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

bool check(vector<int>& a){
    for(int x : a){
        if(x != 0){
            return false;
        }
    }
    return true;
}

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

    int n;
    cin >> n;
    vector<int> h(n);
    rep(i, n) cin >> h[i];

    int ans = 0;
    while(not check(h)){
        int i = 0;
        while(h[i] == 0){
            i++;
        }

        while(i < n && h[i] > 0){
            h[i]--;
            i++;
        }
        ans++;
    }
    cout << ans << endl;


    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;

ll binpow(ll a, ll b) {
    if (b == 0)
    {
        return 1;
    }
    ll res = binpow(a, b / 2);
    res*=res;
    if (b % 2)
    {
        return res * a;
    }
    return res;
}

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

string to_upper(string a)
{
    for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
    return a;
}
 
string to_lower(string a)
{
    for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
    return a;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n,k,d,x;
    cin>>n>>k;
    set<int> s;
    while (k--)
    {
        cin>>d;
        while (d--)
        {
            cin>>x;
            s.insert(x);
        }
    }
    cout<<n-sz(s);
    return 0;
}

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

#define INF INT_MAX
#define UINF UINT_MAX
#define MOD 1000000007
#define REP(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef std::pair<int,int> pii;

using namespace std;

int main() {
    int a,b;
    cin >> a >> b;

    int ans = a - b*2;
    if(ans < 0)ans = 0;
    
    cout << ans << endl;
    return 0;
}

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

vector<int> a;

map<int,int> mp;

int dp(int i){
    if(i == 0){
        return 0;
    }
    if(i==1){
        return abs(a[1]-a[0]);
    }
  	if(mp.count(i)){
      return mp[i];
    }
    mp[i] = min(abs(a[i]-a[i-2])+dp(i-2) , abs(a[i]-a[i-1]) +dp(i-1));
  	return mp[i];
}

int main(){


    int n;
    cin>>n;
    a.resize(n);
  	for(int& i : a)cin>>i;
    cout<<dp(n-1);
}

Measured memory_bytes: 13184
#include <iostream>
#include <cstdio>
using namespace std;
int N;
int x[110],y[110];
int main()
{
    cin >> N;
    int sumy = 0, sumx=0, ans = 0; 
    for(int i = 0 ; i < N; ++i) cin >> x[i];
    for(int i = 0 ; i < N; ++i){
        cin >> y[i];
        sumy+=y[i];
    }
    for(int i =0; i < N; ++i)
    {
      sumx+=x[i];
      if( (sumy + sumx) > ans) ans = sumy + sumx;
      sumy-=y[i];
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
class Array
  def swap!(a,b)
    self[a], self[b] = self[b], self[a]
    self
  end
end

n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)

count = 0

(n-1).times do |i|
  if arr[i] == i+1
    arr.swap!(i,i+1)
    count += 1
  end
end

if arr[n-1] == n
  arr.swap!(n-2,n-1)
  count +=1
end

puts count


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

int cnt1[26], cnt2[4], cnt3[2];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    char A;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A;
            cnt1[A - 'a']++;
        }
    }
    for (int i = 0; i < 26; i++) {
        cnt1[i] %= 4;
        cnt2[cnt1[i]]++;
    }
    cnt2[1] += cnt2[3];
    cnt2[2] += cnt2[3];
    if (cnt2[2] * 2 <= (H / 2) * 2 * (W % 2) + (W / 2) * 2 * (H % 2)) {
        if ((H % 2 && W % 2 && cnt2[1] == 1) || ((H % 2 == 0 || W % 2 == 0) && cnt2[1] == 0)) {
            cout << "Yes" << endl;
        }
        else cout << "No" << endl;
        return 0;
    }
    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

class Main{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        
        int H = sc.nextInt();
        int W = sc.nextInt();
        char[][] c = new char[H][W];

        for(int i=0; i<H; i++){
            c[i] = sc.next().toCharArray();
        }

        int[][] L = new int[H][W];
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(j==0){
                    if(c[i][j]=='#'){
                        L[i][j] = 0;
                    }
                    else{
                        L[i][j] = 1;
                    }
                }
                else{
                    if(c[i][j]=='#'){
                        L[i][j] = 0;
                    }
                    else{
                        L[i][j] = L[i][j - 1] + 1;
                    } 
                }
            }
        }

        int[][] R = new int[H][W];
        for(int i=0; i<H; i++){
            for(int j=W-1; j>=0; j--){
                if(j==W-1){
                    if(c[i][j]=='#'){
                        R[i][j] = 0;
                    }
                    else{
                        R[i][j] = 1;
                    }
                }
                else{
                    if(c[i][j]=='#'){
                        R[i][j] = 0;
                    }
                    else{
                        R[i][j] = R[i][j + 1] + 1;
                    }
                }
            }
        }

        int[][] U = new int[H][W];
        for(int i=0; i<W; i++){
            for(int j=0; j<H; j++){
                if(j==0){
                    if(c[j][i]=='#'){
                        U[j][i] = 0;
                    }
                    else{
                        U[j][i] = 1;
                    }
                }
                else{
                    if(c[j][i]=='#'){
                        U[j][i] = 0;
                    }
                    else{
                        U[j][i] = U[j - 1][i] + 1;
                    }
                }
            }
        }
        
        int[][] D = new int[H][W];
        for(int i=0; i<W; i++){
            for(int j=H-1; j>=0; j--){
                if(j==H-1){
                    if(c[j][i]=='#'){
                        D[j][i] = 0;
                    }
                    else{
                        D[j][i] = 1;
                    }
                }
                else{
                    if(c[j][i]=='#'){
                        D[j][i] = 0;
                    }
                    else{
                        D[j][i] = D[j + 1][i] + 1;
                    }
                }
            }
        }
        int ans = 0;
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                int x = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3;
                ans = Math.max(ans, x);
            }
        }
        System.out.println(ans);
    }
}

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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<(n);i++)
#define REP2(i,d,n) for(int i=(d);i<(n);i++)
#define RREP(i,n) for(int i=(n);i>=0;i--)
#define CLR(a)      memset((a),0,sizeof(a))
#define MCLR(a)     memset((a),-1,sizeof(a))
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int,int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int main(){
    LL n, m;
    cin >> n >> m;
    if( m / 2 <= n){
        cout << m / 2 << endl;
        return 0;
    }

    LL result = n;
    m -= result * 2;
    result += m / 4;
    cout << result << endl;
}


Measured memory_bytes: 256
#!/usr/bin/env python

import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

MOD = 10 ** 9 + 7
n, k = map(int, raw_input().split())

def fact(N):
    if N <= 1:
        return 1
    return fact(N - 1) * N

print (fact(n + k - 1) / fact(k - 1) / fact(n)) % MOD


Measured memory_bytes: 6524
#include<cstdio>
#include <string>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace  std;
int main( )
{
    string i, j;
    int N;
    int t[2]= {0}, x[2]= {0}, y[2]= {0};
    cin >> N;
    int id = 0;
    bool ans = true;
    while(N--)
    {
        id = 1 - id;
        cin >> t[id] >>  x[id] >> y[id];
        int dt = t[id] - t[1 - id];
        int dx = x[id] - x[1 - id];
        int dy = y[id] - y[1 - id];
        int dxy = dx + dy;
        if((dt  - dxy) % 2)
            ans = false;
        if(dt + dxy < 0 || dt - dxy < 0)
            ans = false;
        if(abs(x[id] - x[1 - id]) > dt || abs(y[id] - y[1 - id]) > dt)
            ans = false;
    }
    printf("%s\n", ans ? "Yes" : "No");

    return 0;
}


Measured memory_bytes: 256
import math
dishes = [int(input()) for _ in range(5)]
dishes_mod = 10
for i in range(5) :
  if(dishes[i]%10 != 0) : dishes_mod = min(dishes_mod, dishes[i]%10)
print(sum(list(map(lambda x: math.ceil(x), list(map(lambda x:x/10, dishes)))))*10-10+dishes_mod)

Measured memory_bytes: 3060
#!/usr/bin/ruby
$cnt = 0

def merge(a, left, mid, right)
  # mergeを指示された範囲を配列として取得
  # a[基準, 基準から何個とるか]
  l = a[left, (mid - left)]
  r = a[mid, (right - mid)]
  # loopが綺麗に回るように範囲超えの値を挿入
  l << 10**10
  r << 10**10
  # l, rの配列の先頭から小さい数字を詰めていく
  li, ri = 0, 0 # l, rの配列用のindex
  left.upto(right - 1) do | i | # aに挿入するためのindex
    $cnt += 1
    if l[li] <= r[ri]
      a[i] = l[li]
      li += 1
    else
      a[i] = r[ri]
      ri += 1
    end
  end
end

def merge_sort(a, left, right)
  if left + 1 < right
    mid = (left + right) / 2
    merge_sort(a, left, mid)
    merge_sort(a, mid, right)
    merge(a, left, mid, right)
  end
end

#
n = gets.to_i
a = gets.split.map(&:to_i)
merge_sort(a, 0, n)
puts a.join(' ')
puts $cnt


Measured memory_bytes: 72240
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N = 0, K = 0;
  cin >> N >> K;
  
  int num = 1;
  for (int i = 0; i < N; i++) {
    if ((num * 2) <= (num + K)) {
      num *= 2;
    } else {
      num += K;
    }
  }
  cout << num << endl;
}

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

using namespace std;

const int maxV = 2000;
const int dim = maxV * 2 + 10;

const int mod = 1e9 + 7;

int n, ans;
int fact[2*dim], ifact[2*dim];
int dp[dim][dim], cnt[dim][dim];

int sum(int a, int b) {
    a += b; if (a >= mod) a -= mod; return a;
}

int diff(int a, int b) {
    a -= b; if (a < 0) a += mod; return a;
}

int mul(int a, int b) {
    return (1LL * a * b) % mod;
}

int lgpow(int a, int b) {
    int res = 1;
    for (int i = 0; (1 << i) <= b; ++i) {
        if (b & (1 << i))
            res = mul(res, a);
        a = mul(a, a);
    }
    return res;
}

int f(int x, int y) {
    int res = fact[x+y];
    res = mul(res, ifact[x]);
    res = mul(res, ifact[y]);
    return res;
}

int main()
{
  //  freopen("input","r",stdin);
 //   freopen("output","w",stdout);

    fact[0] = ifact[0] = 1;
    for (int i = 1; i <= maxV * 4; ++i)
        fact[i] = mul(fact[i-1], i),
        ifact[i] = mul(ifact[i-1], lgpow(i, mod - 2));

    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int a, b; scanf("%d %d", &a, &b);
        ans = diff(ans, f(a + a, b + b));
        dp[maxV-a][maxV-b]++;
        cnt[maxV+a][maxV+b]++;
    }

    for (int i = 0; i <= maxV * 2; ++i)
        for (int j = 0; j <= maxV * 2; ++j) {
            if (i) dp[i][j] = sum(dp[i][j], dp[i-1][j]);
            if (j) dp[i][j] = sum(dp[i][j], dp[i][j-1]);
            if (cnt[i][j]) ans = sum(ans, mul(cnt[i][j], dp[i][j]));
        }

    printf("%d\n", mul(ans, lgpow(2, mod - 2)));

    return 0;
}


Measured memory_bytes: 86272
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
vi a;
const int N = 1e6+1;
int o[N];
int main()
{
	int n,i,j;
	scanf("%d",&n);
	for(i=0;i<N;i++)	o[i] = 0;
	int ans = 0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&j);
		a.push_back(j);	
	}
	sort(a.begin(),a.end());
	for(i=0;i<n;i++)
	{
		if(o[a[i]]<=1)
		{
			for(j=a[i];j<N;j+=a[i])
			{
				o[j]++;
			}
		}
	}
	for(i=0;i<n;i++)	if(o[a[i]]==1)		ans++;
	printf("%d",ans);
}


Measured memory_bytes: 8252
n ,a, b = map(int, input().split())
dis = b - a
if dis % 2 == 0:
    print(dis // 2)
else:
    shiftR = (n-b)
    shiftL = (a-1)
    if shiftL < shiftR:
        print(shiftL +1+ (dis - 1)//2)
    else:
        print(shiftR +1+ (dis-1) //2)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef vector<int> VI;
ll mm=1000000000;ll MM=mm+7;
#define rep(i, n) for(int i=0;i<n;i++)
#define PI 3.141592653589793

int main(){
   int a,b;
   cin >> a >> b;
   if(a+b>=10)cout << "error" <<endl;
   else cout << a+b << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (long long i = (a); i <= (b); i++)
#define FORD(i,a,b) for (long long i = (a); i >= (b); i--)
#define REP(i,a) FOR(i,0,(long long)(a)-1)
#define reset(a,b) memset(a,b,sizeof(a))
#define all(x) x.begin(), x.end()
#define uni(x) x.erase(unique(all(x)), x.end());
#define BUG(x) cout << #x << " = " << x << endl
#define BUGP(x) cout << #x << " = " << x._1 << ", " << x._2 << endl
#define PR(x,a,b) {cout << #x << " = "; FOR (_,a,b) cout << x[_] << ' '; cout << endl;}
#define CON(x) {cout << #x << " = "; for(auto _i:x) cout << _i << ' '; cout << endl;}
#define mod 1000000007
#define pi acos(-1)
#define eps 1e-8
#define pb push_back
#define sqr(x) (x) * (x)
#define _1 first
#define _2 second

int d, n;

int main() {
  ios::sync_with_stdio(false);
  cin >> d >> n;
  if (n == 100) cout << 101;
  else cout << n;
  REP (i, d) cout << "00";
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <utility>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cmath>
#include <deque>
#include <bitset>
#define ll long long
#define PI 3.1415926535897932384626433832795
#define read(x) scanf("%d",&x);
#define readll(x) cin>>x;
#define FOR(x,a,b) for(int x=a;x<b;x++)
#define MP make_pair
#define PB push_back
#define pii pair<int,int>
#define readN(N,X) for(int i=0;i<N;i++) cin>>X[i];
#define pff pair<double,double>
using namespace std;

ll N;
ll A[55];

int main() {
	cin>>N;
	ll res = 0;
	readN(N,A);
	bool ganti = 1;
	while(ganti){
		ganti = 0;
		ll naik = 0;
		FOR(i,0,N){
			if (A[i] < N) continue;
			ganti = 1;
			ll bagi = A[i]/N;
			A[i] -= (N + 1) * bagi;
			//cout<<bagi<<endl;
			res += bagi;
			naik += bagi;
		}
		FOR(i,0,N) A[i] += naik;
	}
	cout<<res<<endl;
	
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <set>

int main() {
  size_t N;
  std::cin >> N;

  std::set<std::string> seen;
  std::string last = "^";
  for (size_t i = 0; i < N; ++i) {
    std::string W;
    std::cin >> W;

    if (seen.count(W))
      return (std::cout << "No" << std::endl), 0;
    if (i > 0 && last.back() != W.front())
      return (std::cout << "No" << std::endl), 0;

    last = W;
    seen.insert(W);
  }
  std::cout << "Yes" << std::endl;
}

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

int main(){
  while(1){
    int h, w;
    scanf("%d %d", &h, &w);
    if(h==0 && w==0)
      break;
    else{
      char s[100][100];
      int i;
      for(i=0;i<h;i++){	
	scanf("%s", s[i]);
      }
      int x=0;
      int y=0;
      int xx[10000]={};
      int yy[10000]={};
      int j=0;
      while(1){
	xx[j]=x;
	yy[j]=y;
	j++;
	if(s[x][y] == '>')
	  y++;
	if(s[x][y] == '<')
	  y--;
	if(s[x][y] == '^')
	  x--;
	if(s[x][y] == 'v')
	  x++;	
	if(s[x][y] == '.')
	  {printf("%d %d\n", y, x);break;}
	
	int k;
	int flag=0;
	for(k=0;k<10000;k++){
	  if(xx[k]==x && yy[k]==y){flag++;}
	}
	if(flag!=0)
	  {printf("LOOP\n");break;}
      }
    }
  }
  return 0;
}

Measured memory_bytes: 2692
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

//typedef pair<int,int> P;

int main(){
	int N;
	scanf("%d",&N);

	int ans[101];
	for(int i=1;i<=100;i++){
		int tmp = 0;
		int t = i;
		while(t%2==0){
			t = t/2;
			tmp++;
		}
		ans[i] = tmp;
	}

	int max = 1;
	for(int i=1;i<=N;i++){
		if(ans[max]<ans[i]) max = i;
	}
	printf("%d\n",max);

	return 0;
}


Measured memory_bytes: 256
import java.io.InputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
	public static void main(String args[]) {
		MyInput in = new MyInput();
		int hon = in.nextInt();
		int kusi[] = new int[2*hon];
		int ans = 0;
		for(int i = 0; i < 2*hon; i++) {
			kusi[i] = in.nextInt();	
		}
		Arrays.sort(kusi);
		for(int i = 0; i < hon; i++) {
			ans += kusi[2*hon - 2 - (2 * i)];	
		}
		System.out.println(ans);

	}
}

class MyInput {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr= 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if(ptr < buflen) {
			return true;
		}else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch(IOException e) {
				e.printStackTrace();
			}
			if(buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	public int readByte() { 
		if (hasNextByte()) return buffer[ptr++];
		else return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <=126;
	}

	private void skipUnprintable() {
		while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public String next() {
		if(!hasNext()) throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if(!hasNext()) throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if(b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while(true) {
			if('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			}else if(b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			}else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

	public int nextInt() {
		long nl = nextLong();
		if(nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
		return (int) nl;
	}

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

Measured memory_bytes: 8020
#include <bits/stdc++.h>
using namespace std;
int N, bit[100010]; 
void add(int a, int b){
	int x = a; 
	while(x <= N){
		bit[x] += b; x += x & -x; 
	}
}
int sum(int a){
	int res = 0; 
	int x = a; 
	while(x > 0){
		res += bit[x]; x -= x & -x;
	}
	return res; 
}
int main(){
	int A[3][100010]; 
	cin >> N; 
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < N; j++){
			cin >> A[i][j]; 
		}
	}
	int B[100010] = {}; 
	long long revE = 0; long long revO = 0; 
	bool R[100010] = {}; 
	bool impo = false; 
	for(int i = 0; i < N; i++){
		if(A[1][i] % 3 != 2){
			impo = true; 
		}
		if(A[0][i] - A[1][i] == 1 && A[1][i] - A[2][i] == 1){
			B[i] = (A[1][i] - 2) / 3; R[i] = true; 
			if(i % 2) revO ++; 
			else revE ++; 
			if(i % 2 != B[i] % 2) impo = true; 
		}
		else if(A[0][i] - A[1][i] == -1 && A[1][i] - A[2][i] == -1){
			B[i] = (A[1][i] - 2) / 3; 
			if(i % 2 != B[i] % 2) impo = true; 
		}
		else{
			impo = true; 
		}
	}
	if(impo){
		cout << "No" << endl; return 0; 
	}
	long long invE = 0; long long invO = 0; 
	int E[50010], O[50010]; 
	for(int i = 0; i < N; i++){
		if(i % 2) O[(i+1)/2] = (B[i] + 1)/2;
		else E[(i+2)/2] = (B[i] + 2)/2; 
	}
	for(int i = 1; i <= (N+1)/2; i++){
		invE += (long long) i - 1 - sum(E[i]);
		add(E[i], 1); 
	}
	fill(bit, bit+100010, 0); 
	for(int i = 1; i <= N/2; i++){
		invO += (long long) i - 1 - sum(O[i]);
		add(O[i], 1); 
	}
	if(revE % 2 == invO % 2 && revO % 2 == invE % 2) cout << "Yes" << endl; 
	else cout << "No" << endl;
}

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

#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(int i=(a)-1,i##_cond=(b);i>=i##_cond;--i) // ROF(i,n,0)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで降順
#define SUM(a) accumulate(all(a),int(0))
using ll  = long long;
using vi  = vector<int>;
using vvi = vector<vi>;
using vs  = vector<string>;
using pii = pair<int,int>;
constexpr ll inf = 1ll<<61;
constexpr ll mod = 1e9+7;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>T add(const T &a, const T &b){return (a+b)%mod;}

signed main(){
  int n; cin >> n;
  vi a(n); rep(i,n) cin >> a[i];
  vi l(n+1,-inf), r(n+1,-inf);
  rep(i,n){
    l[i+1] = max(a[i],l[i]);
    r[n-i-1] = max(a[n-i-1],r[n-i]);
  }
  dump(l,r);
  rep(i,n){
    cout << max(l[i],r[i+1]) << endl;
  }
}


Measured memory_bytes: 6272
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
 
#define CASES int t;cin>>t;while(t--)
#define REP(i, val, b) for (int i = val; i < b; ++i)
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
 
int moves(int a, int b, int c) {
	return abs(a - c)/2 + abs(b - c)/2;
}

int main() {
	fastio
	int a, b, c;
	cin >> a >> b >> c;
	int na, nb, nc;
	na = min(a, min(b, c));
	nc = max(a, max(b, c));
	// cout << na << ' ' << nb << ' ' << nc << endl;
	nb = a + b + c - na - nc;
	// cout << na << ' ' << nb << ' ' << nc << endl;
	a = na;
	b = nb;
	c = nc;
	int ans;
	// cout << a << ' ' << b << ' ' << c << endl;
	if ((a%2 && b%2 && c%2) || (a%2==0 && b%2==0 && c%2==0)) {
		ans = moves(a, b, c);
	} else if (a%2 && b%2==0 && c%2==0) {
		ans = moves(a, b+1, c+1) + 1;
	} else if (a%2==0 && b%2 && c%2==0) {
		ans = moves(a+1, b, c+1) + 1;
	} else if (a%2==0 && b%2==0 && c%2) {
		ans = moves(a+1, b+1, c) + 1;
	} else if (a%2 && b%2 && c%2==0) {
		ans = moves(a+1, b+1, c) + 1;
	} else if (a%2 && b%2==0 && c%2) {
		ans = moves(a+1, b, c+1) + 1;
	} else {
		ans = moves(a, b+1, c+1) + 1;
	}
	cout << ans;
	return 0;
}

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

typedef long long ll;
using std::min;
const int N = 710;
const int INF = 1e9;
int _w;

int n, m, g[N][N], vis[N];

int q[N], begin, end;
bool bfs( int s, int &a, int &b ) {
	begin = end = 0, q[end++] = s, vis[s] = 1;
	while( begin != end ) {
		int u = q[begin++];
		if( vis[u] == 1 ) ++a;
		else ++b;
		for( int v = 1; v <= n; ++v )
			if( g[u][v] ) {
				if( vis[v] == vis[u] ) return false;
				if( vis[v] == 3 - vis[u] ) continue;
				vis[v] = 3 - vis[u];
				q[end++] = v;
			}
	}
	return true;
}

int f[2][N][N];

int main() {
	_w = scanf( "%d%d", &n, &m );
	for( int i = 1; i <= n; ++i )
		for( int j = 1; j <= n; ++j )
			g[i][j] = 1;
	for( int i = 1; i <= m; ++i ) {
		int u, v;
		_w = scanf( "%d%d", &u, &v );
		g[u][v] = g[v][u] = 0;
	}
	for( int i = 1; i <= n; ++i ) g[i][i] = 0;
	f[0][0][0] = 1;
	for( int i = 1; i <= n; ++i )
		if( !vis[i] ) {
			int a = 0, b = 0;
			if( !bfs(i, a, b) )
				return puts("-1"), 0;
			for( int x = 0; x <= n; ++x )
				for( int y = 0; y <= n; ++y )
					f[1][x][y] = 0;
			for( int x = 0; x <= n; ++x )
				for( int y = 0; y <= n; ++y )
					if( f[0][x][y] )
						f[1][x+a][y+b] = f[1][x+b][y+a] = 1;
			for( int x = 0; x <= n; ++x )
				for( int y = 0; y <= n; ++y )
					f[0][x][y] = f[1][x][y];
		}
	int ans = INF;
	for( int i = 0; i <= n; ++i )
		for( int j = 0; j <= n; ++j )
			if( f[0][i][j] )
				ans = min( ans, i*(i-1)/2 + j*(j-1)/2 );
	printf( "%d\n", ans );
	return 0;
}


Measured memory_bytes: 6144
using System;
using System.Collections.Generic;

namespace ConsoleApp3
{
	class Program
	{
		static void Main(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			var list = new List<int>(n);
			for (int i = 0; i < n; i++)
			{
				list.Add(int.Parse(Console.ReadLine()));
			}
			var sortedList = new List<int>(list);
			sortedList.Sort();

			for (int i = 0; i < n; i++)
			{
				int target = list[i];
				int max = sortedList[n - 1];
				if (target == max)
				{
					max = sortedList[n - 2];
				}
				Console.WriteLine(max);
			}
		}
	}
}


Measured memory_bytes: 18004
#include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll inf=1000000000;
double pi=2*acos(0);
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll lmax(ll a,ll b){
    if(a<b)return b;
    else return a;
}
ll lmin(ll a,ll b){
    if(a<b)return a;
    else return b;
}
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    ll t[2];rep(i,0,2)cin>>t[i];
    ll a[2],b[2];
    rep(i,0,2)cin>>a[i];
    rep(i,0,2)cin>>b[i];
    ll l[2];
    if(a[0]<b[0]){
        swap(a[0],b[0]);
        swap(a[1],b[1]);
    }
    l[0]=a[0]*t[0]-t[0]*b[0];
    l[1]=b[1]*t[1]-t[1]*a[1];
    if(l[0]>l[1]){
        cout<<0<<endl;
        return 0;
    }
    if(l[0]==l[1]){
        cout<<"infinity"<<endl;
        return 0;
    }
    ll u=l[1]-l[0];
    ll y=l[0]/u;
    y*=2;
    if(l[0]%u!=0)y++;
    cout<<y<<endl;
}


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

int main() {
	ios_base::sync_with_stdio(false);
	int n, k; cin >> n >> k;
	vector<int> pos, neg;
	pos.push_back(0), neg.push_back(0);
	while (n--) {
		int x; cin >> x;
		if (x > 0) pos.push_back(x);
		else neg.push_back(-x);
	}
	int res = 3e8;
	sort(pos.begin(), pos.end());
	sort(neg.begin(), neg.end());
	for (int i = 1; i <= pos.size(); ++i) {
		if (k - i + 2 > neg.size()) continue;
		int f = pos[i - 1], g = neg[k - i+ 1];
		res = min(res, f + g + min(f, g));
	}
	cout << res;
}

Measured memory_bytes: 1020
# coding: utf-8

import math
import re
import numpy as np
import copy

S = input().split(" ")
D = int(S[0])
G = int(S[1])

p_n = []
c_n = []

for i in range(D):
    line = input().split(" ")
    p = int(line[0])
    c = int(line[1])
    p_n.append(p)
    c_n.append(c)

#十分大きい数で初期化
ans = 1000000

#ボーナス取得の有無を分配する関数（2^D通り）
def CombiBonus(flag):
    #最後まで到達した場合
    if len(flag) == D:
        #最小値の計算関数へ
        CalcMin(flag)

    else:
        #次のボーナスを取得するか否か
        non_use = flag + "0"
        use = flag + "1"
        CombiBonus(non_use)
        CombiBonus(use)

#最小回数を計算する関数
def CalcMin(flag):
    global ans

    #print(flag)

    #未回答問題数を格納するリスト
    unsolved = copy.copy(p_n)
    #現在のスコア
    score = 0
    #現在解いた問題数
    cnt = 0

    #ボーナス獲得している場合、全て解いているので反映
    for i in range(D):
        if flag[i] == "1":
            unsolved[i] = 0
            score += (i+1)*p_n[i]*100
            score += c_n[i]
            cnt += p_n[i]

    #これから解く問題の難易度
    current_d = D-1
    #スコアがG以上になるまで、難しい問題から優先して解き続ける
    while score < G:
        #2問以上残っている場合、同じ難易度のものを解く
        if unsolved[current_d] > 1:
            unsolved[current_d] -= 1
            score += (current_d+1)*100
            cnt += 1

        #そうでない場合、次の難易度へ
        else:
            current_d -= 1
            #0を下回る場合、終了
            if current_d < 0:
                return

    #print(cnt, score)

    #問題数が最小値かを判定
    if cnt < ans:
        ans = cnt

#探索
flag = ""
CombiBonus(flag)

print(ans)



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

using namespace std;

vector <int> tree;

void lowDigits(vector <int>& a) {
	int n = a.size();
	vector <pair <int, int>> tmp(n);
	for (int i = 0; i < n; i++) {
		tmp[i] = { a[i], i };
	}
	sort(tmp.begin(), tmp.end());
	int val = 0;
	a[tmp[0].second] = val;
	for (int i = 1; i < n; i++) {
		if (tmp[i - 1].first != tmp[i].first) {
			val++;
		}
		a[tmp[i].second] = val;
	}
}

void update(int v, int vl, int vr, int ind, int del) {
	if (ind < vl || vr <= ind) {
		return;
	}
	if (vr - vl == 1) {
		tree[v] += del;
		return;
	}
	int vm = (vl + vr) / 2;
	update(2 * v, vl, vm, ind, del);
	update(2 * v + 1, vm, vr, ind, del);
	tree[v] = tree[2 * v] + tree[2 * v + 1];
}

int get(int v, int vl, int vr, int l, int r) {
	if (r <= vl || vr <= l) {
		return 0;
	}
	if (l <= vl && vr <= r) {
		return tree[v];
	}
	int vm = (vl + vr) / 2;
	return get(2 * v, vl, vm, l, r) + get(2 * v + 1, vm, vr, l, r);
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	cin >> n >> m;
	vector <int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	lowDigits(a);

	vector <vector <int>> E(m, vector <int> (3));
	for (int i = 0; i < m; i++) {
		cin >> E[i][1] >> E[i][0];
		E[i][2] = i;
	}
	sort(E.begin(), E.end());
	tree.resize(4 * n);
	vector <int> pos(n, -1);
	vector <int> ans(m);
	for (int i = 0; i < E[0][0]; i++) {
		update(1, 0, n, i, 1);
		update(1, 0, n, pos[a[i]], -1);
		pos[a[i]] = i;
	}
	for (int i = 0; i < m; i++) {
		if (i != 0 && E[i - 1][0] != E[i][0]) {
			for (int j = E[i - 1][0]; j < E[i][0]; j++) {
				update(1, 0, n, j, 1);
				update(1, 0, n, pos[a[j]], -1);
				pos[a[j]] = j;
			}
		}
		ans[E[i][2]] = get(1, 0, n, E[i][1] - 1, E[i][0]);
	}
	for (auto i : ans) {
		cout << i << '\n';
	}
}

Measured memory_bytes: 44836
#include<iostream>
#include<vector>
int dp[110][10010] = {0};
using namespace std;
int main(){
    int item;
    int big;
    cin >> item;
    cin >> big;
    vector<int>itw;
    vector<int>itv;
    for(int i = 0;i < item;i++){
        int v,w;
        cin >> v;
        cin >> w;
        itv.push_back(v);
        itw.push_back(w);
    }
    for(int i = 0;i <= item;i++){
        for(int j = 0;j <= big;j++){
            if(i == 0 || j == 0){
                dp[i][j] = 0;
            }
            else{
                //cout << itw[i-1] << ':' << itv[i-1] << endl;
                if(j-itw[i-1] >= 0){
                    dp[i][j] = max(dp[i-1][j-itw[i-1]]+itv[i-1],dp[i-1][j]);
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
    }
    /*for(int i = 0;i <= item;i++){
        for(int j = 0;j <= big;j++){
            cout << dp[item-i][j] << ' ';
        }
        cout << endl;
    }*/
    cout << dp[item][big] << endl;
}

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

int main(){
    int n;
    cin >> n;
    vector<vector<int>> h(101, vector<int>(101, 0));
    vector<P> xy;
    rep(i, n){
        int x, y, h1;
        cin >> x >> y>> h1;
        h[x][y] = h1;
        xy.push_back(P(x, y));
    }
    ll cx, cy, hans;
    bool flag = false;
    rep(i, 101){
        if(flag) break;
        rep(j, 101){
            if(flag) break;
            cx = i; cy = j;
            for(P w:xy){
                int x0 = w.first, y0 = w.second;
                hans = h[x0][y0];
                hans += abs(cx - x0);
                hans += abs(cy - y0);
                if(hans == 0)continue;
                bool ok = true;
                for(P z:xy){
                    ll memo = hans;
                    memo -= abs(z.first - cx);
                    memo -= abs(z.second - cy);
                    memo = max(memo, (ll)0);
                    if(memo != h[z.first][z.second]) {
                        ok = false;
                        break;
                    }
                }
                if(ok){
                    flag = true;
                    break;
                }
                if(h[x0][y0] != 0) break;
            }
        }
    }
    cout << cx << " " << cy << " " << hans << endl;
    return 0;
}

Measured memory_bytes: 256