akhauriyash/Code-Regression huggingface dataset:
class Bridge:
    def __init__(self, nodeA, nodeB):
        self.edges = [BridgeEdge(nodeA, self), BridgeEdge(nodeB, self)]
        nodeA.addEdge(self.edges[1])
        nodeB.addEdge(self.edges[0])
        self.dir = True
        self.vec = None
    def __repr__(self):
        return str((self.vec, self.dir))

class BridgeEdge:
    def __init__(self, node, edge):
        self.edge = edge
        self.node = node
    def another(self):
        return self.edge.edges[1 if self.edge.edges[0] == self else 0]

class Node:
    def __init__(self, id):
        self.id = id
        self.bridges = []
        self.parent = None
        self.judge = True
    def addEdge(self, bridgeEdge):
        self.bridges.append(bridgeEdge)
    def __repr__(self):
        return str((self.id, None if self.parent is None else self.parent.node.id, self.judge))

N, M = map(int, input().split())

nodes = [Node(i) for i in range(N+1)]
bridges = []
for A, B in (map(int, input().split()) for _ in range(M)):
    bridges.append(Bridge(nodes[A], nodes[B]))

s = [(0, nodes[1])]
idx = 0
while len(s) > 0:
    level, cur = s.pop()

    nxtlevel = level + 1
    for nxt in cur.bridges:
        if nxt.edge.vec is None:
            cur.judge ^= True
            nxt.edge.vec = (cur.id, nxt.node.id)
            if nxt.node.parent is None:
                nxt.node.parent = nxt.another()
                s.append((nxtlevel, nxt.node))

    if len(s) > 0:
        nl = s[-1][0]
    else:
        nl = 1
    while level >= nl:
        if not cur.judge:
            cur.judge ^= True
            cur.parent.node.judge ^= True
            cur.parent.edge.dir ^= True
        level -= 1
        cur = cur.parent.node
  
if all(node.judge for node in nodes[1:]):
    for br in bridges:
        if br.dir:
            print("{} {}".format(br.vec[0], br.vec[1]))
        else:
            print("{} {}".format(br.vec[1], br.vec[0]))
else:
    print("-1")


Measured memory_bytes: 115744
#include <cstdio>
#include <cmath>
#include <string>

#define rep( i, n, m ) for (int i = ( n ); i < ( m ); i++)
#define rep_d( i, n, m ) for (int i = ( n ); i < ( m ); i--)

int main( void ){
	int a, b, num = 0;
	if (!scanf( "%d %d", &a, &b ))
		return -1;
	if (b / 10 != 0){
		if (b == 100)
			num = 1000 * a + b;
		else
			num = 100 * a + b;
	}
	else {
		if (b == 0)
			num = a;
		else
			num = 10 * a + b;
	}
	if (sqrt( num ) == ( int )sqrt( num ))
		printf( "Yes\n" );
	else
		printf( "No\n" );
	return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#define INF 2000000000
using namespace std;
int dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0};
long long gcd(long long a,long long b){if(a%b==0){return b;}else return gcd(b,a%b);}
long long lcm(long long a,long long b){if(a==0){return b;} return a/gcd(a,b)*b;}
#define MOD 1000000007
#define sym cout<<"---------"<<endl;
#define ll long long
#define PL pair<long long, long long>
#define mk make_pair
#define en endl
#define WHITE 1
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll
#define P pair<int,int>

#define MAX 100005
int a[MAX];

signed main(){
  int n,x; cin>>n>>x;
  for(int i=0; i<n; i++) cin>>a[i];
  
  int cnt_sum=0;
  for(int i=1; i<n; i++){
    if(a[i]+a[i-1]>x){
      cnt_sum+=a[i]+a[i-1]-x;
      a[i]-=a[i]+a[i-1]-x;
      if(a[i]<0) a[i]=0;
    }
  }
  cout<<cnt_sum<<en;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int n,d,a[12][12],ans;
int main()
{
	cin>>n>>d;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=d;j++)
			scanf("%d",&a[i][j]);
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int s=0;
			for(int k=1;k<=d;k++)s+=(a[i][k]-a[j][k])*(a[i][k]-a[j][k]);
			int r=sqrt(s);
			if(r*r==s)ans++;
		}
	}
	cout<<ans;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include <stdio.h>
