akhauriyash/Code-Regression huggingface dataset:
import java.util.*;


public class Main {
    public static void main(String[] args) {
        // 自分の得意な言語で
        // Let's チャレンジ！！

        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] lines = line.split(" ", 0);

        long start = Long.parseLong(lines[0]);
        long num = Long.parseLong(lines[1]);
        long dis = Long.parseLong(lines[2]);
        long abstart = Math.abs(start);
        
        if (1.0 * abstart / dis > num) {
            if (start < 0) {
                System.out.println(Math.abs(start + num * dis));
            } else {
                System.out.println(Math.abs(start - num * dis));
            }
            return;
        }

        if (abstart <= dis) {
            if (num % 2 == 0) {
                System.out.println(abstart);
            } else {
                if (start < 0) {
                    System.out.println(abstart);
                } else {
                    System.out.println(Math.abs(start - dis));
                }
            }
            return;
        }

        long temp = abstart / dis;
        long ret = 0;
        if (temp % 2 == 0) {
            if (num % 2 == 0) {
                if (start < 0) {
                    ret = start + temp * dis;
                } else {
                    ret = start - temp * dis;
                }
            } else {
                if (start < 0) {
                    ret = start + (temp + 1) * dis;
                } else {
                    ret = start - (temp + 1) * dis;
                }
            }
        } else {
            if (num % 2 == 0) {
                if (start < 0) {
                    ret = start + (temp + 1) * dis;
                } else {
                    ret = start - (temp + 1) * dis;
                }
            } else {
                if (start < 0) {
                    ret = start + temp * dis;
                } else {
                    ret = start - temp * dis;
                }
            }
        }
        System.out.println(Math.abs(ret));
    }
}


Measured memory_bytes: 35616
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>

using namespace std;

int main(void)
{
    int n;
    string w[112];
    cin >> n;
    map<string, bool> tmp;
    bool flag = true;
    for (int i = 0; i < n;i++){
        cin >> w[i];
        if(tmp[w[i]]==true)
            flag = false;
            if(i!=0&&w[i][0]!=w[i-1][w[i-1].length()-1])
                flag = false;
            tmp[w[i]] = true;
    }
    if(flag)
        cout << "Yes" << endl;
        else
            cout << "No" << endl;
        return 0;
}

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

