akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
ll p2p[N];
ll p2n[N];
int popcnt(int x){
	int ret = 0;
	while(x){
		ret+=x%2;
		x/=2;
	}
	return ret;
}
int calc(int x, int y){
	if(x==0)return 0;
	return 1+calc(x%y,popcnt(x%y));
}
void solve(){
	int n; 
	cin>>n; 
	string s; 
	cin>>s; 
	int cnt = 0;
	for(int i=0;i<n;i++){
		if(s[i]=='1')cnt++;
	}
	if(cnt==0){
		for(int i=0;i<n;i++)cout<<1<<endl;
		return ;
	}
	ll cntp = cnt+1;
	ll cntn = cnt-1;
	ll modp = 0;
	ll modn = 0;
	reverse(s.begin(),s.end());
	p2p[0] = p2n[0] = 1;
	for(int i=0;i<n;i++){
		if(s[i]=='1'){
			modp+=p2p[i];
			modp%=cntp;
			if(cntn)modn+=p2n[i];
			if(cntn)modn%=cntn;
		}
		p2p[i+1]=p2p[i]*2;
		if(cntn)p2n[i+1]=p2n[i]*2;
		p2p[i+1]%=cntp;
		if(cntn)p2n[i+1]%=cntn;
	}
	vector<int>ans(n);
	for(int i=0;i<n;i++){
		if(s[i]=='1'){
			if(cntn==0)ans[i] = 0;
			else{
				ll trt = modn;
				trt = ((trt-p2n[i])%cntn+cntn)%cntn;
				ans[i] = calc(trt,popcnt(trt))+1;
			}
		}
		else{
			
			ll trt = (modp+p2p[i])%cntp;
			ans[i] = calc(trt,popcnt(trt))+1;
		}
	}
	reverse(ans.begin(),ans.end());
	for(int x:ans)cout<<x<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1; 
//	cin>>t; 
	while(t--){
		solve();
	}
	return 0;
}


Measured memory_bytes: 7576
#include<iostream>
#include <string> 
#include <vector>  
#include<math.h>

#define INF 1000000000
#define LM 55555
using namespace::std;


long long A,B,C,H,W,X,Y,N, M,num,x[1000],y[1000],z[1000],ans,dp[8][1000][1000],xyz[8][1000],gomi[1000];
string s[1000], t;
int a[3] = {0,-1,1};
int b[3] = {0,-1,1};
int c[3]=  {0,-1,1};
vector<vector<vector<vector<int>>>> v;




int main() {
	cin >>N;
	if (N % 2 == 0) {
		cout << N << endl;
	}
	else {
		cout << 2 * N << endl;
	}
	return 0;

}




Measured memory_bytes: 256
import sys
input = sys.stdin.readline
from collections import *

def bfs():
    q = deque([0])
    color = [0]*N
    color[0] = 1
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if color[nv]==0:
                color[nv] = -color[v]
                q.append(nv)
    
    return color

N = int(input())
G = [[] for _ in range(N)]

for _ in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
    
cnt = [0]*3

for i in range(1, N+1):
    cnt[i%3] += 1

color = bfs()
m = color.count(1)
ans = [-1]*N

if m<=cnt[0]:
    c0, c1, c2 = 3, 1, 2
    
    for i in range(N):
        if color[i]==1:
            ans[i] = c0
            c0 += 3
    
    for i in range(N):
        if color[i]==-1:
            if c0<=N:
                ans[i] = c0
                c0 += 3
            else:
                if c1<=N:
                    ans[i] = c1
                    c1 += 3
                else:
                    ans[i] = c2
                    c2 += 3
elif m>=N-cnt[0]:
    c0, c1, c2 = 3, 1, 2
    
    for i in range(N):
        if color[i]==-1:
            ans[i] = c0
            c0 += 3
    
    for i in range(N):
        if color[i]==1:
            if c0<=N:
                ans[i] = c0
                c0 += 3
            else:
                if c1<=N:
                    ans[i] = c1
                    c1 += 3
                else:
                    ans[i] = c2
                    c2 += 3
else:
    c0, c1, c2 = 3, 1, 2
    
    for i in range(N):
        if color[i]==1:
            if c1<=N:
                ans[i] = c1
                c1 += 3
            else:
                ans[i] = c0
                c0 += 3
        
    for i in range(N):
        if color[i]==-1:
            if c0<=N:
                ans[i] = c0
                c0 += 3
            else:
                ans[i] = c2
                c2 += 3

print(*ans)

Measured memory_bytes: 93876
#include<stdio.h>

int main(){
	long int a[5],n,min,ans,f;int i;
	scanf("%ld",&n);
	for(i=0;i<5;i++){
		scanf("%ld",&a[i]);
		if(i==0)min = a[0];
		else if(min>a[i])min = a[i];
	}
	if(min>=n) ans = 5;
	else if(n%min==0) ans = n/min+4;
	else ans = n/min+5;
	printf("%ld\n",ans);
	return 0;
}

Measured memory_bytes: 128
#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

void Euclidean(long long a,long long b,long long n){
	if(b!=0) Euclidean(b,a%b,n+1);
	else cout<<a<<" "<<n<<endl;
	return;
}

int main(void){

	long long a,b;

	while(1){

		cin>>a>>b;
		if(a<b) swap(a,b);
		if(a==0&&b==0) break;
		if(b!=0) Euclidean(a,b,0);
		else cout<<"0 0"<<endl;
	}

    return 0;
}

Measured memory_bytes: 1156
#include <bits/stdc++.h>
    #define ld long double
    #define ll long long
    using namespace std;
    
    ll A[10000001]={0};
    
    int main() {
        
        ll i, j;
        for(i=1; i<=10000000; i++)
        {
            for(j=i; j<=10000000; j+=i)
            {
                A[j]++;
            }
        }
        for(i=1; i<=10000000; i++)
        {
            A[i]*=i;
        }
        for(i=2; i<=10000000; i++)
        {
            A[i]+=A[i-1];
        }
        ll n;
        cin>>n;
        cout<<A[n];
        return 0;
    }