#include <math.h>
#include <cctype>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
int main(){
	long long int ans;
	int n;
	cin>>n;
	ans=1;
	for(int i=1;i<=n;i++){
		ans*=i;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 1152
def main():
    n, m, x = tuple([int(e) for e in input().split()])
    matrix = []
    book_cost = []
    for i in range(n):
        arr = [int(e) for e in input().split()]
        book_cost.append(arr[0])
        matrix.append(arr[1:])

    best = float('inf')
    for i in range(1, (1 << n)):
        sm = [0] * m
        cost = 0
        for j in range(0, n):
            if (i >> j) & 1 > 0:
                for k in range(m):
                    sm[k] += matrix[j][k]
                cost += book_cost[j]
        if min(sm) >= x and cost < best:
            best = cost
    if best == float('inf'):
        print('-1')
    else:
        print(best)

if __name__ == '__main__':
    main()

Measured memory_bytes: 9244
#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;

#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define pb push_back

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const int mod2 = 998244353;

void fact_init(int n);
ll exp(ll taban, ll us, ll md);
ll ebob(ll a, ll b);
ll ekok(ll a, ll b);
ll komb(ll a, ll b);

vector<ll> fact;
vector<ll> inv_fact;
void fact_init(int n){
    fact.resize(n+5);
    inv_fact.resize(n+5);
    fact[0] = inv_fact[0] = 1;
    for(int i = 1; i <= n; i++){
        fact[i] = (fact[i-1] * i) % mod;
        inv_fact[i] = exp(fact[i], mod-2, mod);
    }
}
ll exp(ll taban, ll us, ll md) {
    ll carpan = taban % md;
    if(carpan == 0) return 0;
    ll temp = us;
    ll res = 1;
    while(temp){
        if(temp % 2) res = (res*carpan) % md;
        temp /= 2;
        carpan = (carpan*carpan) % md;
    }
    return res;
}
 
ll ebob(ll a, ll b){
    if(!a)return b;
    return ebob(b%a, a);
}

ll ekok(ll a, ll b){
    return (a*b)/ebob(a, b);
}

ll komb(ll a, ll b){
    if(a < b) return 0;
    return fact[a] * (inv_fact[a-b] * inv_fact[b] % mod) % mod;
}

const int N = 1e3 + 5;

int main(){
    ios::sync_with_stdio(false); cin.tie(NULL);
	int h, w, m; cin>>h>>w>>m;
	int yatay[h] = {}, dikey[w] = {};
	map<int, map<int, bool>> mp;
	int mx_yatay = 0, mx_dikey = 0;
	int mx_r = 0, mx_c = 0;
	for(int i = 0; i < m; i++){
		int r, c; cin>>r>>c;
		r--, c--;
		yatay[r]++, dikey[c]++;
		if(mx_yatay < yatay[r]){
			mx_yatay = yatay[r];
			mx_r = r;
		}
		if(mx_dikey < dikey[c]){
			mx_dikey = dikey[c];
			mx_c = c;
		}
		mp[r][c] = true;
	}
	vector<int> rows, cols;
	for(int i = 0; i < h; i++) if(mx_yatay == yatay[i]) rows.pb(i);
	for(int i = 0; i < w; i++) if(mx_dikey == dikey[i]) cols.pb(i);
	int res = 0;
	for(auto r: rows) for(auto c: cols){
		int tmp = yatay[r] + dikey[c];
		if(mp[r].count(c)) tmp--;
		else{
			res = tmp;
			cout<<res;
			return 0;
		}
		res = max(res, tmp);
	}
	cout<<res;
}

Measured memory_bytes: 51920
#include<bits/stdc++.h>
using namespace std;
long long a[200000];

string multiply(string num1, string num2) 
{ 
    int len1 = num1.size(); 
    int len2 = num2.size(); 
    if (len1 == 0 || len2 == 0) 
    return "0"; 
  
    // will keep the result number in vector 
    // in reverse order 
    vector<int> result(len1 + len2, 0); 
  
    // Below two indexes are used to find positions 
    // in result.  
    int i_n1 = 0;  
    int i_n2 = 0;  
      
    // Go from right to left in num1 
    for (int i=len1-1; i>=0; i--) 
    { 
        int carry = 0; 
        int n1 = num1[i] - '0'; 
  
        // To shift position to left after every 
        // multiplication of a digit in num2 
        i_n2 = 0;  
          
        // Go from right to left in num2              
        for (int j=len2-1; j>=0; j--) 
        { 
            // Take current digit of second number 
            int n2 = num2[j] - '0'; 
  
            // Multiply with current digit of first number 
            // and add result to previously stored result 
            // at current position.  
            int sum = n1*n2 + result[i_n1 + i_n2] + carry; 
  
            // Carry for next iteration 
            carry = sum/10; 
  
            // Store result 
            result[i_n1 + i_n2] = sum % 10; 
  
            i_n2++; 
        } 
  
        // store carry in next cell 
        if (carry > 0) 
            result[i_n1 + i_n2] += carry; 
  
        // To shift position to left after every 
        // multiplication of a digit in num1. 
        i_n1++; 
    } 
  
    // ignore '0's from the right 
    int i = result.size() - 1; 
    while (i>=0 && result[i] == 0) 
    i--; 
  
    // If all were '0's - means either both or 
    // one of num1 or num2 were '0' 
    if (i == -1) 
    return "0"; 
  
    // generate the result string 
    string s = ""; 
      
    while (i >= 0) 
        s += std::to_string(result[i--]); 
  
    return s; 
} 

int main()
{
	long long n;
	cin>>n;

	for (long long i = 0; i < n; ++i)
	{
		cin>>a[i];
		if(a[i]==0)
		{
			printf("0\n");
			return 0;
		}
	}

	long long ans = 1;
	for (long long i = 0; i < n; ++i)
	{
		string x = to_string(ans);
		string y = to_string(a[i]);
		string w = multiply(x,y);
		
		ans  = ans * a[i];
		
		if(w.length()>18 && ans != (long long)1e18)
		{
			// printf("%d\n",w.length() );
			printf("-1\n");
			return 0;
		}
	}
	cout<<ans<<endl;

	
	return 0;
}

Measured memory_bytes: 4468
import sys
sys.setrecursionlimit(100000000)

import sys
input = sys.stdin.readline

N,Q = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N-1)]
px = [list(map(int,input().split())) for _ in range(Q)]

query = [0] * N
ans = [0] * N

for p,x in px:
    query[p-1] = query[p-1] + x
    
adj_list = [[] for _ in range(N)]
for a,b in ab:
    adj_list[a-1].append(b-1)
    adj_list[b-1].append(a-1)

memo = [True] * N

def dfs(v,val):
    child = adj_list[v]
    memo[v] = False
    val += query[v]
    ans[v] = val
    for s in child:
        if memo[s]:
            dfs(s,val)
    return 
        
dfs(0,0)
print(*ans)