int main() {
	int n,d,x[15][15]={},sum=0,ans=0;
	double a;
	cin>>n>>d;
	for(int i=0;i<n;i++){
	    for(int j=0;j<d;j++){
	        cin>>x[i][j];
	    }
	}
    for(int i=0;i<n;i++){
        for(int k=i+1;k<n;k++){
            for(int j=0;j<d;j++){
                sum+=(x[i][j]-x[k][j])*(x[i][j]-x[k][j]);
            }
         a=sum;
         sum=sqrt(sum);
         a=sqrt(a);
        if(a==sum){
            ans++;
        }
        sum=0;
        }
    }
	cout<<ans<<'\n';
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<numeric>
#include<iomanip>
#include<functional>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
using ll =long long;
//const int mod = 1000000007;
//class mint {
//	long long x;
//public:
//	mint(long long x = 0) : x((x% mod + mod) % mod) {}
//	mint operator-() const {
//		return mint(-x);
//	}
//	mint& operator+=(const mint & a) {
//		if ((x += a.x) >= mod) x -= mod;
//		return *this;
//	}
//	mint & operator-=(const mint & a) {
//		if ((x += mod - a.x) >= mod) x -= mod;
//		return *this;
//	}
//	mint & operator*=(const  mint & a) {
//		(x *= a.x) %= mod;
//		return *this;
//	}
//	mint operator+(const mint & a) const {
//		mint res(*this);
//		return res += a;
//	}
//	mint operator-(const mint & a) const {
//		mint res(*this);
//		return res -= a;
//	}
//	mint operator*(const mint & a) const {
//		mint res(*this);
//		return res *= a;
//	}
//	mint pow(ll t) const {
//		if (!t) return 1;
//		mint a = pow(t >> 1);
//		a *= a;
//		if (t & 1) a *= *this;
//		return a;
//	}
//	// for prime mod
//	mint inv() const {
//		return pow(mod - 2);
//	}
//	mint& operator/=(const mint & a) {
//		return (*this) *= a.inv();
//	}
//	mint operator/(const mint & a) const {
//		mint res(*this);
//		return res /= a;
//	}
//
//	friend ostream& operator<<(ostream & os, const mint & m) {
//		os << m.x;
//		return os;
//	}
//};
char num[5]{ 'M','A','R','C','H' };
void recursive_comb(vector<ll> indexes, int s, int rest, std::function<void(vector<ll>)> f) {
	if (rest == 0) {
		f(indexes);
	}
	else {
		if (s < 0) return;
		recursive_comb(indexes, s - 1, rest, f);
		indexes[rest - 1] = s;
		recursive_comb(indexes, s - 1, rest - 1, f);
	}
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(vector<ll>)> f) {
	 vector<ll>indexes(k);
	recursive_comb(indexes, n - 1, k, f);
}
ll ans = 0;
map<char, ll>a;
int main() {
	int N;
	cin >> N;
	vector<string>s(N);
	rep(i, N)cin >> s[i];
	
	rep(i, N) {
		a[s[i][0]]++;
	}
	
	foreach_comb(5, 3, [](vector<ll> indexes) {
		ans += a[num[indexes[0]]] * a[num[indexes[1]]] * a[num[indexes[2]]];

		});
	cout << ans;
}



Measured memory_bytes: 6144
fn read<T: std::str::FromStr>(
    stdin_lock: &mut std::io::StdinLock,
    buf: &mut Vec<u8>,
    delimiter: u8,
) -> T {
    buf.clear();
    let l = std::io::BufRead::read_until(stdin_lock, delimiter, buf).unwrap();
    buf.truncate(l - 1); // remove delimiter
    let s = unsafe { std::str::from_utf8_unchecked(&buf) };
    s.parse().unwrap_or_else(|_| panic!("read"))
}

fn main() {
    let stdin = std::io::stdin();
    let mut stdin_lock = stdin.lock();
    let mut buf: Vec<u8> = Vec::new();
    let a: u32 = read(&mut stdin_lock, &mut buf, b' ');
    let b: u32 = read(&mut stdin_lock, &mut buf, b' ');
    let c: u32 = read(&mut stdin_lock, &mut buf, b'\n');
    let k: u32 = read(&mut stdin_lock, &mut buf, b'\n');
    let abc = vec![a, b, c];
    let max_abc = abc.iter().max().unwrap();
    let ans = abc.iter().sum::<u32>() + max_abc * 2u32.pow(k) - max_abc;
    println!("{}", ans);
}


Measured memory_bytes: 4352
#include<bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
double PI=acos(-1);
int n,k,arr[105],dp[100005][2];
bool solve(int sum,int p){
    bool check=1;
    bool ret;
    if(dp[sum][p]!=-1) return dp[sum][p];
    if(p) ret=1;
    else ret=0;
    for(int i=0;i<n;i++){
        if(sum>=arr[i]){
            check=0;
            if(p) ret=ret && solve(sum-arr[i],1-p);
            else ret= ret||solve(sum-arr[i],1-p);
        }
    }
    if(check==0) return dp[sum][p]=ret;
    return dp[sum][p]=p;
}

int main(){
    IO
    cin>>n>>k;
    memset(dp,-1,sizeof dp);
    for(int i=0;i<n;i++) cin>>arr[i];
    //solve(k,0);
    if(solve(k,0)==1) cout<<"First\n";
    else cout<<"Second\n";
    //for(int i=0;i<=k;i++) cout<<dp[i][0]<<" "<<dp[i][1]<<'\n';
    return 0;
}


Measured memory_bytes: 12196
package main

import (
	"fmt"
)

func main() {

	var N uint64
	var A[5] uint64

	fmt.Scanf("%v", &N)
	fmt.Scanf("%v", &A[0]) // 0 -> 1
	fmt.Scanf("%v", &A[1]) // 0 -> 1
	fmt.Scanf("%v", &A[2]) // 0 -> 1
	fmt.Scanf("%v", &A[3]) // 0 -> 1
	fmt.Scanf("%v", &A[4]) // 0 -> 1

	num := make([]uint64, 6)
	num[0] = N

	minIdx := 0
	min := A[0]
	for i := 1; i < 5; i++ {
		if A[i] < min {
			min = A[i]
			minIdx = i
		}
	}
	// minの左までを足す
	var t uint64
	for i := 0; i < minIdx; i++ {
		t ++
	}
	// minの分を足す
	t += N / A[minIdx]
	if N % A[minIdx] != 0 {
		t++
	}
	// minの右を足す
	for i := minIdx + 1; i < 5; i++ {
		t ++
	}
	fmt.Printf("%v\n", t)
}




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

int main(){
  int N,M,C;
  cin >> N >> M >> C;
  int B[M]={};
  for(int i=0; i<M; i++) cin >> B[i];
  int A[N][M]={};
  int cnt=0;
  int sum=0;
  for(int i=0; i<N; i++){
    for(int j=0; j<M; j++){
      cin >> A[i][j];
      sum+=A[i][j]*B[j];
    }
    if(sum+C>0) cnt++;
    sum=0;
  }
  cout << cnt << endl;
  
  return 0;
}

Measured memory_bytes: 256
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

x=int(input())
if x>=30:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 10456
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <limits>
#include <cassert>
#include <map>
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define REPI(i,a,b) for(ll (i)=(a);(i)<(b);(i)++)
#define INF numeric_limits<double>::infinity()
#define ll long long
constexpr ll MOD = 1e9 + 7;
ll gcd(ll a, ll b) {return b % a ? gcd(b % a, a) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
using namespace std;
using P = pair<ll, ll>;
using VI = vector<ll>;

int main() {
    string s;
    cin >> s;
    sort(s.begin(), s.end());
    if (s == "abc") {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    string s;
    int d;
    cin >> s >> d;
    vector<vector<vector<ll>>> dp(s.size()+1, vector<vector<ll>>(d, vector<ll>(2, 0)));
    dp[0][0][1] = 1;
    REP(i, s.size()){
        ll it = s[i-1] - '0';
        rep(j, d){
            rep(k, 10){
                if(k < it)dp[i][(j+k)%d][0] = (dp[i-1][j][0] + dp[i-1][j][1] + dp[i][(j+k)%d][0]) % mod;
                if(k == it){
                    dp[i][(j+k)%d][0] = (dp[i-1][j][0] + dp[i][(j+k)%d][0]) % mod;
                    dp[i][(j+k)%d][1] = (dp[i-1][j][1] + dp[i][(j+k)%d][1]) % mod;
                }
                if(k > it)dp[i][(j+k)%d][0] = (dp[i-1][j][0] + dp[i][(j+k)%d][0]) % mod;
            }
        }
    }
    cout << (dp[s.size()][0][0] + dp[s.size()][0][1] - 1 + mod) % mod << endl;
}   

Measured memory_bytes: 55296
def limited_insertion(N: int, B: list)->list:
    # print(N, B)
    if N == 1:
        return [1] if B[0] == 1 else []
    # 後ろからみていって、インデックス(1-indexed)と
    # その位置の配列(B)の値が等しい最初のペアを探す。
    # なければ失敗
    for i in range(N, 0, -1):
        if i == B[i-1]:
            # 見つけたら、それを除いた配列に関して
            # 再帰的に同じことを行う。
            # print('>>>pair', i, B[i-1])
            rec = limited_insertion(N-1, B[:i-1] + B[i:])
            if len(rec) > 0:
                return rec + [i]
            else:
                return []

    return []


if __name__ == "__main__":
    N = int(input())
    B = [int(s) for s in input().split()]
    A = limited_insertion(N, B)
    if len(A) == 0:
        print(-1)
    else:
        for a in A:
            print(a)


Measured memory_bytes: 3316
#include <bits/stdc++.h>  // g++ -std=c++14 -o a a.cpp

using namespace std;

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

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repto(i, n) for(int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

__attribute__((constructor)) void initial() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

int like[32][32] = {0};

int main() {
    int N, M;
    cin >> N >> M;
    rep(i, N) {
        int K;
        cin >> K;
        rep(j, K) {
            int A;
            cin >> A;
            like[i][A] = 1;
        }
    }

    int allLike = 0;

    repto(j, M) {
        bool isAllLike = true;
        rep(i, N) {
            if(like[i][j] == 0) isAllLike = false;
        }
        if (isAllLike) allLike++;
    }

    cout << allLike << endl;
}

Measured memory_bytes: 256
n=int(input())
L=input().split( )
a=[int(L[i]) for i in range(n)]
for i in range(n-1):
    x=a[i]
    y=a[i+1]
    while x%y!=0:
        w=y
        y=x%y
        x=w
    a[i+1]=a[i+1]*a[i]//y


print(a[n-1])



Measured memory_bytes: 5604
# include <bits/stdc++.h>
# define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
# define int long long

using namespace std;

const int N = 2e5 + 7;

int32_t main() {
    speed;
    int n, m, ans = 0;
    cin >> n >> m;

    vector < int > a(n, -1), s(m), c(m);

    for ( int i = 0; i < m; i++ ) {
        cin >> s[i] >> c[i];
        s[i]--;

        if ( ( a[ s[i] ] == -1 ) || a[ s[i] ] == c[i] ) {
            a[ s[i] ] = c[i];
        }
        else {
            ans = -1;
        }
    }
    if ( n != 1 && a[0] == 0 ) {
        ans = -1;
    }
    if ( ans == -1 ) {
        cout << -1;
        return 0;
    }
    if ( a[0] == -1 && n != 1 ) {
        a[0] = 1;
    } 
    if ( a[0] == -1 && n == 1 ) {
        a[0] = 0;
    }
    cout << a[0];
    for ( int i = 1; i < n; i++ ) {

        if ( a[i] == -1 ) {
            a[i] = 0;
        }
        cout << a[i];
    }
}   
  

Measured memory_bytes: 256
#include<iostream>
    using namespace std;
     
    int main(){
    	int a,b,c;
     
    	cin >>a >>b >>c;
    	for(int i=0;i<=b;i++){
    		if(a*i%b==c){
    			cout<<"YES"<<endl;
    			return 0;
    		}
    	}
    	cout<<"NO"<<endl;
    	return 0;
    }

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;

int p[50001];
int prime()
{
    for(int i=0;i<=50000;++i) p[i] = 1;
    for(int j=4;j<=50000;j+=2) p[j] = 0;
    for(int i=3;i<=50000;++i)
    {
        if(!p[i]) continue;
        for(int j=i+i;j<=50000;j+=i) p[j] = 0;
    }
}
int solve(int n)
{
    int ans=0;
    int half = n / 2;
    if(p[n-2]) ++ans;
    if(n%2==0){
        for(int i=3;i<=half;i+=2)
        {
            if(p[i] && p[n-i]) ++ans;
        }
    }
    return ans;
}
int main()
{
    int n;
    prime();
    while(cin >> n && n) cout << solve(n) << endl;
}

Measured memory_bytes: 1356
#include <bits/stdc++.h>
using namespace std;
int main(void){
  int N;
  string s;
  cin >> N >> s;
  
  int r_cnt = 0;
  int b_cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'R') {
      r_cnt++;
    } else {
      b_cnt++;
    }
  }
  if (r_cnt > b_cnt) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{	
	int n,k;
    cin>>n>>k;
	int ans=0;
	while(n)
	{
		if(n<k)
			break;
		n=n/k;
		ans++;
	}
	cout<<ans+1<<endl;
	return 0;
}


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

ll r, c, n;
vector<pair<ll, ll> > vr[(ll)1e5];
vector<pair<ll, ll> > vc[(ll)1e5];
bool used[2][(ll)1e5];
ll score[2][(ll)1e5];

pair<ll, ll> dfs(ll pos, ll value, bool first, bool tate){
	if(first){
		used[(ll)!tate][pos] = 1;
		score[(ll)!tate][pos] = 0;
		pair<ll, ll> ret = pair<ll, ll>(1145141919 * (ll)!tate, 1145141919 * (ll)tate);
		for(auto next : (tate ? vr[pos] : vc[pos])){
			auto temp = dfs(next.first, next.second, 0, !tate);
			ret = pair<ll, ll>(min(ret.first, temp.first), min(ret.second, temp.second));
		}
		return ret;
	}
	used[(ll)!tate][pos] = 1;
	score[(ll)!tate][pos] = value;
	pair<ll, ll> ret = pair<ll, ll>((tate ? value : 1145141919), (tate ? 1145141919 : value));
	for(auto next : (tate ? vr[pos] : vc[pos])){
		if(used[(ll)tate][next.first]){
			if(score[(ll)tate][next.first] + value != next.second) return pair<ll, ll>(-1, - 1);
			else continue;
		}
		auto temp = dfs(next.first, next.second - value, 0, !tate);
		ret = pair<ll, ll>(min(ret.first, temp.first), min(ret.second, temp.second));
	}
	return ret;
}
	
int main(){
	cin >> r >> c >> n;
	for(ll i = 0; i < n; ++i){
		ll po, yo, pi; cin >> po >> yo >> pi;
		vr[--po].push_back(pair<ll, ll>(--yo, pi));
		vc[yo].push_back(pair<ll, ll>(po, pi));
	}
	for(ll i = 0; i < r; ++i){
		if(!used[0][i]){
			pair<ll, ll> ret = dfs(i, 0, 1, 1);
			if(ret.first + ret.second < 0){
				cout << "No\n";
				return 0;
			}
		}
	}
	for(ll i = 0; i < c; ++i){
		if(!used[1][i]){
			pair<ll, ll> ret = dfs(i, 0, 1, 0);
			if(ret.first + ret.second < 0){
				cout << "No\n";
				return 0;
			}
		}
	}
	cout << "Yes\n";
	return 0;
}

Measured memory_bytes: 19712
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <stack>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long int ll;


int main()
{
	int x;
	cin>>x;
	int a = 599;
	for(int i = 8 ; i >= 1; i--){
		if( x <= a){
			cout<<i<<endl;
			break;
		}
		a += 200;
	}
	return 0;
}


Measured memory_bytes: 3196
import java.util.Scanner;


public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        char[] ca = str.toCharArray();
        int[] count = new int[4];
        for (int i =0; i < ca.length; i++) {
            for (char c : ca) {
                if (c == ca[i]) {
                    count[i]++;
                }
            }
        }
        boolean key = true;
        for (int c:count) {
            if (c != 2) {
                key = false;
            }
        }
        if (key) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
        
    }
}

Measured memory_bytes: 21844
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//
//    #pragma GCC optimize("unroll-loops")
//    #pragma GCC optimize("Ofast")
//    #pragma GCC optimize("-O3")
//    #pragma GCC optimize("no-stacnice_task-protector")
//    #pragma GCC optimize("fast-math")

#define fi first
#define f first
#define se second
#define s second
#define vi_a vector<int>a;
#define p_b emplace_back
////////////////////////////////???????????????CHECnice_task THIS OUT???????????????//////////////////////////////
#define ll long long
////////////////////////////////???????????????CHECnice_task THIS OUT???????????????//////////////////////////////
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define fast_io cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
#define all(x) x.begin(),x.end()
#define getfiles    ifstream cin("input.txt");ofstream cout("output.txt");
#define pw(x) (1ll << x)
#define sz(x) (ll)x.size()
#define endl "\n"
#define rall(x) x.rbegin(),x.rend()
#define len(a) (ll)a.size()
#define rep(x,l,r) for(ll x=l;x<r;x++)
using namespace std;
//using namespace __gnu_pbds;
ld eps = (ld)1 / 1e6;
ll inf = 1e9, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const ll R=1e4;
const ll tx[4]={0,0,1,-1};
const ll ty[4]={1,-1,0,0};
const char rev_to[4]={'E','W','N','S'};
//const ll N
signed main(){
    fast_io;
    ll f,s;
    cin>>f>>s;
    if(s>=f){
        cout<<"unsafe";
    }
    else{
        cout<<"safe";
    }
    return 0;
}
/*
    если WA , то чекер говно, поэтому почекай 18 строчку
    чекай для t>1 (выводишь ли ты  с endl))
*/


Measured memory_bytes: 3688
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <random>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using vb = vector<bool>;
using mll = map<ll, ll>;
using sll = stack<ll>;
#define REP(i,n) for(ll i(0);(i)<(n);(i)++)
#define rep(i,n) for(ll i(0);(i)<(n);(i)++)
#define ALL(a) a.begin(), a.end()
#define elnd endl //* missspell check


void solve(long long K, long long T, std::vector<long long> a){
    sort(ALL(a));
    ll x = a[T-1];
    cout << max(0LL, (x-1)-(K-x))<<endl;
}

int main(){
    long long K;
    scanf("%lld",&K);
    long long T;
    scanf("%lld",&T);
    std::vector<long long> a(T);
    for(int i = 0 ; i < T ; i++){
        scanf("%lld",&a[i]);
    }
    solve(K, T, std::move(a));
    return 0;
}


Measured memory_bytes: 256
import numpy as np

n, d = map(int, input().split())
s = [np.asarray(list(map(int, input().split()))) for i in range(n)]

counter = 0

for i in range(n):
    for j in range(i+1, n):
        ans = np.sqrt(np.sum((s[i] - s[j]) ** 2))
        if ans == int(ans):
            counter += 1

print(counter)

Measured memory_bytes: 12500
# 貪欲+山登り(2点交換+1点更新)
import time
import random

d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]