Measured memory_bytes: 81756
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

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

int main() {
    string s;
    cin >> s;
    int a = (int)s.find("A");
    int b = (int)s.rfind("Z");
    cout << b-a+1 << endl;
}


Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
using namespace std;
#define Would
#define you


int main() {
	int a, b;
	cin >> a >> b;
	cout << abs(a - b) << endl;
}



Measured memory_bytes: 3120
n = input()
ar = map(int, input().split(' '))

product = 1
exe = 1
maxP = 1000000000000000000

for val in ar:
    product *= val
    # print(product)
    if(product > maxP):
        product = -1
        break

for e in ar:
    if(e == 0):
        exe = 0

print(product * exe)
# print(exe)

Measured memory_bytes: 19268
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T>
void cout_vec(const vector<T> &vec){
  for(auto itr:vec) cout<<itr<<' ';
  cout<<endl;
}

typedef pair<ll,ll> P;
const ll mod=1e9+7;
const ll inf=1e15;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin>>n;
  vector<int> p(n),ind(n);
  rep(i,n){
    cin>>p[i];
    p[i]--;
    ind[p[i]]=i;
  }
  set<int> cnt;
  ll ans=0;
  for(int i=n-1;i>=0;i--){
    cnt.insert(ind[i]);
    auto itr=cnt.find(ind[i]);
    vector<int> l(2,-1),r(2,n);
    rep(j,2){
      if(itr==begin(cnt)) break;
      itr--;
      l[j]=*itr;
    }
    itr=cnt.find(ind[i]);
    itr++;
    rep(j,2){
      if(itr==end(cnt)) break;
      r[j]=*itr;
      itr++;
    }
    ans+=(ll)(i+1)*((l[0]-l[1])*(r[0]-ind[i])+(r[1]-r[0])*(ind[i]-l[0]));
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 5760
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def mat_mul_opt(_mat_info):
    rc_info = list(map(int, _mat_info[0]))
    rc_info.extend(list(map(lambda x: int(x[1]), _mat_info[1:])))

    for l in range(2, mat_num + 1):
        for i in range(1, mat_num - l + 2):
            j = i + l - 1
            ans[i][j] = 1e7
            for k in range(i, j):
                ans[i][j] = min(ans[i][j], ans[i][k] + ans[k + 1][j] +
                                rc_info[i - 1] * rc_info[k] * rc_info[j])

    return ans[1][mat_num]


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    mat_num = int(_input[0])
    mat_info = tuple(map(lambda x: x.split(), _input[1:]))
    # assert len(mat_info) == mat_num

    ans = [[0] * (mat_num + 1) for _ in range(mat_num + 1)]
    print(mat_mul_opt(mat_info))

Measured memory_bytes: 7952
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().solve();
        }

        public void solve()
        {
            var CinTemp = ReadLine().Split(' ');
            int a = int.Parse(CinTemp[0]);
            int b = int.Parse(CinTemp[1]);
            if(a <= 5) WriteLine(0);
            else if(a <= 12) WriteLine(b/2);
            else WriteLine(b);
        }
    }
}

Measured memory_bytes: 11092
//Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 2000005
#define mod 1000000007
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/
int add(int x,int y) {
  return ((x+y)%mod+mod)%mod;
}
int mul(int x,int y) {
  return (ll)x*y%mod;
}
int mypow(int x,int c) {
  int ret = 1;
  while(c>0) {
    if(c&1) {
      ret = mul(ret,x);
    }
    x = mul(x,x);
    c/=2;
  }
  return ret;
}

int fac[N],invFac[N];
int combi(int n,int r) {
  if(n<r) {
    return 0;
  }
  int ret = mul(fac[n],invFac[r]);
  ret = mul(ret, invFac[n-r]);
  return ret;
}

int f(int r,int c) {
  int ret = 0,pp = r+1;
  ret = pp;
  for(int i=1;i<=c;++i) {
    int tmp = add(mul(r+1,pp), mul(i,combi(r+i,i)));
    tmp = mul(tmp, mypow(i+1,mod-2));
    ret = add(ret,tmp);
    pp = tmp;
  }
  return ret;
}

void solve() {
  int r1,c1,r2,c2;
  scanf("%d %d %d %d ", &r1,&c1,&r2,&c2);
  fac[0] = invFac[0] = 1;
  for(int i=1;i<N;++i) {
    fac[i] = mul(fac[i-1],i);
    invFac[i] = mypow(fac[i],mod-2);
  }
  int ret = add(f(r2,c2),f(r1-1,c1-1));
  ret = add(ret, -add(f(r1-1,c2),f(r2,c1-1)));

  //printf("%d\n", f(r2,c2));
  printf("%d\n", ret);
}

int main() {
    //freopen("input.txt","r",stdin);
    solve();
    return 0;
}


Measured memory_bytes: 15872
t = input()
n = int(input())
for i in range(n):
    orders = input().split()
    order = orders[0]
    a = int(orders[1])
    b = int(orders[2])

    if order == "replace":
        word = orders[3]
        t = t[:a] + word + t[b+1:]
    if order == "print":
        print(t[a:b+1])
    if order == "reverse":
        t = t[:a] + t[a:b+1][::-1]+ t[b+1:]


Measured memory_bytes: 5608
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def isprime(x):
    if x == 2:
        return True
    if x % 2 == 0:
        return False
    for m in range(3, x):
        if m ** 2 > x: break
        if x % m == 0:
            return False
    return True

def main():
    # x,y = list(map(int, input().split()))
    n = int(input())
    ans = 0
    for _ in range(n):
        ans += isprime(int(input()))
    print(ans)

main()



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