Measured memory_bytes: 326484
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(item) for item in input().split()]
    As.sort()

    res = 0

    for i in range(1, N):
        res += As[N-1 - i//2]        

    print(res)

if __name__ == "__main__":
    resolve()


Measured memory_bytes: 31520
#include<iostream>
#include<deque>
using namespace std;
deque<int>deq;
int n,a,b,z,cnt,key,t[10000],mn,sum,u[10000];
void judge(){
  while(!deq.empty())deq.pop_back();
 
  for(int i=0;i<z;i++){
    if(t[i]/10==0)continue;
    if(deq.empty()){
      if(t[i]/10<4)deq.push_back(t[i]);
      continue;
    }
    if(deq.back()%10==t[i]%10){
      a=t[i]+(deq.back()/10)*10;
      deq.pop_back();
      if(a/10<4)deq.push_back(a);
    }else{
      if(t[i]/10<4)deq.push_back(t[i]);
    }
  }
  sum=0;
  while(!deq.empty()){
    sum+=deq.back()/10;
    deq.pop_back();
  }
  if(mn>sum){
    mn=sum;
  }
   
}
int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    cin>>key;
    cnt=1;
    z=0;
    mn=99999;
    for(int i=1;i<n;i++){
      cin>>a;
      if(key==a){
    cnt++;
      }else{
    t[z++]=cnt*10+key;
    key=a;
    cnt=1;
      }
    }
    t[z++]=cnt*10+key;
    for(int k=0;k<z;k++)u[k]=t[k];
    for(int i=0;i<z;i++){
      if(i>0){
    //t[i]=u[i]-10;
    //t[i-1]=u[i-1]+10;
    //judge();
 
    t[i]=u[i]+10;
    t[i-1]=u[i-1]-10;
    judge();
 
    t[i]=u[i];
    t[i-1]=u[i-1];
      }
      if(i<z-1){
    t[i]=u[i]+10;
    t[i+1]=u[i+1]-10;
    judge();
 
    //t[i]=u[i]-10;
    //t[i+1]=u[i+1]+10;
    //judge();
     
    t[i]=u[i];
    t[i+1]=u[i+1];
 
      }
       
    }
     
    cout<<mn<<endl;
  }
  return 0;
}

Measured memory_bytes: 1296
#include <bits/stdc++.h>
#define pii pair<int,int>
#define eb emplace_back
#define MOD 1000000007
#define int long long
using namespace std;
#define s second
#define f first

int h, w, k;

int black(vector<string>& a)
{
    int c = 0;
    for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
        c += (a[i][j] == '#');
    return c;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> h >> w >> k;
    vector<string> a(h);
    for(int i=0;i<h;i++)
        cin >> a[i];
    
    int ans = 0;
    int r=pow(2,h), c=pow(2,w);
    for(int i=0;i<r;i++)
    for(int j=0;j<c;j++)
    {
        vector<string> v = a;
        bitset<6> a(i), b(j);
        for(int p=0;p<6;p++)
            if( a[p] )
            for(int q=0;q<w;q++)
                v[p][q] = 'R';
        for(int p=0;p<6;p++)
            if( b[p] )
            for(int q=0;q<h;q++)
                v[q][p] = 'R';
        ans += (black(v)==k);
    }
    cout << ans;
}

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

int main() {
  int K;
  cin >> K;
  string S;
  cin >> S;
  int sz = S.size();
  if( sz <= K ) {
    cout << S << endl;
  }
  else{
    for( int i = 0; i < K; i++) {
      cout << S.at(i);
    }
    cout << "..." << endl;
  }
}

Measured memory_bytes: 3632
n = int(input())
s = input()

all_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

result = ''
for i in range(len(s)):
    for j in range(len(all_)):
        if s[i] == all_[j]:
            result += all_[(j+n)%len(all_)]
            break

print(result)


Measured memory_bytes: 3060
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
#include<math.h>
#include<string>
#include <cctype>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define out(x) cout << x << endl
#define INF 1ll<<60;

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

int main(){
    int n, k; cin >> n >> k;

    int count=0;
    for(int i=1; i<=n; i++){
        int a, b, x; cin >> x;

        a = x*2;
        b = 2*abs(x-k);
        count += min(a, b);
    }
    out(count);
}

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

bool check(const vector< vector<int> > &board, int c) {
    int H = board.size(), W = board[0].size();
    vector<int> rs(H), cs(W);

    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            int col = (i + j + c) % 2;
            // fprintf(stderr, "%d%c", col, " \n"[j + 1 == W]);
            rs[i] += col;
            cs[j] += col;
        }
    }

    vector<int> rt(H), ct(W);
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            rt[i] += board[i][j];
            ct[j] += board[i][j];
        }
    }

    sort(rs.begin(), rs.end());
    sort(cs.begin(), cs.end());
    sort(rt.begin(), rt.end());
    sort(ct.begin(), ct.end());
    return rs == rt and cs == ct;
}

int main() {
    int W, H; cin >> W >> H;
    vector< vector<int> > board(H, vector<int>(W));

    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            cin >> board[i][j];
        }
    }

    bool ok = false;
    ok |= check(board, 0);
    ok |= check(board, 1);

    // row
    set< vector<int> > SR;
    for(int i=0; i<H; i++) {
        vector<int> vec; vec.reserve(W);
        for(int j=0; j<W; j++) {
            vec.emplace_back(board[i][j]);
        }
        SR.emplace(vec);
    }

    // col
    set< vector<int> > SC;
    for(int j=0; j<W; j++) {
        vector<int> vec; vec.reserve(H);
        for(int i=0; i<H; i++) {
            vec.emplace_back(board[i][j]);
        }
        SC.emplace(vec);
    }

    ok &= (SR.size() == 2);
    ok &= (SC.size() == 2);
    
    cout << (ok ? "yes" : "no") << endl;
}



Measured memory_bytes: 14784
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

int main()
{
	int n,k;
	scanf("%i %i",&n,&k);
	if(k==1)
    {
        printf("0\n");
        return 0;
    }
	printf("%i\n",n-k);
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
 
#define pb push_back
#define ll long long
#define maxn 1000005
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define ms(i) memset(i,0,sizeof(i));
#define ms1(i) memset(i,-1,sizeof(i));
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pii pair<int,int>
const int mod = 1e9 + 7;
int dp[2005][2005];
int v1[2005] ,v2[2005];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    f1(n) {
        cin >> v1[i];
    }
    f1(m) {
        cin >> v2[i];
    }
    f(n + 1) {
        dp[i][0] = 1;
    }
    f(m + 1) {
        dp[0][i] = 1;
    }
    f1(n) {
        fr(j,1,m+1) {
            dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1];
            if (v1[i] == v2[j]) {
                dp[i][j] += dp[i-1][j-1];
            }
            dp[i][j] %= mod;
            dp[i][j] += mod;
            dp[i][j] %= mod;
        }
    }
    cout << dp[n][m] << endl;
}

Measured memory_bytes: 16000
n=int(input())
a=[[] for i in range(n)]
b=[[] for i in range(n)]
for i in range(n):
    ai=int(input())
    for j in range(ai):
        x,y=map(int,input().split())
        x-=1
        if y==1:
            a[i].append(x)
        else:
            b[i].append(x)