# 貪欲法による初期解の構築
T = []
L = [-1 for j in range(26)]
for i in range(d):
    max_diff = -10**7
    arg_max = 0
    for j in range(26):
        memo = L[j]
        L[j] = i
        diff = S[i][j] - sum([C[k] * (i - L[k]) for k in range(26)])
        if diff > max_diff:
            max_diff = diff
            arg_max = j
        L[j] = memo
    T.append(arg_max)
    L[arg_max] = i


def calc_score(T):
    L = [-1 for j in range(26)]
    X = [0 for j in range(26)]
    score = 0
    for i in range(d):
        score += S[i][T[i]]
        X[T[i]] += (d - i) * (i - L[T[i]])
        L[T[i]] = i
    for j in range(26):
        score -= C[j] * (dd - X[j])
    return score


score = calc_score(T)
start = time.time()
cnt = 0
while True:
    now = time.time()
    if now - start > 1.8:
        break
    # 1点更新
    i = (cnt // 26) % d
    j = random.choice(range(26))
    memo = T[i]
    T[i] = j
    new_score = calc_score(T)
    T[i] = memo
    if new_score > score:
        T[i] = j
        score = new_score
    cnt += 1
    # 2点交換
    i0 = random.choice(range(d))
    i1 = i0 - 13 if i0 > 13 else i0 + 13
    T[i0], T[i1] = T[i1], T[i0]
    new_score = calc_score(T)
    T[i0], T[i1] = T[i1], T[i0]
    if new_score > score:
        T[i0], T[i1] = T[i1], T[i0]
        score = new_score
for t in T:
    print(t + 1)


Measured memory_bytes: 84056
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(),(x).end())
#define ALL(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define reps(i, m, n) for(int i = m; i < n; i++)
#define repr(i, m, n) for(int i = m; i >= n; 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 y0 y3487465 //j0
#define y1 y1347829 //j1
#define INF 1000000007 //4000000000000000037
#define PI (acos(-1))
#define MOD 1000000007
using namespace std;
typedef long long ll;