int main() {
  ll n;
  cin >> n;
  vector<pair<ll, int>> a(n);
  rep(i, n) {
    cin >> a[i].first;
    a[i].second = i;
  }

  sort(a.begin(), a.end(), greater<pair<ll, int>>());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));

  rep(i, n) {
    ll ai = a[i].first;
    int x = a[i].second;
    rep2(j, -1, i + 1) {
      int l = j;
      int r = n - 1 - (i - j - 1);
      ll dpl = j < 0 ? -1 : dp[i][j] + ai * abs(x - l);
      ll dpr = j == i ? -1 : dp[i][j + 1] + ai * abs(x - r);
      dp[i + 1][j + 1] = max(dpl, dpr);
    }
  }

  ll ans = -1;
  rep(i, n + 1) ans = max(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 34624
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<bitset>
#define ll long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,0,n)
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define INF ((1LL<<62)-(1LL<<31))
#define MOD 1000000007
using namespace std;

int main(){
  ll N;
  cin>>N;
  vector<ll> A;
  vector<ll> B;
  REP(i,N){
    ll a;
    cin>>a;
    A.push_back(a);
  }
  SORT(A);
  REVERSE(A);
  
  bool CAN=false;
  REP(i,N){
    if(A[i]==A[i+1]){
      CAN=true;
      B.push_back(A[i]);
      i++;
    }
  }
  
  if(CAN) cout<<B[0]*B[1]<<endl;
  else cout<<0<<endl;
}


Measured memory_bytes: 1908
N, M = list(map(int, input().split()))
XYZ = [list(map(int, input().split())) for _ in range(M)]

class Union:
  def __init__(self, num):
    self.T = [-1 for i in range(num)]

  def root(self, x):
    if self.T[x] < 0: return x
    return self.root(self.T[x])

  def size(self, x):
    return -self.T[self.root(x)]

  def merge(self, x, y):
    a = self.root(x)
    b = self.root(y)
    if a == b: return False
    if self.size(a) < self.size(b): a, b = b, a
    self.T[a] += self.T[b]
    self.T[b] = a
    return True

uni = Union(N)

for i in range(M):
  x, y, z = XYZ[i]
  x -= 1
  y -= 1
  z -= 1
  uni.merge(x, y)

ans = 0
for i in range(N):
  if uni.T[i] < 0:
    ans += 1

print(ans)

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

template<int MOD>
struct mod_int {
	static const int mod = MOD;
	unsigned x;
	mod_int() : x(0) { }
	mod_int(int sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	mod_int(long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	mod_int &operator+=(mod_int that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator-=(mod_int that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator*=(mod_int that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	mod_int &operator/=(mod_int that) { return *this *= that.inverse(); }

	mod_int operator+(mod_int that) const { return mod_int(*this) += that; }
	mod_int operator-(mod_int that) const { return mod_int(*this) -= that; }
	mod_int operator*(mod_int that) const { return mod_int(*this) *= that; }
	mod_int operator/(mod_int that) const { return mod_int(*this) /= that; }

	mod_int inverse() const {
		long long a = x, b = MOD, u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		return mod_int(u);
	}
};

template<int MOD>
istream& operator >> (istream& is, mod_int<MOD>& val) {
	long long x;
	is >> x; val = x;
	return is;
}

template<int MOD>
ostream& operator << (ostream& os, const mod_int<MOD>& val) {
	os << val.get();
	return os;
}

using mint = mod_int<998244353>;

const int MAX = 2e6;

bool inited = false;
mint fac[MAX + 1];
mint rfac[MAX + 1];

void init() {
	inited = true;
	fac[0] = 1;
	for (int i = 1; i <= MAX; i++) {
		fac[i] = fac[i - 1] * i;
	}
	rfac[MAX] = fac[MAX].inverse();
	for (int i = MAX; i >= 1; i--) {
		rfac[i - 1] = rfac[i] * i;
	}
}

mint nPr(int n, int r) {
	if (!inited) init();
	return r < 0 || n < r ? 0 : fac[n] * rfac[n - r];
}

mint nCr(int n, int r) {
	if (!inited) init();
	return r < 0 || n < r ? 0 : fac[n] * rfac[n - r] * rfac[r];
}

mint nHr(int n, int r) {
	if (!inited) init();
	return r == 0 ? 1 : nCr(n + r - 1, r);
}

int main()
{
	int N;
	ll A, B, K;
	cin >> N >> A >> B >> K;
	mint res = 0;
	for (int i = 0; i <= N; i++) if (i * A <= K && (K - i * A) % B == 0) {
		int j = (K - i * A) / B;
		res += nCr(N, i) * nCr(N, j);
	}
	cout << res << endl;
	return 0;
}


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

int main(){
  int Nm, Wm;
  cin >> Nm >> Wm;
  int wt[101], vt[101];
  for(int i=1;i<=Nm;i++)
    cin >> vt[i] >> wt[i];
  
  int t[101][10001];
  for(int i=0;i<=100;i++) t[i][0]=0;
  for(int i=0;i<=10000;i++) t[0][i]=0;

  for(int i=1;i<=Nm;i++){
    for(int w=1;w<=Wm;w++){
      if(w>=wt[i]) t[i][w]=max(t[i-1][w],t[i-1][w-wt[i]]+vt[i]);
      else t[i][w]=t[i-1][w];
    }
  }

  cout << t[Nm][Wm] << endl;

  return 0;
}

Measured memory_bytes: 7040
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

ll pow(ll a, ll b, ll m)
{
    ll  ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}



int main()
{
	IOS;
	int t=1; //cin>>t;
	while(t--){
	    int n,k; cin>>n>>k;
	    int a[n+1];
	    for(int i=1;i<=n;i++) cin>>a[i];
	    ll dp[n+1][k+1];
	    ll prevprefixsum[k+1];
	    
	    for(int i=0;i<=k;i++) dp[0][i]=0,prevprefixsum[i]=1;
	    for(int i=0;i<=n;i++) dp[i][0]=1;

        for(int i=1;i<=n;i++){
            for(int j=1;j<=k;j++){
                if(j<=a[i]) dp[i][j]=prevprefixsum[j]%MOD;
                else dp[i][j]=prevprefixsum[j]-prevprefixsum[j-a[i]-1];
                dp[i][j]=(dp[i][j]+MOD)%MOD;
            }
            for(int j=1;j<=k;j++)
              prevprefixsum[j]=(dp[i][j]+prevprefixsum[j-1])%MOD;
        }
        //for(int i=0;i<=k;i++) cout<<prevprefixsum[i]<<" "; 
        cout<<dp[n][k];
        /*for(int i=0;i<=n;i++){
            for(int j=0;j<=k;j++){cout<<dp[i][j]<<" ";}
            cout<<endl;}*/
	    cout<<endl;
	}  
}

Measured memory_bytes: 83176
class Heap
  attr_accessor(:data, :size)

  def initialize()
    @data = []
    @size = 0
  end

  def push(x)
    @data[@size] = x
    i = @size
    j = ((i + 1) / 2 ) - 1
    issorted = false
    while j >= 0 && !issorted
      if @data[i] > @data[j]
        @data[i], @data[j] = @data[j], @data[i]
        i = j
        j = ((i + 1) / 2 ) - 1
      else
        issorted = true
      end
    end
    @size += 1
  end

  def pop()
    if @size == 0
      nil
    else
      re = @data[0]
      @size -= 1
      @data[0] = @data[size]
      i = 0
      j1 = (i+1) * 2 - 1
      j2 = (i+1) * 2
      issorted = false
      while size > j1 && !issorted
        if size > j2
          max_i = @data[j1] > @data[j2] ? j1 : j2
          if @data[i] >= @data[max_i]
            issorted = true
          else
            @data[i], @data[max_i] = @data[max_i], @data[i]
            i = max_i
            j1 = (i+1) * 2 - 1
            j2 = (i+1) * 2
          end
        else
          if @data[i] >= @data[j1]
            issorted = true
          else
            @data[i], @data[j1] = @data[j1], @data[i]
            i = j1
            issorted = true
          end
        end
      end
      re
    end
  end

end

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

works = Array.new(10**5+1).map{Array.new}
n.times do |i|
  a,b = gets.chomp.split(" ").map(&:to_i)
  works[a].push(b)
end

ans = 0
heap = Heap.new
for i in 1..m
  works[i].each do |x|
    heap.push(x)
  end
  highest = heap.pop()
  if highest != nil
    ans += highest
  end
end
puts ans

Measured memory_bytes: 12216
#include<stdio.h>
int main()
{
    int i;
    char a[5];
    for(i=1;i<=5;i++){
        scanf("%c",&a[i]);
        }
    if(a[1]==a[2]&&a[2]==a[3]){
        printf("Yes");
        }
    else if(a[2]==a[3]&&a[3]==a[4]){
        printf("Yes");
        }
    else{
        printf("No");
        }
    return 0;
}

Measured memory_bytes: 128
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
l = [["#"] for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            l[i].append(0)
        else:
            l[i].append("#")
    l[i].append("#")
l = [["#" for i in range((w+2))]] + l + [["#" for i in range(w+2)]]
pos = []
for i in range(1, h+1):
    for j in range(1, w+1):
        if l[i][j] == "#":
            pos.append([i, j])
for i in pos:
    x, y = i
    for j in [[x-1, y-1], [x, y-1], [x+1, y-1], [x-1, y], [x+1, y], [x-1, y+1], [x, y+1], [x+1, y+1]]:
        if l[j[0]][j[1]] != "#":
            l[j[0]][j[1]] += 1
for i in range(1, h+1):
    print("".join(list(map(str, l[i][1:-1]))))

Measured memory_bytes: 9012
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'

using namespace std;

typedef long long ll;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = INT_MAX;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main() {
  int N,M;
  cin >> N >> M;
  vint p(N),inp(N);
  repeat(i,N){
    cin >> p[i];
    p[i]--;
    inp[p[i]] = i;
  }
  UnionFind uf(N);
  repeat(i,M){
    int x,y;
    cin >> x >> y;
    x--;y--;
    uf.unionSet(x,y);
  }
  ll ans = 0;
  repeat(i,N){
    if(uf.findSet(inp[i],i)){
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


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


def solve(N: int, M: int):
    ret = 0
    i = 1
    while i * i <= M:
        if M % i == 0:
            if i >= N:
                ret = max(ret, M // i)
            if M // i >= N:
                ret = max(ret, i)
        i += 1
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    solve(N, M)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace D
{
    class Program
    {
        const long r = 1000000007;
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            int N = int.Parse(line);
            long[] A = new long[N < 6 ? 6 : N+1];
            A[1] = 0;
            A[2] = 0;
            A[3] = 1;
            A[4] = 1;
            A[5] = 1;
            for(int i = 6; i <= N; i++)
            {
                A[i] = A[i - 1] + A[i - 3];
                A[i] %= r;
            }

            Console.WriteLine(A[N]);
        }
    }
}


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

int main(){
  string a,b;
  cin >> a;
  b  = a;
  reverse(b.begin(),b.end());
  
  for(int i=0; i<a.size(); i++){
    if ( a.at(i) != b.at(i)) {
      cout << "No" << endl;
      return 0;
    }
  }
 
 cout << "Yes" << endl;
  
 return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define MAXN	100005
#define P	1000000007
long long f[MAXN], sum[MAXN];
long long a, b, A[MAXN];
bool tmp[MAXN];
int pre[MAXN];
int main() {
	int n;
	scanf("%d%lld%lld", &n, &a, &b);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &A[i]);
	if (a < b) swap(a, b);
	for (int i = 3; i <= n; i++)
		if (A[i] - A[i - 2] < b) {
			printf("0\n");
			return 0;
		}
	int now = 0;
	for (int i = 2; i <= n; i++) {
		tmp[i] = A[i] - A[i - 1] < b;
		pre[i] = now;
		if (tmp[i]) now = i - 1;
	}
	A[n + 1] = 3e18;
	pre[n + 1] = now;
	now = 0;
	f[0] = sum[0] = 1;
	for (int i = 1; i <= n + 1; i++) {
		while (A[i] - A[now + 1] >= a) now++;
		if (now >= pre[i]) {
			if (pre[i] >= 1) f[i] = (sum[now] - sum[pre[i] - 1]) % P;
			else f[i] = sum[now] % P;
		} else f[i] = 0;
		sum[i] = (f[i] + sum[i - 1]) % P;
	}
	printf("%lld\n", (f[n + 1] + P) % P);
	return 0;
}

Measured memory_bytes: 3200
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <bits/stdc++.h>


using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,m,n) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cout(n) cout<<fixed<<setprecision(n)

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,d;
    cin>>n>>d;
    vector<vector<ll>> v(n,vector<ll>(d));
    FOR(i,0,n)FOR(j,0,d)cin>>v[i][j];
    ll ans = 0;
    FOR(i,0,n){
      FOR(j,i+1,n){
        ld a=0;
        FOR(k,0,d){
          a += abs(v[i][k] - v[j][k])*abs(v[i][k] - v[j][k]);
//          cout<<v[i][k]<<" "<<v[j][k]<<" "<<a<<endl;
        }
        if(sqrt(a) == (ll)sqrt(a)) ans++;
//        cout<<sqrt(a)<<" "<<a<<endl;
      }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 256
from collections import deque

N = int(input())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
C = list(map(int, input().split()))

C.sort()
C = C[::-1]

graph = [[] for _ in range(N + 1)]
check = [1] * (N + 1)
for A, B in AB:
  graph[A].append(B)
  graph[B].append(A)

answer = [0] * (N + 1)
stack = deque()
stack.append(1)
check[1] = 0
ci = 0

while stack:
  s = stack.popleft()
  answer[s] = C[ci]
  ci += 1
  for g in graph[s]:
    if check[g]:
      check[g] = 0
      stack.append(g)
      
print(sum(C) - max(C))
print(*answer[1:])  

Measured memory_bytes: 51800
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x*x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(d)  (d).begin(),(d).end()
#define RALL(d) (d).rbegin(), (d).rend()
#define PB push_back
#define MP make_pair
#define SZ(d) int((d).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,d,b) for(int i=(d);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(d) memset((d), 0 ,sizeof(d))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
int main()
{
	int h, w;
	cin >> h >> w;
	VVI c(10), a(h);
	REP(i, 10) c[i] = VI(10);
	REP(i, 10) REP(j, 10) cin >> c[i][j];
	REP(i, h) a[i] = VI(w);
	REP(i, h) REP(j, w) cin >> a[i][j];
	VI r = { 0, 2, 3, 4, 5, 6, 7, 8, 9 };
	int tmp;
	VI ans(10);
	const int inf = (1 << 29);
	REP(i, 10) ans[i] = inf;
	do {
		FOR(j, 0, 9) {
			tmp = 0;
			REP(i, j) tmp += c[r[i]][r[i + 1]];
			tmp += c[r[j]][1];
			ans[r[0]] = min(ans[r[0]], tmp);
		}
	} while (next_permutation(r.begin(), r.end()));
	ans[1] = 0;
	LL res = 0;
	REP(i, h) REP(j, w) if (a[i][j] != -1) res += ans[a[i][j]];
	cout << res << endl;
	return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int c=0,mx=0;
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
    c=0;
    if(s[i]=='R')
    {
     while(s[i]=='R')
     {
         c++;
         i++;
         if(i>=s.length()){break;}
     }


    }
    if(mx<c){mx=c;}
       if(i>=s.length()){break;}
}
cout<<mx<<endl;


}


Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define INF 1e14
#define dd double
#define MOD 1000000007
#define my_sizeof(type) ((char *)(&type+1)-(char*)(&type)) 
#define nl cout<<endl
#define fill(a,val) memset(a,val,sizeof(a))
#define mp make_pair
#define endl "\n"
#define pb push_back
#define ff first    
#define ss second
#define all(v) v.begin(),v.end()
#define s(ar,n) sort(ar,ar+n)
#define rs(ar,n) sort(ar, ar+n, greater<ll>()) 
#define oa(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" ";nl
#define cn(a,n) for(ll i=0;i<n;i++)cin>>a[i];
#define maxa(ar,N) *max_element(ar,ar+N)
#define mina(ar,N) *min_element(ar,ar+N)
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define printclock cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n";
ll power(ll x, ll y){ll res=1;x=x%MOD;while(y>0){if(y&1)res = (res*x)%MOD;y = y>>1;x = (x*x)%MOD;}return res%MOD;}  
ll logtwo(ll n){if(n==1)return 0;return logtwo(n/2)+1;}
void ceilsearch(ll ar[],ll low,ll high,ll x,ll &y){if(low>high)return;ll mid=(low+high)/2;if(ar[mid]>=x){y=mid;ceilsearch(ar,low,mid-1,x,y);}else{ceilsearch(ar,mid+1,high,x,y);}}
signed main()
{
    fastio();   
    ll K;
    cin>>K;
    ll ans=0;
    for(ll i=1;i<=K;i++)
    {
        for(ll j=1;j<=K;j++)
        {
            for(ll k=1;k<=K;k++)
                ans+=__gcd(i,__gcd(j,k));
        }
    }
    cout<<ans<<endl;
}   

Measured memory_bytes: 3640
#include <stdio.h>
#define N 100
#define X 10000000

int main(){
  int A[N+1],B[N+1][N+1];
  int n,i,j,k,l,o;

  scanf("%d",&n);

  for(i=1 ; i<=n ; i++){
    scanf("%d %d",&A[i-1],&A[i]);
  }

  for(i=1 ; i<=n ; i++){
    B[i][i] = 0;
  }

  for(k=2 ; k<=n ; k++){
    for(i=1 ; i<=n-k+1 ; i++){
      j = i + k - 1;
      B[i][j] = X;

      for(l=i ; l<j ; l++){
        o = B[i][l] + B[l+1][j] + A[i-1] * A[l] * A[j];
        B[i][j] = (B[i][j] > o) ? o : B[i][j];
      }
    }
  }

  printf("%d\n",B[1][n]);

  return 0;
}



Measured memory_bytes: 2112
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3+5;

bool visited [N];
vector<int>adjList[N];
bool compared[N][N];

int main () {
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, x, y;
    cin >> n >> x >> y;
    for (int i = 1; i < n; i++) {
        adjList[i].push_back(i+1);
        adjList[i+1].push_back(i);
    }


    adjList[x].push_back(y);
    adjList[y].push_back(x);
    /*for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j+1 || j == i+1 || ((i == x && j == y) || (j == x && i == y))) cout << 1 << " ";
            else cout << 0 << " ";
        }
        cout << endl;
    }*/

    int discount [N] = {};
    int dist [N];
    for (int c = 1; c <= n; c++) {
        memset(visited, 0, sizeof visited);
        queue<int>q;
        q.push(c);
        dist[c] = 0;
        int node;
        while (!q.empty()) {
            node = q.front();
            q.pop();
            if (!compared[c] [node]) {
                discount[dist[node]]++;
                compared[c] [node] = true;
                compared[node] [c] = true;
            }
            for (auto i : adjList[node])
            {
                if (visited[i])
                    continue;
                dist[i] = dist[node] + 1;
                visited[i] = true;
                q.push(i);
            }
        }
    }

    for (int i = 1; i <= n-1; i++) {
        cout << discount[i] << endl;
    }

    return 0;
}


Measured memory_bytes: 4352
var dp, back: array[0..5000, 0..5000] of longint;
    c: array[1..3000] of char;
    s, t: ansistring;
    i,j,k: longint;
begin
    readln(s);
    readln(t);
    for i:=1 to length(s) do 
    begin
        for j:=1 to length(t) do
        begin
            if s[i]=t[j] then
            begin
                dp[i,j]:=dp[i-1][j-1]+1;
                back[i,j]:=0;
            end
            else
            begin
                if dp[i,j-1]>dp[i-1,j] then
                begin
                    dp[i,j]:=dp[i,j-1];
                    back[i,j]:=1;
                end
                else
                begin
                    dp[i,j]:=dp[i-1,j];
                    back[i,j]:=2;
                end;
            end;
        end;
    end;
    k:=0;
    while k<dp[length(s),length(t)] do
    begin
        if back[i,j]=2 then
            dec(i)
        else if back[i,j]=1 then
            dec(j)
        else
        begin
            inc(k);
            c[k]:=s[i];
            dec(i);
            dec(j);
        end;
    end;
    for i:=k downto 1 do
        write(c[i]);
    writeln;
end.


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

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

int f(int n){
	int sum=0;
	while(n>0){
		sum +=n%10;
		n/=10;
	}
	return sum;
}

int main() {
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n) cin>>a[i];
	
	int Alice=0,Bob=0;
	sort(a.begin(),a.end(),greater<int>());
	rep(i,n){
		if(i%2==0) Alice+=a[i];
		else Bob+=a[i];
	}
	cout<<Alice-Bob<<endl;
	return 0;
}

Measured memory_bytes: 256
readline = open(0).readline
writeline = open(1, 'w').writelines

s = set()


def insert(x):
    s.add(x)
    ans.append("%d\n" % len(s))


def find(x):
    ans.append("%d\n" % (x in s))


C = [insert, find, s.discard].__getitem__
Q = int(readline())
ans = []

for _ in range(Q):
    t, x = map(int, readline().split())
    C(t)(x)

writeline(ans)



Measured memory_bytes: 34996
#include<cstdio>
#include<cmath>
#include<climits>
#include<algorithm>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<utility>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<unordered_set>
#include<unordered_map>
#include<set>
#include<map>
#include<list>

using namespace std;
using ll = long long;

#define in(s) cin >> s
#define out(s) cout << s << endl
#define rep(i,n) for(int i=0; i<n; i++)
#define rept(i,a,b) for(int i=a; i<b; i++)
#define pcnt __builtin_popcount
#define pcntll(x) __builtin_popcountll(x)
#define DMAX DBL_MAX;
#define IMIN INT_MIN;
#define IMAX INT_MAX;

const long long MOD = 1e9+7;
const long long MAX = 3e5+1;

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);

    ll A, B, C, K;
    in(A); in(B); in(C); in(K);
    ll ans = min(A, K);
    if (K <= A) {
        ans = K;
    }
    else {
        K -= A;
        if (K <= B) ans = A;
        else {
            K -= B;
            ans = A - K;
        }
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // std::ifstream in("input.txt");
    // std::cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    vector<string> s(n);
    rep(i, n) cin >> s[i];
    ll ans = 0;
    rep(i, n) {
        bool f = true;
        rep(ii, n) {
            rep(iii, n) {
                if (s[ii][(iii + (n - i)) % n] != s[iii][(ii + (n - i)) % n]) {
                    f = false;
                    break;
                }
            }
            if (!f) break;
        }
        if (f) ans += n;
    }
    cout << ans << endl;
    return 0;
}


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

#define M 100000
int main(){
  int n,g[M],i,j;
  long long a=0;
  cin>>n;
  for(i=0;i<n;i++){
      cin>>g[i];
  }
  sort(g,&g[n]);
  for(i=0;i<n;i++){
    a+=g[i]-(i+1);
  }
  cout<<a<<endl;
  return 0;
}



Measured memory_bytes: 3552
/*----------------------------------------------------*
* लेखक --> अमित सिंह
* संस्थान --> राष्ट्रीय प्रौद्योगिकी संस्थान, कुरुक्षेत्र
*-----------------------------------------------------*/
   #include <bits/stdc++.h>
   using namespace std;
   #define ll long long
   #define w(t) int t; cin>>t; while(t--)
   #define fo(i,n) for(int i=0;i<n;i++)
   #define endl "\n" 
   #define MOD 1000000007
   void Tez(){
      ios::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
   }
   int main(){
      Tez();
      int n;
      cin>>n;
      ll a[n];
      fo(i,n) cin>>a[i];
      ll ans=0;
      ll arr[n-1];
      ll sum=0;
      for(int i=0;i<n;i++) sum=(sum+a[i])%MOD;
      for(int i=0;i<n-1;i++){
         arr[i]=((sum-a[i])%MOD+MOD)%MOD;
         // arr[i]=sum-a[i];
         sum=arr[i];
      }
      for(int i=0;i<n-1;i++){
         ans=(ans+a[i]*arr[i])%MOD;
      }
      cout<<ans;
      return 0;
   }



Measured memory_bytes: 6756
import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class Main {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		for (;;) {
			int n = sc.nextInt();
			if (n == 0) break;
			int min = 1000, max = 0, sum = 0;
			for (int i = 0; i < n; i++) {
				int a = sc.nextInt();
				sum += a;
				min = min(a, min);
				max = max(a, max);
			}
			System.out.println((sum - min - max) / (n - 2));
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new Main().run();
	}
}

Measured memory_bytes: 20944
# 解説AC、辺の張り方が難しすぎる
from collections import deque
INF = 1 << 23


class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for _ in range(self.N)]

    def add_edge(self, u, v, cap):
        self.G[u].append([v, cap, len(self.G[v])])
        self.G[v].append([u, 0, len(self.G[u])-1])

    def bfs(self, s):
        self.level = [-1] * self.N
        que = deque([s])
        self.level[s] = 0
        while que:
            u = que.popleft()
            for (v, cap, _) in self.G[u]:
                if cap > 0 and self.level[v] < 0:
                    self.level[v] = self.level[u] + 1
                    que.append(v)

    def dfs(self, u, t, f):
        if u == t:
            return f
        for i in range(self.progress[u], len(self.G[u])):
            self.progress[u] = i
            (v, cap, rev) = self.G[u][i]
            if cap > 0 and self.level[u] < self.level[v]:
                d = self.dfs(v, t, min(f, cap))
                if d > 0:
                    self.G[u][i][1] -= d
                    self.G[v][rev][1] += d
                    return d
        return 0

    def max_flow(self, s, t):
        flow = 0
        INF = 10**9
        while True:
            self.bfs(s)
            if self.level[t] < 0:
                return flow
            self.progress = [0] * self.N
            current_flow = self.dfs(s, t, INF)
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, INF)


def main():
    H, W = (int(i) for i in input().split())
    A = [input() for i in range(H)]
    F = Dinic(H+W+2)  # 0,1,...,W-1,0,..,H-1,s,t
    s = H+W
    t = H+W+1
    for h in range(H):
        for w in range(W):
            if A[h][w] == "S":
                F.add_edge(s, h+W, INF)
                F.add_edge(s, w, INF)
            elif A[h][w] == "T":
                F.add_edge(h+W, t, INF)
                F.add_edge(w, t, INF)
            if A[h][w] != ".":
                F.add_edge(h+W, w, 1)
                F.add_edge(w, h+W, 1)

    ans = F.max_flow(s, t)
    if INF <= ans:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 78972
import java.util.*;

public class Main{
    int h,w,c;
    int max;
    int from,to;
    int ans;
    int[][] panel2;

    public static void main(String[] args){
	new Main().solve();
    }

    void solve(){
	Scanner sc = new Scanner(System.in);
	h = sc.nextInt();
	while(h!=0){
	    ans = 0;
	    w = sc.nextInt();
	    c = sc.nextInt();
	    int[][] panel = new int[h][w];
	    for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
		    panel[i][j] = sc.nextInt();
		}
	    }
	    
	    max = Integer.MIN_VALUE;
	    identically(0,panel);
	    System.out.println(max);

	    h = sc.nextInt();
	}
    }

    void identically(int count, int[][] panel){
	if(count==4){
	    if(panel[0][0]==c){return;}
	    from = panel[0][0]; to = c;
	    panel2 = panel;
	    change(0,0);
	    ans = 0;
	    from = c; to = 7;
	    change(0,0);
	    max = Math.max(max,ans);
	    return;
	}

	int first = panel[0][0];
	panel2 = new int[h][w];
	for(int i=0; i<h; i++){
	    for(int j=0; j<w; j++){
		panel2[i][j] = panel[i][j];
	    }
	}
	for(int i=1; i<=6; i++){
	    if(i==first)continue;
	    from = first; to = i;
	    identically(count+1,change(0,0));
	    for(int k=0; k<h; k++){
		for(int j=0; j<w; j++){
		    panel2[k][j] = panel[k][j];
		}
	    }
	}
    }

    int[] dx = {1,-1,0,0};
    int[] dy = {0,0,1,-1};
    int[][] change(int x, int y){
	panel2[y][x] = to;
	ans++;
	for(int k=0; k<4; k++){
	    int nx = x+dx[k];
	    int ny = y+dy[k];
	    if(ny<h && ny>=0 && nx<w && nx>=0 && panel2[ny][nx]==from){
		panel2 = change(nx,ny);
	    }
	}
	
	return panel2;
    }
}

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

using namespace std;

#define M_DEBUG if (0)

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
#define rreps(i, n) for(int i=((int)(n)); i>0; --i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)

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

using lint = long long;
typedef unsigned long long int ullint;


/******************************************************************************
	Main
******************************************************************************/
#define L_N_MAX 101
#define L_V_MAX 100100
const long long INF = 1LL<<60;

int main()
{
	int n = 0, w = 0;
	cin >> n >> w;
	assert(n >= 1 && n <= 100);
	assert(w >= 1 && w <= pow(10, 9));

	int ws[L_N_MAX] = {0};
	ullint vs[L_N_MAX] = {0};
	rep(i, n){
		cin >> ws[i] >> vs[i];
		assert(ws[i] >= 1 && ws[i] <= w);
		assert(vs[i] >= 1 && vs[i] <= pow(10, 3));
	}

	// ullint sum_list[L_N_MAX][L_W_MAX] = {0}; // [-] RE: return code 139
	ullint **sum_list = new ullint*[L_N_MAX]; // 品物 i までを 価値が v 以上になるように選んだ品物 (w, v) について、w の合計値
	rep(i,L_N_MAX) {
		sum_list[i] = new ullint[L_V_MAX]();
		for (ullint sum_v = 0; sum_v < L_V_MAX; sum_v++) {
            sum_list[i][sum_v] = INF;
        }
	}
    sum_list[0][0] = 0;

	rep(i, n){
		rep(sum_v, L_V_MAX) {
            // i 番目の品物を取る場合の価値
			if (sum_v >= vs[i]) {
				chmin(sum_list[i+1][sum_v], sum_list[i][sum_v - vs[i]] + ws[i]); // 今追加しようとしている品物の価値 vs[i] を除いた価値 sum_v - vs[i] での最小重さ + 品物の重さ ws[i]
			}
			// 取らない
			chmin(sum_list[i+1][sum_v], sum_list[i][sum_v]);
            M_DEBUG { cout << "sum_list[" << n << "][" << sum_v << "] = " << sum_list[n][sum_v] << endl; }
		}
	}

    // sum_list[n][sum_v] <= w なるインデックスのうち、sum_vの最大値
    ullint ret = 0;
    rep(sum_v, L_V_MAX) {
        M_DEBUG cout << "sum_list[n][" << sum_v << "] = " << sum_list[n][sum_v] << endl;
        if (sum_list[n][sum_v] <= w) {
            ret = sum_v;
        } 
    }
	cout << ret << endl;

	rep(i,L_N_MAX) {
		delete[] sum_list[i];
	}
	delete[] sum_list;

	return 0;
}


Measured memory_bytes: 79488
#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int INF = 1e9;
const int MOD = INF+7;
#define rep(i,N) for(int (i)=0;(i)<(N);++(i))
#define rrep(i,N) for(int (i)=(N-1);(i)>0;--i)
#define FOR(i,j,N) for(int (i)=(j);(i)<(N);++(i))
#define put(n) cout<<(n)<<endl;
#define all(v) v.begin(),v.end()
#define MP make_pair
#define pb push_back
using P = pair<int,int>;
//d

signed main(){
    int N;
    cin >> N;
    vector<P> job(N);

    rep(i,N){
        int a,b;
        cin >> a >> b;
        job[i] = {b,a};
    }

    sort(all(job));

    bool can = true;
    vector<int> time(N+1,0);
    for(int i=1;i<=N;i++){
        time[i] = time[i-1] + job[i-1].second;
        if(time[i] > job[i-1].first) can = false;
    }

    if(can) cout << "Yes" << endl;
    else cout << "No" << endl;
}


Measured memory_bytes: 4992
#include<iostream>

#define N 100
#define M 50

using namespace std;

struct Picture{
  int m;
  int D[M][M];
  void rot90(){
    int newD[M][M];
    for(int i = 0; i < m; ++i){
      for(int j = 0; j < m; ++j){
	newD[i][j] = D[m-j-1][i];
      }
    }
    for(int i = 0; i < m; ++i){
      for(int j = 0; j < m; ++j){
	D[i][j] = newD[i][j];
      }
    }
  }
  void print(){
    cout << " PIC PRINT : " << endl;
    for(int i = 0; i < m; ++i){
      for(int j = 0; j < m; ++j){
	cout << D[i][j] << ' ';
      }
      cout << endl;
    }
  }
  pair<int,int> getFirst(){
    for(int i = 0; i < m; ++i){
      for(int j = 0; j < m; ++j){
	if( D[i][j] > -1 ){
	  return make_pair(i,j);
	}
      }
    }
  }
};


struct Landscape{
  int n;
  int D[N][N];
  bool match(int bi, int bj, const Picture& p){
    for(int i = 0; i < p.m; ++i){
      for(int j = 0; j < p.m; ++j){
	if( D[i+bi][j+bj] != p.D[i][j] && p.D[i][j] != -1 ) return false;
      }
    }
    return true;
  }
};

bool operator<(const pair<int,int> &p1, const pair<int,int> &p2){
  if( p1.first == p2.first ){
    return p1.second < p2.second;
  }else return p1.first < p2.first;
}

int main()
{
  while(true){
    pair<int,int> ans(10000,10000);
    int n,m;
    
    cin >> n >> m;
    if( n == 0 && m == 0 ) break;
    Picture pic;
    Landscape ls;
    
    ls.n = n;
    pic.m = m;
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
	cin >> ls.D[i][j];
      }
    }
    for(int i = 0; i < m; ++i){
      for(int j = 0; j < m; ++j){
	cin >> pic.D[i][j];
      }
    }
 
    for(int bi = 0; bi <= n - m; ++bi){
      for(int bj = 0; bj <= n - m; ++bj){
	for(int rot = 0; rot < 4; ++rot){
	  if( ls.match( bi, bj, pic ) ){
	    pair<int,int> cand = make_pair(bi,bj);
	    pair<int,int> f = pic.getFirst();
	    cand.first += f.first;
	    cand.second += f.second;
	    ans = min( ans , cand );
	  }
	  //pic.print();
	  pic.rot90();
	}
      }
    }
  end:;
    
    if( ans.first > N )
      cout << "NA" << endl;
    else{
      cout << 1+ans.second << ' ' << 1+ans.first << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 936
<?php
list($l,$r)=explode(" ",trim(fgets(STDIN)));
$d=$r-$l;
if($l==0){
	echo 0;
}elseif($d>=673){
	echo 0;
}elseif($d==1){
	echo ($l%2019)*($r%2019)%2019;
}elseif($d==2){
	echo min(($l%2019)*(($l+1)%2019)%2019,(($l+1)%2019)*($r%2019)%2019,($l%2019)*($r%2019)%2019);
}else{
	$ans=2019;
	$i=$l;
	while($i<$r){
		$j=$i+1;
		while($j<=$r){
			$q=($i%2019)*($j%2019)%2019;
			if($q==0){
				echo 0;
				exit;
			}
			if($ans>$q){
				$ans=$q;
			}
			++$j;
		}
		++$i;
	}
	echo $ans;
}

Measured memory_bytes: 4604