ok=0
for i in range(2**n):
    j=i
    k=0
    cnt=0
    flag=True
    while(j>0 and flag):
        if j&1:
            cnt+=1
            for x in a[k]:
                if i|(1<<x)!=i:
                    flag=False
                    break
            for x in b[k]:
                if i&(1<<x)!=0:
                    flag=False
                    break
        j//=2
        k+=1
    if flag:
        ok=max(ok,cnt)
print(ok)


Measured memory_bytes: 43504
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
//
// using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  cout << "ABC" << n << endl;
}


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

using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define debug(x) cerr << #x << " = " << (x) << endl;
template<typename T>
ostream& operator<<(ostream& o, vector<T>& v) {
    for (auto& x : v) o << x << ' ';
    return o;
}
char grid[10][10];
char ans[10][10];
int main(){
    std::ios_base::sync_with_stdio(false); cin.tie(0);

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            cin >> grid[i][j];
            ans[i][j] = '.';
        }
    }
    int j = 0;
    for (int i = 0; i < n; i++){
        while (j < m && grid[i][j] == '#') {
            ans[i][j] = '#';
            j++;
        }
        j--;
        if (i == n-1) break;
        if (grid[i+1][j] != '#') break;
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] != ans[i][j]) {
                cout << "Impossible" << endl;
                return 0;
            }
    cout << "Possible" << endl;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;
int main()
{
	int n;
	cin>>n;
	int *p=new int[n]();
	for(int i=1;i<=n;i++)
	{
		cin>>p[i];
	}
	int count=0;
	for(int i=1;i<=n;i++)
	{
		if(i%2==1&&p[i]%2==1)
		{
			count++;
		}
	}
	cout<<count<<endl;
	system("pause");
	return 0;
}

Measured memory_bytes: 3660
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
/************************************************/
#define rep(i,n) for(int i=0;i<n;i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i,a,n) for(int i=a;i<n;i++)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(),s.end()
#define zero(x) memset(x,0,sizeof(x))
#define vii vector<pair<int,int> >
#define mpis map<int,string>
#define mpii map<int,int>
#define mpsi map<string,int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get(){
	char c=getchar();
	long long x=0LL;
	while(c<'0'||c>'9')
	c=getchar();
	while(c>='0'&&c<='9'){
		x*=10LL;
		x+=(c-'0');
		c=getchar();
	}
	return x;
}
string s,t,a;
int dp[3001][3001];
pii p[3001][3001];
int main(){
	ios::sync_with_stdio(0);
	cin>>s>>t;
	rep(i,sz(s)){
		rep(j,sz(t)){
			if(s[i]==t[j]){
				int a1=dp[i][j+1],a2=dp[i+1][j],a3=dp[i][j]+1;
				dp[i+1][j+1]=max(a1,max(a2,a3));
				if(a1==dp[i+1][j+1]){
					p[i+1][j+1]=m_p(i,j+1);
				}
				else{
					if(a2==dp[i+1][j+1]){
						p[i+1][j+1]=m_p(i+1,j);
					}
					else{
						p[i+1][j+1]=m_p(i,j);
					}
				}
			}
			else{
				int a1=dp[i][j+1],a2=dp[i+1][j];
				dp[i+1][j+1]=max(a1,a2);
				if(a1==dp[i+1][j+1]){
					p[i+1][j+1]=m_p(i,j+1);
				}
				else{
					p[i+1][j+1]=m_p(i+1,j);
				}
			}
		}
	}
	pii now=m_p(sz(s),sz(t));
	while(1){
		now=p[now.fr][now.se];
		if(s[now.fr]==t[now.se]){
			a+=s[now.fr];
		}
		if(now.fr==0||now.se==0){
			break;
		}
	}
	reverse(a.begin(),a.end());
	cout<<a;
	re 0;
}

Measured memory_bytes: 105856
#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1LL << 30;