void Main(){

  int N;
  cin>>N;
  int ans=INF;
  rep(i,N){
    ll a;cin>>a;
    int ct=0;
    while(a%2==0){
      ++ct;
      a/=2;
    }
    chmin(ans,ct);
  }
  cout<< ans <<"\n";

}

//-----------------------------------
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------


Measured memory_bytes: 256
#include<iostream>
    #include<string>
     
    using namespace std;
     
    int main()
    {
     string s;
     cin >> s;
     
     string out;
     for(int i = 0;i < s.length();i +=2)
     {
      out.push_back(s[i]);
     }
     
     cout << out << endl;
     
     return 0;
    }

Measured memory_bytes: 512
import sys

class Node:
    __slots__ = ['key', 'left', 'right']
    def __init__(self, key):
        self.key = key
        self.left = self.right = None

root = None

def insert(key):
    global root
    x, y = root, None
    while x is not None: x, y = x.left if key < x.key else x.right, x
    if y is None: root = Node(key)
    elif key < y.key: y.left = Node(key)
    else: y.right = Node(key)

def find(target):
    result = root
    while result and target != result.key:
        result = result.left if target < result.key else result.right
    return result is None

def delete(target):
    def remove_node(p, c, a):
        if p.left == c: p.left = a
        else: p.right = a
    p, c = None, root
    while c.key != target: p, c = c, c.left if target < c.key else c.right
    if c.left is None:
        remove_node(p, c, c.right)
    elif c.right is None:
        remove_node(p, c, c.left)
    elif c.right.left is None:
        c.right.left = c.left
        t = c.right.right
        c.key = c.right.key
        c.right = t
    else:
        g = c.right
        while g.left.left is not None: g = g.left
        c.key = g.left.key
        g.left = g.left.right