int main(){
  string S, T;
  cin >> S >> T;
  vi f(26, 30);
  set<int> s, ss;
  for(int i = 0; i < S.size(); i++){
    f.at(S.at(i) - 'a') = T.at(i) - S.at(i);
  }
  for(int i = 0; i < S.size(); i++){
    s.insert(S.at(i));
    S.at(i) += f.at(S.at(i) - 'a');
    ss.insert(S.at(i));
  }
  if(S == T &&  s.size() == ss.size()) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Measured memory_bytes: 776
#include <stdio.h>

int main(void)
{
	char str[1000];
	int goukei = 0, a = 0;
	
	while(1){
		scanf("%s", str);
		if(str[0] == '0'){
			break;
		}
		while(str[a] != '\0'){
			   goukei = goukei + (str[a] - '0');
			   a++;
		}
		printf("%d\n", goukei);
		a = 0;
		goukei = 0;
	}
	
	return (0);
}
		

Measured memory_bytes: 1828
a =  $stdin.read.to_i
#n = 10 ** 5
n = 2

def power2(a, n)
  return 1 if n == 0
  k = power2(a, n >> 1)
  k *= k
  return k if n & 1 == 0
  return k * a
end

sum = 0
(0..n).each{|i|
  #sum += power2(a, i)
  sum = power2(a, i)
}
#sum == a ** (n + 1) / (a - 1)
puts sum.to_i 


Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 1000000007
#define ALL(v) v.begin(),v.end()
#define ALLR(v) v.rbegin(),v.rend()
typedef long long ll;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//isPrime
//modpow modinv
//getDigit
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 main() {
  cout << fixed << setprecision(10);
  int k;
  string s;
  cin >> k >> s;
  if(k>=s.size()){
    cout << s << endl;
    return 0;
  }
  rep(i,k){
    cout << s.at(i);
  }
  cout << "..." << endl;
}


Measured memory_bytes: 3632
#include<stdio.h>
 
int main()
{
	int a,b,c;
	scanf("%d %d %d",&a,&b,&c);
	if(a+b+c==17 && a*b+b*c+c*a==95 && a*b*c==175)
		printf("YES");
	else printf("NO");
	return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <map>
#include <string>

#define reps(i,s,n) for(int (i) = (s); (i) < (n); (i)++)
#define rep(i,n) reps(i,0,n)
using namespace std;
using ll = long long;

int main(){
    int n;
    string s;
    string al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    cin >> n >> s;
    // cout << t - 'A' << endl;
    rep(i,s.length()){
        int num = s[i] - 'A';
        cout << al[( num + n) % 26];
    }

    cout << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long
#define db double
using namespace std;
const int maxn=3e5+5;
ll f[maxn],g[maxn],a[maxn],d[maxn],p[maxn],s[maxn],q[maxn];
ll n,m,i,t,j,k,l,x,y,z,num;
ll pan(ll j,ll k){
   return f[j]-f[k]+s[j]-s[k]+j*(j-1)/2-k*(k-1)/2;
}
void make(){
	for (i=1;i<=n;i++)s[i]=s[i-1]+a[i];
    num=0;
    for (i=1;i<=n;i++){	
    	while (num>1 && pan(i-1,d[num])*(d[num]-d[num-1])>=pan(d[num],d[num-1])*(i-1-d[num]))num--; 
    	while (num>1 && pan(d[num],d[num-1])<i*(d[num]-d[num-1]))num--;
		if (!num||pan(i-1,d[num])>i*(i-1-d[num])) d[++num]=i-1;
        j=d[num];
        f[i]=max(f[i-1],f[j]+(i-j+1)*(i-j)/2+s[j]-s[i]);
    }
}
void dg(int l,int r){
	if (l==r){
		p[l]=max(p[l],g[l+1]+f[l-1]+1-a[l]);return;
	}
	int mid=(l+r)/2;num=0;
	for (i=l;i<=mid;i++){
		while (num>1 && pan(i,d[num])*(d[num]-d[num-1])>=pan(d[num],d[num-1])*(i-d[num]))num--; 
    	while (num>1 && pan(d[num],d[num-1])<(mid+1)*(d[num]-d[num-1]))num--;
		if (!num||pan(i,d[num])>(mid+1)*(i-d[num])) d[++num]=i;
	}
	for (i=mid+1;i<=r;i++){
		while (num>1 && pan(d[num],d[num-1])<i*(d[num]-d[num-1]))num--;
		j=d[num];
		p[i]=max(p[i],g[i+1]+f[j]+(i-j+1)*(i-j)/2-s[i]+s[j]);
	}
	for (i=r-1;i>mid;i--)p[i]=max(p[i],p[i+1]);
	dg(l,mid);dg(mid+1,r);
}
int main(){
 //   freopen("data.in","r",stdin);//freopen("genocide.out","w",stdout);
    scanf("%lld",&n);
    for (i=1;i<=n;i++)scanf("%lld",&a[i]);
    scanf("%lld",&m);
    make();
    for (i=1;i*2<=n;i++)swap(a[n-i+1],a[i]);
    swap(f,g);
    make();for (i=1;i*2<=n;i++)swap(a[n-i+1],a[i]);
    swap(f,g);
    for (i=1;i*2<=n;i++) swap(g[i],g[n-i+1]);
    for (i=1;i<=n;i++)s[i]=s[i-1]+a[i];
    memset(p,128,sizeof(p));memset(q,128,sizeof(q));
    dg(0,n);
    swap(p,q);swap(f,g);
	for (i=1;i*2<=n;i++)swap(f[i],f[n-i+1]),swap(g[i],g[n-i+1]),swap(a[i],a[n-i+1]);
	for (i=1;i<=n;i++)s[i]=s[i-1]+a[i];
	dg(0,n);
	for (i=1;i<=n;i++)q[i]=max(q[i],p[n-i+1]);
	swap(f,g);
	for (i=1;i*2<=n;i++)swap(f[i],f[n-i+1]),swap(g[i],g[n-i+1]),swap(a[i],a[n-i+1]);
    for (i=1;i<=m;i++){
    	scanf("%lld%lld",&x,&y);
    	t=max(f[x-1]+g[x+1],q[x]+a[x]-y);
    	printf("%lld\n",t);
	}
}

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

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
int R,G,B,N,r,g,b,i,ans;
int main(){
    scanf("%d%d%d%d", &R,&G,&B, &N);
    fornum(r,0,N+1){
        fornum(g,0,N+1){
            if(R*r+G*g>N||(N-R*r-G*g)%B!=0){
                continue;
            }
            //printf("%d,%d,%d\n", r, g, (N - R * r + G * g) / B);
            ans++;
        }
    }
    printf("%d", ans);
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)




int N;
string A[500];
//-----------------------------------------------------------------
char B[500][500];
int sol(int x) {
	rep(i, 0, N) rep(j, 0, N) B[i][j] = A[i][j];

	bool ok = false;
	rep(i, 0, N) if (A[i][x] == '#') ok = true;

	int cnt = 0;
	if (!ok) cnt++;

	rep(i, 0, N) if (A[x][i] == '.') cnt++;

	int non = 0;
	rep(i, 0, N) {
		bool ok = true;
		rep(j, 0, N) if (A[j][i] == '.') ok = false;
		if (!ok) non++;
	}

	return cnt + non;
}
//-----------------------------------------------------------------
int main() {
	cin >> N;
	rep(i, 0, N) cin >> A[i];

	bool ok = true;
	rep(i, 0, N) rep(j, 0, N) if (A[i][j] == '#') ok = false;
	if (ok) {
		printf("-1\n");
		return 0;
	}

	int ans = INT_MAX;
	rep(i, 0, N) ans = min(ans, sol(i));
	if (ans == INT_MAX) ans = -1;
	cout << ans << endl;
}

Measured memory_bytes: 768
N, M, K = list(map(int, input().split()))

A = list(map(int, input().split()))
B = list(map(int, input().split()))

a, b = [0], [0]

for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])

ans,j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Measured memory_bytes: 47480
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;


int main(){
  ll A, B;
  cin >> A >> B;

  if((A+B)%2 == 1) cout << "IMPOSSIBLE" << endl;
  else cout << (A+B)/2 << endl;
  
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
    ll N;
    cin>>N;
    ll nj=N*(N-1);
    nj/=2;
    ll k=1;
    for(ll i=1;i<=N;i++)
    {
        k*=i;
    }
    ll jp=k*(N-1);
    ll ar[N];
    ll br[N];
    for(ll i=0;i<N;i++)
    {
        cin>>ar[i]>>br[i];
    }
    double ans=0.0;
    nj=jp/nj;
    //cout<<nj<<endl;
    for(ll i=0;i<N-1;i++)
    {
        for(ll j=i+1;j<N;j++)
        {
            ans+=nj*sqrt((ar[j]-ar[i])*(ar[j]-ar[i])+(br[j]-br[i])*(br[j]-br[i]));
        }
        //cout<<ans<<endl;
    }
    ans/=k;
    cout<<fixed<<setprecision(10)<<ans<<endl;
	return 0;
}

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

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    ll a[3];
    int k;
    cin >> a[0] >> a[1] >> a[2] >> k;

    sort(a, a+3);
    a[2] *= (1LL << k);
    cout << a[0] + a[1] + a[2];
}


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