def inorder(node):
    return inorder(node.left) + f' {node.key}' + inorder(node.right) if node else ''
def preorder(node):
    return f' {node.key}' + preorder(node.left) + preorder(node.right) if node else ''

input()
for e in sys.stdin:
    if e[0] == 'i': insert(int(e[7:]))
    elif e[0] == 'd': delete(int(e[7:]))
    elif e[0] == 'f': print(['yes','no'][find(int(e[5:]))])
    else: print(inorder(root)); print(preorder(root))


Measured memory_bytes: 56180
# -*- coding: utf-8 -*-
 
def main():
    N = int(input())
    A = list(map(int,input().split()))
    R = 1000000007

    Ans = 1
    P = [0, 0, 0]
    for Ai in A:
        Count = 0
        for i, Pi in enumerate(P):
            if Ai == Pi:
                if Count == 0:
                    P[i] += 1
                Count += 1
        Ans = (Ans * Count) % R

    print(Ans)

             
 

        
 
 
 
if __name__ == "__main__":
    main()


Measured memory_bytes: 14056
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            List<int> listN = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToList();
            int cnt = 0;
            for(int i = 1; i <N - 1; i++)
            {
                if(listN[i - 1] >= listN[i]  && listN[i] >= listN[i + 1]
                   || listN[i - 1] <= listN[i]  && listN[i] <= listN[i + 1])
                {
                    cnt++;
                }
            }
            Console.WriteLine(cnt);
        }
    }
}

Measured memory_bytes: 13268
#include <bits/stdc++.h>
#include <array>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for(UL i = 0; i < (n); i++)

struct Forest_Uni {
	vector<pair<UL, UL>> v;
	void init(UL n) {
		v.clear();
		rep(i, n) v.push_back({ i, 1 });
	}
	UL root(UL p) {
		if (v[p].first == p) return v[p].first;
		return v[p].first = root(v[p].first);
	}
	void unite(UL a, UL b) {
		UL A = root(a);
		UL B = root(b);
		if (A != B) {
			v[A].first = B;
			v[B].second += v[A].second;
		}
	}
};

struct Problem {

	void Solve() {
		UL N, M, K; cin >> N >> M >> K;
		vector<vector<UL>> E(N);
		vector<vector<UL>> WE(N);
		Forest_Uni F; F.init(N);
		rep(i, M) {
			UL u, v; cin >> u >> v; u--; v--;
			E[u].push_back(v);
			E[v].push_back(u);
			F.unite(u, v);
		}
		rep(i, K) {
			UL u, v; cin >> u >> v; u--; v--;
			WE[u].push_back(v);
			WE[v].push_back(u);
		}
		rep(i, N) {
			UL ans = F.v[F.root(i)].second - 1;
			for (UL e : E[i]) if (F.root(i) == F.root(e)) ans--;
			for (UL e : WE[i]) if (F.root(i) == F.root(e)) ans--;
			if (i) cout << " ";
			cout << ans;
		}
		cout << endl;
	}

	Problem();
};
int main() {
	unique_ptr<Problem> p(new Problem());
	p->Solve();
	return 0;
}
Problem::Problem() {
	cout << fixed << setprecision(10);
}


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

int main(){
    int n;
    cin >> n;
    vector<string> s(n);
    map<char,int> mp[n];
    vector<int> f(26,mod);
    string ans;
    
    
    for(int i = 0; i < n; i++){
        cin >> s[i];
        for(int j = 0; j < s[i].size(); j++){
            mp[i][s[i][j]]++;
        }
    }   
    for(int i = 0; i < 26; i++){
        for(int j = 0; j < n; j++){
            if(mp[j]['a'+i] < f[i]) f[i] = mp[j]['a'+i];
        }
        for(int j = 0; j < f[i]; j++){
            ans += 'a'+i;
        }
    } 
    cout << ans << endl;
}

Measured memory_bytes: 256
#pragma GCC optimize (Ofast)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forit(it, m) for(auto it = m.begin(); it!=m.end(); ++it)

int main(){
	ios::sync_with_stdio(false);
  	cin.tie(0); 
	int n,m,x,tot,cost,fin=1000000000;
	bool f;

	cin>>n>>m>>x;
	vector<int> c(n);
	vector<vector<int> > a(n,vector<int> (m));
	forn(i,n){
		cin>>c[i];
		forn(j,m)
			cin>>a[i][j];
	}
	tot = pow(2,n);
	forn(perm,tot){
		cost = 0;
		vector<int> skill(m,0);
		f=true;
		for(int i=1;i<tot;i*=2){
			if(perm&i){
				cost+=c[log2(i)];
				forn(j,m)
					skill[j]+=a[log2(i)][j];
			}
		}
		forn(j,m)
			if(skill[j]<x)
				f=false;
		if(f)
			fin=min(fin,cost);
	}
	cout<<(fin==1000000000?-1:fin)<<endl;

	return 0;
}




Measured memory_bytes: 4004
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
#define all(a) sort((a).begin(),(a).end()) //ソートのマクロ
#define Re(a) reverse((a).begin(),(a).end())
#define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する
const int MOD=1000000007;
const double pi = 3.141592653589793;
//素数判定 O(√N)
bool isPrime(int64_t x){
    if(x<=1){
        return false;
    }
    for(int i=2 ;i*i<=x ;i++){
        if(x%i==0){
            return false;
        }
    }
    return true;
}
int main(){
    int n;
    cin>>n;
    vector<int> prime;
    set<int> s;
    vector<int> ans;
    for(int i=2;i<=55555;i++){
        if(isPrime(i)){
            prime.push_back(i);
        }
    }
    int sum=0;
    int k=0;
    //5で割って1余る素数だけ入れてけばどの5個選んでも5の倍数だね
    //天才すぎる　かっけえ・・・・・(思いつかなった)
    while(ans.size()!=n){
        if(prime[k]%5==1){
            ans.push_back(prime[k]);
        }
        k++;
    }
    rep(i,n){
        cout<<ans[i]<<" ";
    }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i,n) for(ll i=0;i<n;i++)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define fi first
#define se second
#define endl "\n"

template<typename T> inline bool chmax(T &a, T b){if(a<b){a=b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){int len=d.size();rep(i,len){s<<d[i];if(i<len-1) s<<" ";}return s;}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){int len=d.size();rep(i,len){s<<d[i]<<endl;}return s;}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T> ostream& operator<<(ostream& s,const multiset<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){s<<"{"<<endl;for(auto itr=m.begin();itr!=m.end();++itr){s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;}s<<"}"<<endl;return s;}

const ll mod=1'000'000'007;
const ll inf=1'000'000'000'000'000'00;
const int INF=1'000'000'000;
const double EPS=1e-10;
const double PI=acos(-1);

int main(){
	cin.tie(0);ios::sync_with_stdio(false);
	ll x;
	cin>>x;
	ll ans=x/500*1000;
	x%=500;
	ans+=x/5*5;
	cout<<ans<<endl;
}

Measured memory_bytes: 3660
n = gets.chomp.to_i
arr = gets.chomp.split('')
left, right = 0, arr.length - 1
count = 0
while true
  head_left = -1
  tail_right = -1
  left.upto(n - 1) do |i|
    if arr[i] == 'W'
      head_left = i
      break
    end
  end
  right.downto(0) do |i|
    if arr[i] == 'R'
      tail_right = i
      break
    end
  end
  break if head_left == -1 || tail_right == -1 || head_left >= tail_right
  arr[head_left], arr[tail_right] = arr[tail_right], arr[head_left]
  left = head_left
  right = tail_right
  count += 1
end
puts count


Measured memory_bytes: 30548
#include <bits/stdc++.h>
//#define int long long
#define loop(i, a, b) for(int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define all(a) (a).begin(),(a).end()
using namespace std;
const int MOD = 1e9 + 7, INF = 1e9;
using vi = vector <int>;
using vvi = vector <vi>;
//g++ -std==c++14

signed main(){ 
    int a, b, c;
    cin >> a >> b >> c;
    cout << min(a * b, c) << endl;
}


Measured memory_bytes: 256
#include<stdio.h>
#define N 500000
#define S 1000000000
int L[N],R[N];
int count=0;
void merge(int A[],int left,int mid,int right){
  int n1 = mid - left;
  int n2 = right - mid;
  int i,j,k;
  for(i=0;i<n1;i++) L[i] = A[left + i];
  for(i=0;i<n2;i++) R[i] = A[mid + i];
  L[n1] = 2*S;
  R[n2] = 2*S;
  i = 0;
  j = 0;
    for(k=left;k<right;k++){
      count++;
      if(L[i] <= R[j]){
	A[k] = L[i];
	i++;
      }
      else { 
	A[k] = R[j];
      j++;
      }
    }
}