int main(){
    int n, val, link, linknum;
    cin >> n;
    int Graphlinked[n][n];
    for(int i=0; i<n; i++){
        for(int k=0; k<n; k++){
            Graphlinked[i][k] = 0;
        }
        cin >> val >> linknum;
        for(int j=0; j<linknum; j++){
            cin >> link;
            Graphlinked[i][link - 1] = 1;
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(j) cout << " ";
            cout << Graphlinked[i][j];
        }
        cout << endl;
    }
}


Measured memory_bytes: 3152
import Control.Monad
import Data.Array

main = do
    n <- read <$> getLine
    as <- replicateM n (read <$> getLine)
    print $ f (listArray (0, n) as) n 1 0

f :: Array Int Int -> Int -> Int -> Int -> Int
f list n i count = if i == 2 then count else if n < count then -1 else f list n next (count + 1)
    where next = list ! (i - 1)

Measured memory_bytes: 45564
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define reps(i,n) for(int i = 1; i <= n; i++)
#define rrep(i,n) for(int i = n-1; i >= 0; i--)
#define rreps(i,n) for(int i = n; i >= 1; i--)
#define mrep(i,from,n) for(int i = from; i < n; i++)
#define mreps(i,from,n) for(int i = from; i <= n; i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define ERASE(x,val) x.erase(remove(ALL(x), val), x.end())
typedef long long ll;
typedef pair<ll, ll> P;
template<class T>inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; }
ll MOD = 1000000007;
int INF = 1001001001;

void solve()
{
	ll n, a, b; cin >> n >> a >> b;
	ll q = n / (a+b);
	ll r = n % (a+b);
	ll ans = q * a;
	if(r < a)ans += r;
	else ans += a;
	cout << ans << endl;
	return;
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}


Measured memory_bytes: 256
n,s,*a=map(int,open(0).read().split());d=[0]*-~s;m=998244353;v=pow(2,m-2,m);d[0]=pow(2,n,m)
for i in a:
  for j in range(s,i-1,-1):d[j]+=(d[j-i]*v)%m
print(d[s]%m)

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

int main(){
    int n,k; cin>>n>>k;
    vector<int> x(n);
    for(int i=0; i<n; i++)cin>>x[i];
    int ans=1e9;
    for(int i=0; i<n-k+1; i++){
        int left = x[i];
        int right = x[i+k-1];

        ans = min(ans,min(abs(left),abs(right))+right-left);
    }
    cout<<ans<<endl;
    return 0;
}

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

int main() {
  string S;
  cin >> S;
  string ans = "";
  for (int b = 0; b < (1 << 3); b++) {
    int res = S.at(0) - '0';
    string formula = to_string(res);
    for (int i = 0; i < S.size() - 1; i++) {
      if (b & (1 << i)) {
        formula += '+';
        res += S.at(i + 1) - '0';
      } else {
        formula += '-';
        res -= S.at(i + 1) - '0';
      }
      formula += S.at(i + 1);
    }

    if (res == 7) {
      cout << formula + "=7" << endl;
      break;
    }
  }
}

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

int main(){
    string S;
    cin>>S;
    char a=S.at(0);
    char b=S.at(1);
    char c=S.at(2);
    char d=S.at(3);
    int x=0;
    if(a=='+'){x+=1;}
    else if(a=='-'){x-=1;}
    if(b=='+'){x+=1;}
    else if(b=='-'){x-=1;}
    if(c=='+'){x+=1;}
    else if(c=='-'){x-=1;}
    if(d=='+'){x+=1;}
    else if(d=='-'){x-=1;}
    cout<<x<<endl;
    return 0;
}