void mergeSort(int A[],int left,int right){
  if (left+1 < right){
    int mid = (left + right)/2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main(){ 
  int A[N],n,i;
  scanf("%d",&n);
  for(i=0;i<n;i++) scanf("%d",&A[i]);
    mergeSort(A,0,n);    
    for(i=0;i<n;i++){
      if(i == n-1)
	printf("%d\n",A[i]);
      else printf("%d ",A[i]);
    }
    printf("%d\n",count);
    return 0;
}



Measured memory_bytes: 5648
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
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(){
   string S;
   cin>>S;
   vector<int> A(S.size()-2);
   for(int i=0;i<S.size()-2;i++){
       A.at(i)=100*(S.at(i)-'0')+10*(S.at(i+1)-'0')+(S.at(i+2)-'0');
   }
   int ans=INT_MAX;
  for(int i=0;i<S.size()-2;i++){
      chmin(ans,abs(A.at(i)-753));
  }
  cout<<ans<<endl;
}    

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;

int main() {
int a,b,c;
cin >> a >> b >> c;
int ans = c- (a-b);
if (ans < 0) cout << 0 << endl;
else cout << ans << endl;

return 0;
}


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

long long int Q,H,S,D,N;
long long int raito[4];

int main(){
    cin >> Q >> H >> S >> D >> N;
    long long ans = 0;

    if(Q * 2 < H){
        H = Q * 2;
    } 
    if(H * 2 < S){
        S = H * 2;
    }
    if(S * 2 < D){
        D = S * 2;
    }

    ans += (N / 2) * D;
    N -= 2 * (N / 2);
    ans += (N / 1) * S;

    cout << ans << endl;
}

Measured memory_bytes: 3516
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, a, b;
    cin >> n >> a >> b;
    cout << min(n * a, b) << endl;
}


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

#define N 16

int bitcount(int n){return n?(n&1)+bitcount(n>>1):0;}

int main()
{
    int n, c;
    vector<int> a, b;
    while(cin>>n>>c, n|c) {
        a.clear();
        a.resize(n, 0);
        for(int i=0; i<n; i++) {
            for(int j=0; j<N; j++) {
                int in;
                cin>>in;
                a[i] |= in<<j;
            }
        }
        b.clear();
        b.resize(c, 0);
        for(int i=0; i<c; i++) {
            for(int j=0; j<N; j++) {
                int in;
                cin>>in;
                b[i] |= in<<j;
            }
        }

        vector< vector<int> > dp(n+1, vector<int>(1<<N, -1));
        dp[0][0] = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<(1<<N); j++) {
                if(dp[i][j]<0) continue;
                for(int k=0; k<c; k++) {
                    int panel = a[i]|j;
                    int pushed = panel&b[k];
                    int next = panel^pushed;
                    dp[i+1][next] = max(dp[i+1][next], dp[i][j]+bitcount(pushed));
                }
            }
        }
        int point = 0;
        for(int j=0; j<(1<<N); j++) point = max(point, dp[n][j]);
        cout<<point<<endl;
    }
}

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

int main(){
  long N,K;
  cin>>N;
  cin>>K;
  long array[100000]={},number,value,reference = 0;

  for(int i=0;i<N;i++){
    cin>>number;
    cin>>value;
    array[number-1] += value;
  }

  for(int j=0;j<100000;j++){
    reference += array[j];
    if(reference>=K){
      cout<<j+1<<endl;
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 1024
h,w,d=map(int, input().split())
a=[list(map(int,list(input().split()))) for _ in range(h)]
p=[0]*(h*w+1)
for i in range(h):
    for j in range(w):
        p[a[i][j]]=(i,j)

c=[0]*(h*w+1) #累積和
for i in range(d+1,h*w+1):
    c[i]=c[i-d]+abs(p[i][0]-p[i-d][0])+abs(p[i][1]-p[i-d][1])
q=int(input())
ans=0
for i in range(q):
    l,r=map(int, input().split())
    print(c[r]-c[l])

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

#define int long long
#define rep(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define eb emplace_back
#define dump(x) cout << #x << " = " << (x) << endl
#define chk() cout << "now" << endl;
#define ALL(x) (x).begin(), (x).end()
#define SORT(c) sort((c).begin(), (c).end())
#define SORTG(c) sort(ALL(c), greater<int>());
#define fi first
#define se second
int INF = 1e9 + 7;

typedef pair<int, int> P;
typedef vector<int> vi;

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

int n, l;

void solve() {
  cin >> n >> l;
  int sum = n * l;
  rep(i, 0, n) { sum += i; }

  int ret = INF;
  int ans;
  rep(i, 0, n) {
    int a = l + i;
    int b = sum - a;
    if (ret > abs(sum - b)) {
      ret = abs(sum - b);
      ans = a;
    }
  }
  cout << sum - ans << endl;
}

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

  // cout << fixed << setprecision(10);

  solve();

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;
    ll p=1000000007;
    cin>>n>>m;
    ll dp[n+1];
    dp[0]=1;
    ll num;
    for(ll i=1;i<=m;i++)
    {
        cin>>num;
        dp[num]=-1;
    }
    if(dp[1]==-1)
    {
        dp[1]=0;
    }
    else
        dp[1]=1;
    for(ll i=2;i<=n;i++)
    {
        if(dp[i]==-1)
        {
            dp[i]=0;
        }
        else
        {
            dp[i]=(dp[i-1]+dp[i-2])%p;
        }
        //cout<<dp[i]<<" ";
    }
    cout<<dp[n];
}

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

int h,w;
string s[1010],joi[2];
bool ok[1010][1010];
const char change[] = {'J', 'O', 'I'};

inline bool check(int y, int x){
  if(y<0 || x<0 || y>=h || x>=w)return 0;
  return s[y][x] == joi[0][0] && s[y+1][x] == joi[1][0] &&
	  s[y][x+1] == joi[0][1] && s[y+1][x+1] == joi[1][1];
}

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

  cin >> h >> w;
  for(int i=0;i<h;i++)cin >> s[i];
  cin >> joi[0] >> joi[1];

  int sum = 0;
  for(int i=0;i<h-1;i++){
    for(int j=0;j<w-1;j++){
      ok[i][j] = check(i,j);
      if(ok[i][j])sum++;
    }
  }

  int ans = sum;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      char mem = s[i][j];
      for(int c=0;c<3;c++){
	int tmp = sum;
	for(int dy=-1;dy<=0;dy++){
	  for(int dx=-1;dx<=0;dx++){
	    tmp -= ok[i+dy][j+dx];
	  }
	}
      
	s[i][j] = change[c];
	for(int dy=-1;dy<=0;dy++){
	  for(int dx=-1;dx<=0;dx++){
	    tmp += check(i+dy,j+dx);
	  }
	}
	ans = max(ans, tmp);
      }
      s[i][j] = mem;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  rep(i,n)cin >> h[i];
  bool can = true;
  //右から見て必要な低さのベクタをつくる
  vector<int> need(n);
  need[n-1]=h[n-1];
  for(int i=n-2;i>=0;i--){
    need[i]=min(h[i],need[i+1]);
    if(h[i]-need[i]==1)h[i]--;
    else if(h[i]-need[i]>1){
      can = false;
      break;
    }
  }
  
  can ? cout << "Yes" : cout << "No" ;
  cout << endl;
}


Measured memory_bytes: 1024
import collections
import itertools
N = int(input())
L = list(map(int,input().split()))
s = list(set(L))
c = collections.Counter(L)
ans = 0
for x in itertools.combinations(s, 3):
  if (max(x) < sum(x)-max(x)):
    prd = 1
    for i in range(len(x)):
      prd *= c[x[i]]
    ans += prd
print(ans)

Measured memory_bytes: 9460
/*input
10 10 10
1 6
2 9
4 5
4 7
4 7
5 8
6 6
6 7
7 9
10 10
1 8
1 9
1 10
2 8
2 9
2 10
3 8
3 9
3 10
1 10

*/
#include <bits/stdc++.h>
#pragma GCC optmize("O2")
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int, int>
const int MX = 500 + 10;
vector<int> v[MX];
main () {
    int n, m, q; cin >> n >> m >> q;
    while(m --) {
        int a, b; cin >> a >> b;
        v[a].push_back(b);
    }
    for(int i = 0; i < MX; i ++) sort(v[i].begin(), v[i].end());
    while(q--) {
        int ans = 0;
        int a, b; cin >> a >> b;
        for(int i = a; i <= b; i ++) ans += upper_bound(v[i].begin(), v[i].end(), b) - lower_bound(v[i].begin(), v[i].end(), a);
        cout << ans << endl; 
    }
}


Measured memory_bytes: 3456
#include <iostream>
#include <cstdlib>
#include <queue>
#include <vector>
#include <utility>
#include <climits>
using namespace std;

struct edge {int to, cost;};
typedef int Vertex;
typedef pair<int, Vertex> P;
typedef vector<vector<edge>> Graph;

Graph make_graph(const int V, const int E){
    Graph G(V);

    for(int i = 0; i < E; ++i){
        int s,t,d; cin >> s >> t >> d;
        G[s].emplace_back(edge{t, d});
    }

    return G;
}

// ???????????´???????????§??????????????¢????±???????
/*int dijkstra(Graph &G, Vertex s, Vertex t){
    vector<int> d(G.size(), INF);
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(make_pair(0, s));

    while(!q.empty()){
        int cost = q.top().first;
        Vertex current = q.top().second;
        q.pop(); 
        
        if(t == current) break;

        if(d[current] < cost) continue;

        for(auto &e : G[current]){
            if(d[e.to] > d[current] + e.cost){
                d[e.to] = d[current] + e.cost;
                q.push(make_pair(d[e.to], e.to));
            }
        }
    }

    return d[t];
}*/

vector<int> dijkstra(Graph &G, Vertex s){
    vector<int> d(G.size(), INT_MAX);
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(make_pair(0, s));

    while(!q.empty()){
        int cost = q.top().first;
        Vertex current = q.top().second;
        q.pop(); 
        
        if(d[current] < cost) continue;

        for(auto &e : G[current]){
            if(d[e.to] > d[current] + e.cost){
                d[e.to] = d[current] + e.cost;
                q.push(make_pair(d[e.to], e.to));
            }
        }
    }

    return d;
}

int main(){
    int V, E, r; cin >> V >> E >> r;
    Graph G = make_graph(V, E);
    vector<int> d = dijkstra(G, r);

    for(int v = 0; v < V; ++v){
        //int dist = dijkstra(G, r, v);
        //if(dist == INF) cout << "INF\n";
        //else cout << dist << endl;
        if(d[v] < INT_MAX) cout << d[v] << endl;
        else cout << "INF\n";
    }

    return EXIT_SUCCESS;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}


int nextInt() { int x; scanf("%d", &x); return x;}

bool edge[51][51];
bool possible(vector<int> a, vector<int> b, int K, ll ans) {
  CLR(edge, 0);
  for (int i = 0; i <= 50; i++) edge[i][i] = true;
  for (int i = 0; i <= 50; i++) {
    for (int k = 1; k <= 50; k++) {
      if (k <= K || (ans >> k & 1)) edge[i][i % k] = 1;
    }
  }

  for (int k = 0; k <= 50; k++) {
    for (int i = 0; i <= 50; i++) {
      for (int j = 0; j <= 50; j++) {
        edge[i][j] = edge[i][j] || (edge[i][k] && edge[k][j]);
      }
    }
  }
  int t = 0;
  REP(i, a.size()) if (edge[a[i]][b[i]]) t++;
  return t == (int)a.size();
}

int main2() {
  int N = nextInt();
  vector<int> a(N), b(N);
  REP(i, N) a[i] = nextInt();
  REP(i, N) b[i] = nextInt();

  if (!possible(a, b, 50, 0)) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 0;
  for (int k = 50; k > 0; k--) {
    if (possible(a, b, k-1, ans)) {
      // ok
    } else {
      // assert(true == possible(a, b, k, ans))
      ans += (1LL << k);
    }
  }
  cout << ans << endl;
  return 0;
}

int main() {
  for (;!cin.eof();cin>>ws)
    main2();
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9+7;

int main() {
    int n;
    cin >> n;
    vector<int> d(n, 0);
    rep(i, n){
        int tmp;
        cin >> tmp;
        if(i==0 and tmp!=0){
            cout << 0 << endl;
            return 0;
        }
        if(i>0 and tmp==0){
            cout << 0 << endl;
            return 0;
        }
        d[tmp]++;
    }
    ll ans = 1;
    bool flag = true;
    rep(i, n){
        if(i==0) continue;
        if(d[i]==0){
            flag = false;
            continue;
        }
        if(flag){
            rep(j, d[i]){
                ans = (ans*d[i-1]) % 998244353;
            }
        }else{
            cout << 0 << endl;
            return 0;
        }
    }

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 640