Measured memory_bytes: 256
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [[input().split()] for _ in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
a=n1()
b=n1()
c=n1()

a.sort()
b.sort()
c.sort()

from bisect import bisect_left,bisect_right
b2=[]
for i in b:
    b2.append(bisect_left(a,i))

import itertools
b3=list(itertools.accumulate(b2))
    
ans=0
for i in c:
    t=bisect_left(b,i)
    if t>0:
        ans+=b3[t-1]
    
print(ans)

Measured memory_bytes: 24564
#include<bits/stdc++.h>
#define MAX_H 2000
#define MAX_W 2000
using namespace std;
int main(void){

  ios::sync_with_stdio(false);
  int H, W;   cin >> H >> W;
  char field[MAX_H + 1][MAX_W + 1];
  for(int i = 1 ; i <= H ; i++)
    for(int j = 1 ; j <= W ; j++)   cin >> field[i][j];

  int R[MAX_H + 1][MAX_W + 1];
  for(int i = 1 ; i <= H ; i++){
    for(int j = W ; 1 <= j ; j--){

      if(field[i][j] == '#')
	R[i][j] = 0;
      else{
	if(j == W)  R[i][j] = 1;
	else	R[i][j] = R[i][j + 1] + 1;
      }
    }
  }
  
  int L[MAX_H + 1][MAX_W + 1];
  for(int i = 1 ; i <= H ; i++){
    for(int j = 1 ; j <= W ; j++){

      if(field[i][j] == '#')
	L[i][j] = 0;
      else{
	if(j == 1)  L[i][j] = 1;
	else	L[i][j] = L[i][j - 1] + 1;
      }

    }
  }

  int U[MAX_H + 1][MAX_W + 1];
  for(int i = 1 ; i <= H ; i++){
    for(int j = 1 ; j <= W ; j++){

      if(field[i][j] == '#')
	U[i][j] = 0;
      else{
	if(i == 1)  U[i][j] = 1;
	else	U[i][j] = U[i - 1][j] + 1;
      }
      
    }
  }

  int D[MAX_H + 1][MAX_W + 1];
  for(int i = H ; 1 <= i ; i--){
    for(int j = 1 ; j <= W ; j++){

      if(field[i][j] == '#')
	D[i][j] = 0;
      else{
	if(i == H)  D[i][j] = 1;
	else D[i][j] = D[i + 1][j] + 1;
      }

    }
  }

  int ans = 0;
  for(int i = 1 ; i <= H ; i++)
    for(int j = 1 ; j <= W ; j++)
      ans = max(ans , R[i][j] + L[i][j] + U[i][j] + D[i][j] - 3);

  
  cout << ans << endl;

  return 0;
}


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

int main(){
  int K, A, B;
  cin >> K;
  cin >> A >> B;
  bool res = false;
  for(int i=A; i<=B; i++){
    if(i%K==0){
      res = true;
      break;
    }
  }
  if(res){
    cout << "OK" << endl;
  }else{
    cout << "NG" << endl;
  }
}


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

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()  
#define sz(x) int(x.size()) 
#define MOD 1000000007

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF=63;
const int mxn=1e3+5;
bool DEBUG=0;

const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
string grid[mxn];
int dist[mxn][mxn];
int sx,sy,ex,ey,h,w;

bool check(int x, int y){
	return x>=0&&x<h&&y>=0&&y<w;
}

void bfs(){
	memset(dist,0x3f,sizeof(dist));
	priority_queue<pair<int,ii>,vector<pair<int,ii>>,greater<pair<int,ii>>>q;
	dist[sx][sy]=0;
	if(grid[sx][sy]=='#'||grid[ex][ey]=='#'){
		cout<<-1<<"\n";
		return;
	}
	q.emplace(dist[sx][sy],ii(sx,sy));
	while(!q.empty()){
		int d = q.top().fi;
		int x = q.top().se.fi;
		int y = q.top().se.se;
		q.pop();
		if(d>dist[x][y])continue;
		for(int k=0; k<4; k++){
			int X = x+dx[k];
			int Y = y+dy[k];
			if(check(X,Y)&&grid[X][Y]=='.'){
				if(dist[X][Y]>d){
					dist[X][Y]=d;
					q.emplace(dist[X][Y],ii(X,Y));
				}
			}
		}
		for(int i=-2; i<=2; i++){
			for(int j=-2; j<=2; j++){
				int X=x+i;
				int Y=y+j;
				if(check(X,Y)&&grid[X][Y]=='.'){
					if(dist[X][Y]>d+1){
						dist[X][Y]=d+1;
						q.emplace(dist[X][Y],ii(X,Y));
					}
				}
			}
		}
	}
	int ans=dist[ex][ey];
	if(ans>1e9)ans=-1;
	cout<<ans<<"\n";
	return;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    cin>>h>>w;
    cin>>sx>>sy>>ex>>ey;
    sx--,sy--,ex--,ey--;
    for(int i=0; i<h; i++){
		cin>>grid[i];
	}
	bfs();
	
}
	
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN


Measured memory_bytes: 8644
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Point {
  int x;
  int y;
  int type;
  Point() {;}
  Point(double x, double y, int type) : x(x), y(y), type(type) {;}
  bool operator<(const Point &rhs) const {
    if (fabs(y - rhs.y) > EPS) { return y < rhs.y; }
    if (fabs(x - rhs.x) > EPS) { return x < rhs.x; }
    return false;
  }
};

struct Event {
  int x;
  Point p;
  int in;
  Event() {;}
  Event(int x, Point p, int in) : x(x), p(p), in(in) {;}
  bool operator<(const Event &rhs) const {
    if (x != rhs.x) { return x < rhs.x; }
    return in > rhs.in;
  }
};

inline int square(int x) { return x * x; }

inline int dist2(const Point &a, const Point &b) {
  return square(a.x - b.x) + square(a.y - b.y);
}

int an, bn, n;
int r;
Event event[500000];

int PlaneSweep() {
  int ans = 0;
  set<Point> open;
  REP(iter, n) {
    Event &e = event[iter];
    Point &p = e.p;
    if (e.in) {
      set<Point>::iterator lower = open.lower_bound(Point(0, p.y - 4.0 * r - 1, 0));
      set<Point>::iterator upper = open.upper_bound(Point(0, p.y + 4.0 * r + 1, 0));
      for (set<Point>::iterator it = lower; it != upper; it++) {
        if (dist2(*it, p) <= 16.0 * r * r) {
          ans += p.type ^ it->type;
        }
      }
      open.insert(p);
    } else {
      open.erase(open.find(p));
    }
  }
  return ans;
}

int main() {
  while (scanf("%d %d %d", &an, &bn, &r), an|bn) {
    n = (an + bn) * 2;
    REP(i, an) {
      int x, y;
      scanf("%d %d", &x, &y);
      event[2 * i + 0] = Event(x - 2 * r, Point(x, y, 0), 1);
      event[2 * i + 1] = Event(x + 2 * r, Point(x, y, 0), 0);
    }
    REP(i, bn) {
      int x, y;
      scanf("%d %d", &x, &y);
      event[2 * an + 2 * i + 0] = Event(x - 2 * r, Point(x, y, 1), 1);
      event[2 * an + 2 * i + 1] = Event(x + 2 * r, Point(x, y, 1), 0);
    }
    sort(event, event + n);
    int ans = PlaneSweep();
    printf("%d\n", ans);
  }
}

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

int main (void){
	string s;
	int w;
	cin >> s;
	cin >> w;
	for(int i = 0; i < s.length(); i++){
		if(i % w == 0){
			cout << s[i];
		}
	}

	cout << endl;
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>

int main() {
	int x,i,j,h,n,k=0;
	scanf("%d", &x);
	for (j = 2; j < 10; j++) {
		for (i = 1; i < 32; i++) {
			n = 1;
			for (h = 0; h < j; h++) {
				n *= i;
			}
			if (n <= x) {
				if (k < n) {
					k = n;
				}
			}
		}
	}
	printf("%d", k);
	return 0;
}

Measured memory_bytes: 128
x = int(input())


def dvd(a, b):
    cnt = 0
    while a >= b and a is not 0:
        a = a - b
        cnt += 1
    if b == 500:
        return b * 2 * cnt, a
    else:
        return b * cnt, a


print(dvd(x, 500)[0] + dvd(dvd(x, 500)[1], 5)[0])


Measured memory_bytes: 2940
from fractions import gcd


N, M = map(int, input().split())
S = input()
T = input()
g = gcd(N, M)
N //= g
M //= g
for i in range(g):
    if S[i * N] != T[i * M]:
        print(-1)
        break
else:
    print(N * M * g)


Measured memory_bytes: 5432
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
ll MOD=1000000007;
ll mod=998244353;
int inf=1000000000;
ll INF=10000000000000000;
int main(){
    string s;
    cin >> s;
    int l=s.size();
    int d;
    cin >> d;
    ll dp[2][l+1][d];
    rep(i,2){
        rep(j,l+1){
            rep(k,d) dp[i][j][k]=0;
        }
    }
    int y=s.at(0)-'0';
    dp[1][0][y%d]=1;
    rep(i,y) dp[0][0][(i)%d]++;
    rep(i,l-1){
        int u=s.at(i+1)-'0';
        rep(j,10){
            rep(k,d) (dp[0][i+1][(k+j)%d]+=dp[0][i][k])%=MOD;
        }
        rep(k,d) (dp[1][i+1][(k+u)%d]+=dp[1][i][k])%=MOD;
        rep(j,u){
            rep(k,d) (dp[0][i+1][(k+j)%d]+=dp[1][i][k])%=MOD;
        }
    }
    // rep(j,l+1){
    //         rep(k,d) cout << dp[1][j][k];
    //         cout << endl;
    //     }
    cout << (dp[0][l-1][0]+dp[1][l-1][0]-1+MOD)%MOD << endl;
}

Measured memory_bytes: 15872
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n,black=0,lblack=0,rwhite,ans;
    string s;
    cin>>n>>s;
    ans=n;
    for(int i=0;i<n;i++)if(s[i]=='#')black++;
    rwhite=n-black;
    for(int i=0;i<=n;i++){
        ans=min({ans,lblack+rwhite});
        //cout<<lblack<<rwhite<<endl;
        if(s[i]=='#')lblack++;
        else if(s[i]=='.') rwhite--;
    }
    //cout<<lblack<<rwhite<<endl;
    ans=min({ans,lblack+rwhite});
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 640
n=input()
cc=0
ok=1
for i in range(n):
    a,b=map(int,raw_input().split()) 
    if a==b: 
        cc+=1 
    else: 
        cc=0
    if cc==3: 
        print "Yes";ok=0;break
if ok: 
    print "No"

Measured memory_bytes: 64616
n, k = map(int, input().split())
cnt = [0] * (n)
for _ in range(k):
    d = int(input())
    A = list(map(int, input().split()))
    for a in A:
        a -= 1
        cnt[a] += 1
num = 0
for i in range(n):
    if cnt[i] == 0:
        num += 1

print(num)


Measured memory_bytes: 9184
s = input()

s_x = s.replace('x', '')
s = list(s)

ans = 0
if s_x != s_x[::-1]:
    print(-1)
else:
    while(len(s) > 1):
        if s[0] == s[-1]:
            s.pop()
            s.pop(0)
        elif s[0] == 'x':
            s.pop(0)
            ans += 1
        elif s[-1] == 'x':
            s.pop()
            ans += 1
    print(ans)

Measured memory_bytes: 4084
from collections import Counter
from sys import exit
class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

S = input()
N = len(S)

cnt = 0
SC = Counter(S)
for v in SC.values():
    if v % 2:
        cnt += 1
if cnt > 1:
    print(-1)
    exit()

geta = 10**9+7
T = [None]*N
table = Counter()
idx = dict()
ctr = 0
for i, s in enumerate(S):
    table[s] += 1
    if table[s] <= SC[s]//2: 
        T[i] = ctr
        idx[ord(s)*geta + table[s]] = ctr
        ctr += 1
        continue
    if table[s] > (1+SC[s])//2:
        T[i] = N - 1 - idx[ord(s)*geta+(SC[s] + 1 - table[s])]
        continue
    if SC[s] % 2 and table[s] == (SC[s]+1)//2:
        T[i] = (N-1)//2
        continue
Tr = [None]*N
for i, v in enumerate(T):
    Tr[v] = i
B = BIT(N)
ans = 0
for tr in Tr[::-1]:
    ans += B.sum(tr + 1)
    B.add(tr + 1, 1)
print(ans)

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

using namespace std;

#define allof(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;



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

    int A, B, C;
    cin >> A >> B >> C;

    int AB = abs(A - B);
    int AC = abs(A - C);
    int BC = abs(B - C);

    cout << AB + AC + BC - max(max(AB, AC), BC);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
#define ll long long 
#define ull unsigned ll 
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP1(i,n) for (int i=1;i<=(n);i++)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define pii pair<int,int>
#define SZ(x) x.size()
#define ALL(x) x.begin(), x.end()
#define ull unsigned ll 
#define pb push_back
#define SQ(x) (x)*(x)
#define endl '\n'
using namespace std;

int main (){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	string s; cin>>s;
	int siz=s.size();
	int ans=1;
	string prev=s.substr(0,1);
	FOR(i,1,siz){
		if (s.substr(i,1)!=prev) {
			ans++;
			prev=s.substr(i,1);
			continue;
		}
		if (i==siz-1&&s.substr(i,1)==prev) continue;
		prev=s.substr(i,2);
		i++;
		ans++;
	}
	cout<<ans<<endl;
}


Measured memory_bytes: 720