akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++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 irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
 
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vs = vector<string>;
using vd = vector<double>;
using ul = unsigned long;
 
template<class T, class C> void chmax(T& a, C b){ a>b?:a=b; }
template<class T, class C> void chmin(T& a, C b){ a<b?:a=b; }
 
const int mod=1e9+7;
 
struct mint {
  ll x;
  mint(ll x=0):x(x%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
};
 
mint c[4005][4005];
void init() {
  c[0][0] = 1;
  for (int i = 0; i <= 4000; i++) {
    for (int j = 0; j <= i; j++) {
      c[i+1][j] += c[i][j];
      c[i+1][j+1] += c[i][j];
    }
  }
}
mint comb(int n, int k) {
  return c[n][k];
}
 
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;
 
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            return false;
        }
    }
 
    return true;
}
 
map<ll, ll> primeFact(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1) res[n]++;
  return res;
}
 
void printv(vi& v){
  rep(i,v.size()) cout << v[i] << " ";
  cout << endl;
}
void printv(vll& v){
  rep(i,v.size()) cout << v[i] << " ";
  cout << endl;
}
int vtotal(vi& v){
  int total=0;
  rep(i,v.size()) total+=v[i];
  return total;
}
 
int main()
{
  //init(); // mint初期化
  cin.tie( 0 );
  ios::sync_with_stdio( false );
  
  int x;cin>>x;
  string ans="ABC";
  if(x>=1200)ans="ARC";
  cout(ans);
  
  return 0;
}

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

int main() {
	string s; cin >> s;
	for (char c : s) {
		if (c >= 'D')cout << char(c - 3);
		else cout << (char)(c + 23);
	}
	cout << endl;
	return 0;
}

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

int main(){
    ll N;
    cin >> N;
    ll big =0;
    ll small=0;
    vector<ll> a(N);
    for(int i=0;i<N;i++){
        cin >> a[i];
    }
    vector<ll> b(N);
    for(int i=0;i<N;i++){
        cin >> b[i];
    }
    for(int i=0;i<N;i++){
        ll temp = a[i] -b[i];
        if(temp<0){
            small -= temp/2;
        }else{
            big += (temp);
        }
    }
    if(big-small<=0){
        cout <<"Yes" << endl;
    }else{
        cout <<"No" << endl;
    }

    return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

const int MAX_N = 3 * 1e5;

struct UnionFind{
    int N;
    int par[MAX_N];

    UnionFind(int N): N(N){
        for (int i=0;i<N;i++){
            par[i] = i;
        }
    }

    int root(int x){
        if(par[x] == x) return x;
        int rx = root(par[x]);
        par[x] = rx;
        return rx;
    }

    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if(rx==ry) return;
        par[rx] = ry;
    }

    bool find(int x, int y){
        return root(x) == root(y);
    }
};

struct diff{
    ll s, t, w;
    bool operator<( const diff& right ) const {
        return w < right.w;
    }
};

int main(){
    int N;
    cin >> N;
    vector<ll> x(N), y(N);
    REP(i, N) cin >> x[i] >> y[i];
    vector<pii> xsort(N), ysort(N);
    REP(i, N){
        xsort[i].first = x[i];
        xsort[i].second = i;
        ysort[i].first = y[i];
        ysort[i].second = i;
    }
    sort(xsort.begin(), xsort.end());
    sort(ysort.begin(), ysort.end());
    vector<diff> diffs;
    REP(i, N-1){
        diff d;
        d.s = xsort[i].second;
        d.t = xsort[i+1].second;
        d.w = xsort[i+1].first - xsort[i].first;
        diffs.push_back(d);
    }
    REP(i, N-1){
        diff d;
        d.s = ysort[i].second;
        d.t = ysort[i+1].second;
        d.w = ysort[i+1].first - ysort[i].first;
        diffs.push_back(d);
    }
    sort(diffs.begin(), diffs.end());
    UnionFind uf(N);
    ll res = 0;
    REP(i, diffs.size()){
        if(!uf.find(diffs[i].s, diffs[i].t)){
            res += diffs[i].w;
            uf.unite(diffs[i].s, diffs[i].t);
        }
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 11504
//B
#include <bits/stdc++.h>
#define write(x) printf("%d\n", x)
using namespace std;
typedef long long ll;
template <typename T>
void read(T &x)
{
	x = 0;
	char c;
	while (!isdigit(c = getchar()));
	do
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
	}while (isdigit(c = getchar()));
}
int a[100005];
int sum;
int main()
{
	int h, n;
	read(h);read(n);
	for (int i = 0;i < n;++i)
		read(a[i]), sum += a[i];
	if (sum >= h) puts("Yes");
	else puts("No");
	return 0;
}


Measured memory_bytes: 640
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

int get_int2(int *a1, int64_t *a2) {
  scanf("%d %lld", a1, a2);
  return 0;
}

// <arr[0]> <arr[1]> .. <arr[size-1]>
int fget_array(int *arr, int size) {
    int i;
    for(i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }
    return 0;
}

#define NUM_MAX 200000

int main(void) {
    int num;
    int arr[NUM_MAX+1];
    int64_t changes;
    get_int2(&num, &changes);

    fget_array(&arr[1], num);

    int pos[NUM_MAX*2+1];
    static int idx_map[NUM_MAX+1];
    int i;
    for(i = 0; i <= NUM_MAX; i++) idx_map[i] = -1;
    int pidx = 0;
    pos[pidx++] = 1;
    idx_map[1] = 0;
    int cycle_start = 0;
    int cycle_end = 0;
    for(i = 1; i <= NUM_MAX*2; i++) {
        int cur = pos[i-1];
        int next = arr[cur];
        if(idx_map[next] != -1) {
            cycle_start = idx_map[next];
            cycle_end = i;
            break;
        }
        pos[i] = next;
        idx_map[next] = i;
    }

    int ans = 0;

#ifdef DEBUG
    printf("%d %d\n", cycle_start, cycle_end);
#endif
    if(changes < cycle_end) {
        ans = pos[changes];
    } else {
        changes -= cycle_start;
        ans = pos[cycle_start+changes%(cycle_end-cycle_start)];
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 3964
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n;
string s;
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n;
    cin >> s;
    int cntw = 0, cntb = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '.') {
            cntw++;
        } else {
            cntb++;
        }
    }
    int c0 = 0, c1 = 0;
    int ans = min(cntb, cntw);
    for (int i = 0; i < n; i++) {
        if (s[i] == '.') {
            c0++;
        } else {
            c1++;
        }
        ans = min(ans, c1 + (cntw - c0));
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 720
import sys
from collections import Counter
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
s=[input() for i in range(n)]
ans=0
dic={}
for i in range(n):
    tmp=list(s[i])
    tmp.sort()
    tmp="".join(tmp)
    if tmp in dic:
        dic[tmp]+=1
    else:
        dic[tmp]=1
for i in dic.values():
    ans+=i*(i-1)//2
# print(dic)
print(ans)

Measured memory_bytes: 66012
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	int num[n],sum=0;
	for (int i=0; i<n; i++)
		cin>>num[i];
	sort(num, num+n);
	for (int i=n-1; i>=n-k; i--)
		sum+=num[i];
	cout<<sum;
	return 0;
}

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

int main(void){
    int n,m;
    cin>>n;
    map<string,int> mp;
    rep(i,n){
        string s;
        cin>>s;
        mp[s]++;
    }
    cin>>m;
    rep(i,m){
        string t;
        cin>>t;
        mp[t]--;
    }
    int res = 0;
    for(auto p : mp){
        res = max(res, p.second);
    }
    cout<<res<<endl;
    return 0;
}


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

int D;
int C[26];
int S[365][26];

int64_t evaluate(const vector<int> &T) {
  int64_t res = 0;
  vector<int> last(26);
  for (int i = 0; i < T.size(); ++i) {
    res += S[i][T[i] - 1];
    last[T[i] - 1] = i + 1;
    for (int j = 0; j < 26; ++j) {
      res -= C[j] * (i + 1 - last[j]);
    }
  }
  return res;
}

int64_t evaluate_avg(const vector<int> &T) {
  int64_t res = 0;
  vector<int> last(26);
  for (int i = 0; i < T.size(); ++i) {
    res += S[i][T[i] - 1];
    last[T[i] - 1] = i + 1;
    for (int j = 0; j < 26; ++j) {
      res -= C[j] * (i + 1 - last[j]);
    }
  }
  for (int i = T.size(); i < min((int) T.size() + 8, D); ++i) {
    for (int j = 0; j < 26; ++j) {
      res -= C[j] * (i + 1 - last[j]);
    }
  }
  return res;
}

int main() {
  cin >> D;
  for (int i = 0; i < 26; ++i) cin >> C[i];
  for (int i = 0; i < D; ++i) for (int j = 0; j < 26; ++j) cin >> S[i][j];

  vector<vector<int>> cands;
  cands.push_back(vector<int>());
  for (int _ = 0; _ < D; ++_) {
    vector<int> nscores;
    vector<vector<int>> ncands;
    for (auto cand : cands) {
      vector<pair<int64_t, int>> inc;
      for (int n = 1; n <= 26; ++n) {
        cand.push_back(n);
        inc.emplace_back(evaluate_avg(cand), n);
        cand.pop_back();
      }
      sort(inc.begin(), inc.end(), greater<pair<int64_t, int>>());
      for (int i = 0; i < 4; ++i) {
        cand.push_back(inc[i].second);
        nscores.push_back(inc[i].first);
        ncands.push_back(cand);
        cand.pop_back();
      }
    }
    vector<int> ord(nscores.size());
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int i, int j) { return nscores[i] > nscores[j]; });
    cands.clear();
    for (int i = 0; i < min((int) nscores.size(), 5); ++i) {
      cands.push_back(ncands[ord[i]]);
    }
  }

  for (int i = 0; i < D; ++i) cout << cands[0][i] << endl;

  return 0;
}



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

const int N = 305;
typedef long long ll;

string s;
int n, m, f[N][N][N];

int Dfs(int l, int r, int p) {
	if(f[l][r][p]) return f[l][r][p];
	if(l == r) return f[l][r][p] = 1;
	if(l + 1 == r) {
		if(p || s[l] == s[r]) return f[l][r][p] = 2;
		else return f[l][r][p] = 1;
	}
	if(s[l] == s[r]) f[l][r][p] = max(f[l][r][p], Dfs(l + 1, r - 1, p) + 2);
	else if(p) f[l][r][p] = max(f[l][r][p], Dfs(l + 1, r - 1, p - 1) + 2);
	return f[l][r][p] = max(f[l][r][p], max(Dfs(l, r - 1, p), Dfs(l + 1, r, p)));
}

int main() {
	cin>>s>>m;
	n = s.size();
	cout<<Dfs(0, n - 1, m)<<endl;
	return 0;
}

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

const int d[] = {-1, -4, 1, 4};

typedef pair<string, int> P;

int main()
{
	queue<P> que;
	que.push({"01234567", 0});
	map<string, int> done;
	while (!que.empty()){
		P st = que.front(); que.pop();
		//if (st.first == g){
		//	res = st.second;
		//	break;
		//}
		if (done.count(st.first)) continue;
		done.insert(st);
		
		int p = 0;
		while (st.first[p] != '0') p++;
		
		for (int dir = 0; dir < 4; dir++){
			P tmp = st;
			tmp.second++;
			int to = p + d[dir];
			if (to < 0 || to >= 8) continue;
			if (p + to == 7 && dir % 2 == 0) continue;
			swap(tmp.first[p], tmp.first[to]);
			que.push(tmp);
		}
	}
	
	while (true){
		string g;
		for (int i = 0; i < 8; i++){
			string a;
			if (!(cin >> a)) return 0;
			g += a;
		}
		
		printf("%d\n", done[g]);
	}
	
	return 0;
}

Measured memory_bytes: 3532
#include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 60;
static const int MAX_M = 60;
char a[MAX_N][MAX_N];
char b[MAX_M][MAX_M];
int n,m;
int a_tate,a_yoko,b_tate,b_yoko;

bool isMatch(int y,int x){
    for(int i=y;i<y+b_tate;i++){
        for(int j=x;j<x+b_yoko;j++){
            if(a[i][j]!=b[i-y][j-x])
                return false;
        }
    }
    return true;
}


int main()
{
    cin>>n>>m;
    a_tate = n;
    b_tate = m;
    //input a
    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        for(int j=0;j<s.size();j++){
            a[i][j] = s[j];
        }
        a_yoko = s.size();
    }
    //input b
    for(int i=0;i<m;i++){
        string s;
        cin>>s;
        for(int j=0;j<s.size();j++){
            b[i][j] = s[j];
        }
        b_yoko = s.size();
    }
    //judge
   // cout<<"a_tate : "<<a_tate<<endl;
   // cout<<"a_yoko : "<<a_yoko<<endl;
   // cout<<"b_tate : "<<b_tate<<endl;
   // cout<<"b_yoko : "<<b_yoko<<endl;

    for(int i=0;i<a_tate-b_tate+1;i++){
        for(int j=0;j<a_yoko-b_yoko+1;j++){
            //match?
            bool flag = isMatch(i,j);
            if(flag){
                cout<<"Yes"<<endl;
                return 0;
            }
        }
    }
    cout<<"No"<<endl;
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
const int nax=20;
const ll INF=1e18L+5;
ll values[nax][nax];
vector<ll> pre;
vector<ll> dp;

void rec(int i,vector<int>& ava,ll score_so_far,int mask,int group){
    if(i==ava.size()){
        dp[mask]=max(dp[mask],score_so_far+pre[group]);
        return;
    }
    rec(i+1,ava,score_so_far,mask,group);
    rec(i+1,ava,score_so_far,mask^(1<<ava[i]),group^(1<<ava[i]));
}

int main()
{
  int n;cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        cin>>values[i][j];
    }
  }
  pre.resize((1<<n));

  for(int mask=0;mask<(1<<n);mask++){
        for(int i=0;i<n;i++){
            if(mask&(1<<i)){
                for(int j=i+1;j<n;j++){
                    if(mask&(1<<j)){
                        pre[mask]+=values[i][j];
                    }
                }
            }
        }

  }

  dp=vector<ll> (1<<n,-INF);
  dp[0]=0;
   for(int i=0;i<(1<<n);i++){
        vector<int>ava;
     for(int j=0;j<n;j++){
        if(!(i&(1<<j))){
            ava.push_back(j);
        }
     }
     rec(0,ava,dp[i],i,0);
   }
  cout<<dp[(1<<n)-1];
}


Measured memory_bytes: 4320
#include<iostream>
using namespace std;
int n,m,dir[8][2]={{1,0},{0,1},{-1,0},{0,-1},{-1,-1},{1,-1},{1,1},{-1,1}};
char mp[101][101];
void dfs()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int num=0;
            if(mp[i][j]=='#')cout<<'#';
            if(mp[i][j]=='.')
            {
                for(int k=0;k<8;k++)
                {
                    int x0=i+dir[k][0];
                    int y0=j+dir[k][1];
                    if(mp[x0][y0]=='#')num++;
                }
            cout<<num;
            }

        }
        cout<<endl;
    }

}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>mp[i][j];
    dfs();
    return 0;
}

Measured memory_bytes: 256
"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(a){return a?cin[cid++]:+cin[cid++];}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}

var myOut = main();
if(myOut !== undefined)console.log(myOut);

function main(){
  var n = next();
  var a = nexts(n);
  var b = xArray(0,n);
  var ans = 0;
  for(var i = 0; i < n; i++){
    var t = i - a[i];
    if(t >= 0)ans += b[t];
    t = i + a[i];
    if(t < n)b[t]++;
  }
  return ans;
}

Measured memory_bytes: 55184
#include <iostream>      // cout, endl, cin
#include <string>        // string, to_string, stoi
#include <vector>        // vector
#include <algorithm>     // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility>       // pair, make_pair
#include <tuple>         // tuple, make_tuple
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <deque>         // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset>        // bitset
#include <cctype>        // isupper, islower, isdigit, toupper, tolower
#include <cmath>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

int count(int N)
{
        int cnt = 0;
        while (N)
        {
                cnt += N % 10;
                N /= 10;
        }
        return cnt;
}

int main()
{
        int N,M;
        cin >> N>>M;
        vector<int> vec(N + 1);

        rep(i,M){
                int a, b;
                cin >> a >> b;
                vec[a-1]++;
                vec[b-1]++;
        }
        rep(i,N){
                cout << vec[i] << endl;
        }

        return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int a[1111],b[1111],c[1111],d[1111],p[1111],i,j,x,y,z;
main(){
  for(;scanf("%d%d%d",a,b,c)&&(*a+=*b+*c);){
    for(i=0,scanf("%d",p);i++<*p;scanf("%d%d%d%d",a+i,b+i,c+i,d+i),p[i]=2);
    for(i=0;i++<*p;)
      for(j=0;j++<*p;d[j]?p[a[j]]=p[b[j]]=p[c[j]]=1:(x=p[a[j]]&1)&(y=p[b[j]]&1)?p[c[j]]=0:x&(z=p[c[j]])?p[b[j]]=0:y&z?p[a[j]]=0:0);
    for(i=0;i<*a;printf("%d\n",p[++i]));
  }return 0;
}

Measured memory_bytes: 620
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    int flag=0;
    cin>>s;
    for(int i=0;i<s.size();i++)
        if(s[i]=='R'&&i%2)
        {
            flag=1;
            break;
        }
        else if(s[i]=='L'&&i%2==0)
        {
            flag=1;
            break;
        }
    if(flag)
        cout<<"No"<<endl;
    else
        cout<<"Yes"<<endl;
    return 0;
}


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

using namespace std;

int main(){
	int a;
  	cin>>a;
  vector<long long> v(a);
  for(int i=0;i<a;i++){
    cin>>v[i];
  }
  sort(v.begin(),v.end());
  for(int i=1;i<a;i++){
    if(v[i]==v[i-1]){cout<<"NO"<<endl;return 0;}
  }
  cout<<"YES"<<endl;
}





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

const int INF = INT_MAX;

int n, m;// number of (rows, columns)
vector<vector<char>> maze;
vector<vector<int>> d;// shortest path
int sx, sy;// x: row, y: column
int gx, gy;

int BFS(){// length of the shortest path
    queue<pair<int, int>> q;
    vector<int> dx = {1,0,-1,0};
    vector<int> dy = {0,1,0,-1};

    // initialize
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            d[i][j] = INF;
        }
    }
    d[sx][sy] = 0;
    q.push({sx, sy});

    // search
    pair<int, int> current;
    while(!q.empty()){
        current = q.front();
        q.pop();
        if(current.first == gx && current.second == gy) break;

        for(int i=0; i<4; i++){
            pair<int, int> next;
            next.first = current.first + dx[i];
            next.second = current.second + dy[i];
            if(0 <= next.first && next.first < n
            && 0 <= next.second && next.second < m
            && maze[next.first][next.second] != '#'// . or G
            && d[next.first][next.second] == INF){// have not visited
                q.push(next);
                d[next.first][next.second] = d[current.first][current.second] + 1;
            }
        }
    }
    
    return d[gx][gy];
}

int main(){
    cin >> n >> m;
    maze.resize(n, vector<char>(m));
    d.resize(n, vector<int>(m));

    int white = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin >> maze[i][j];
            if(maze[i][j] == '.') white += 1;
        }
    }
    
    sx = 0;
    sy = 0;
    gx = n-1;
    gy = m-1;

    int ans = white - (BFS() + 1);
    if(ans >= 0) cout << ans << endl;
    if(ans <  0) cout << -1 << endl;
    return 0;
}

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


int main(){
	char s[3][4];
	for(int i=0;i<3;i++){
		scanf("%s",s[i]);
	}
	char ans[4]="";
	for(int i=0;i<3;i++){
		ans[i]=s[i][i];
	}
	printf("%s",ans);


	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define rrep(i,n) for(int i=(int)n-1;i>=0;--i)
using namespace std;
using ll = long long;
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(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U& u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U& u,const V... v){for(auto& e:u)fill_v<T>(e,v...);}

struct range_edge_graph {
    int n;
    struct edge { int to; long long weight;};
    vector<vector<edge>> g;
    
    range_edge_graph(int _n) : n(_n) {
        g.resize(n<<2);
        for (int i = 1; i < n; ++i) {
            int c1 = i<<1|0, c2 = i<<1|1;
            g[i].push_back({c1, 0});
            g[i].push_back({c2, 0});
            g[c1+2*n].push_back({i+2*n, 0});
            g[c2+2*n].push_back({i+2*n, 0});
        }
        for (int i = n; i < 2*n; ++i) g[i].push_back({i+2*n, 0});
    }

    // from [l1, r1) to [l2, r2)
    void add_edge(int l1, int r1, int l2, int r2, long long w) {
        int id = g.size();
        for (l1 += n, r1 += n; l1 < r1; l1 >>= 1, r1 >>= 1) {
            if (l1 & 1) g[l1+2*n].push_back({id, 0}), l1++;
            if (r1 & 1) --r1, g[r1+2*n].push_back({id, 0});
        }
        vector<edge> node;
        for (l2 += n, r2 += n; l2 < r2; l2 >>= 1, r2 >>= 1) {
            if (l2 & 1) node.push_back({l2++, w});
            if (r2 & 1) node.push_back({--r2, w});
        }
        g.push_back(node);
    }

    vector<long long> dijkstra(int s) {
        s += n;
        vector<long long> dist(g.size(), numeric_limits<long long>::max());
        dist[s] = 0;
        using P = pair<long long, long long>;
        priority_queue<P, vector<P>, greater<P>> que;
        que.emplace(0, s);
        while (!que.empty()) {
            P p = que.top();
            que.pop();
            int v = p.second;
            if (dist[v] < p.first) continue;
            for (edge& e : g[v]) {
                if (dist[e.to] > dist[v] + e.weight) {
                    dist[e.to] = dist[v] + e.weight;
                    que.emplace(dist[e.to], e.to);
                }
            }
        }
        vector<long long> res(dist.begin() + n, dist.begin() + 2*n);
        return res;
    }
};

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

    int n, m;
    cin >> n >> m;
    
    range_edge_graph g(n);

    rep(i, m) {
        int l, r, c;
        cin >> l >> r >> c;
        l--; r--;
        g.add_edge(l, r+1, l, r+1, c);
    }

    auto d = g.dijkstra(0);
    if (d[n-1] == numeric_limits<ll>::max()) cout << -1 << '\n';
    else cout << d[n-1] << '\n';
}


Measured memory_bytes: 89488
N = int(input())
S1 = input()
S2 = input()
if S1[0] == S2[0]:
    count = 3
    start_idx = 1
    prev = 'v'
else:
    count = 6
    start_idx = 2
    prev = 'h'

skip = False

for i in range(start_idx, len(S1)):
    if skip:
        skip = False
        continue
    if S1[i] == S2[i]:  # current = v
        if prev == 'v':
            count *= 2
            prev = 'v'
        else:
            count *= 1
        prev = 'v'
    else:               # current = h
        if prev == 'v':
            count *= 2
        else:
            count *= 3
        prev = 'h'
        skip = True
print(count % (10**9 + 7))



Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define reint register int
#define coint const int
using namespace std;
typedef long long ll;
coint SIZE = 3000 + 5;
ll c[SIZE][SIZE];
ll li[SIZE], ans[SIZE];
ll xpow[SIZE];
int main(void) {
	int n;
	scanf("%d", &n);
	for (reint i = 0; i < n; ++i) scanf("%lld", &li[i]);
	for (reint i = 0; i <= n; ++i) {
		c[i][0] = 1;
		for (reint j = 1; j <= i; ++j)
			c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % n;
	}
	for (reint i = 0; i < n; ++i) {
		if (!li[i]) continue;
		ans[0] = (ans[0] + 1) % n;
		xpow[0] = 1;
		for (reint j = 1; j <= n; ++j) xpow[j] = xpow[j - 1] * (n - i) % n;
		for (reint j = 0; j < n; ++j)
			ans[j] = ((ans[j] - xpow[n - 1 - j] * c[n - 1][j]) % n + n) % n;
	}
	for (reint i = 0; i < n; ++i)
		printf("%lld%c", ans[i], " \n"[i == n - 1]);
	return 0;
}

Measured memory_bytes: 69248
N, X = map(int, input().split())
*V, = map(int, input().split())

MOD = 10**9 + 7

fact = [1]*(X+1)
rfact = [1]*(X+1)
for i in range(X):
    fact[i+1] = r = fact[i] * (i+1) % MOD
    rfact[i+1] = pow(r, MOD-2, MOD)

V.sort()

S = [0]*(X+1)
S[X] = 1
for i in range(N-1, -1, -1):
    T = [0]*(X+1)
    for k in range(X+1):
        S[k] %= MOD
        T[k] += S[k] * i % MOD
        T[k % V[i]] += S[k]
    S = T
print(sum(k*S[k] % MOD for k in range(V[0])) % MOD)

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

class MainClass : Scanner
{
    static void Main()
    {
        int N = RInt();
        long ans = 0;
        for (int a = 1; a < N; a++)
        {
            if (N % a == 0) ans--;
            ans += N / a;
        }
        
        WriteLine(ans);
    }
}



class Scanner
{
    public static string RString() => Console.ReadLine();
    public static int RInt() => ReadTuple<int>();
    public static long RLong() => ReadTuple<long>();
    public static double RDouble() => ReadTuple<double>();

    public static string[] RStrings() => Console.ReadLine().Split();
    public static int[] RInts() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
    public static long[] RLongs() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
    public static double[] RDoubles() => Array.ConvertAll(Console.ReadLine().Split(), double.Parse);
    public static int[] RInts(Func<string, int> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static long[] RLongs(Func<string, long> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static double[] RDoubles(Func<string, double> func) => Console.ReadLine().Split().Select(func).ToArray();
    //public static string[] RStrings(Func<string, string> func) => Console.ReadLine().Split().Select(func).ToArray();

    public static T1 ReadTuple<T1>()
    {
        var r = RString();
        var r1 = (T1)Convert.ChangeType(r, typeof(T1));
        return r1;
    }
    public static (T1, T2) ReadTuple<T1, T2>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        return (r1, r2);
    }
    public static (T1, T2, T3) ReadTuple<T1, T2, T3>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        var r3 = (T3)Convert.ChangeType(r[2], typeof(T3));
        return (r1, r2, r3);
    }
    public static (T1, T2, T3, T4) ReadTuple<T1, T2, T3, T4>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        var r3 = (T3)Convert.ChangeType(r[2], typeof(T3));
        var r4 = (T4)Convert.ChangeType(r[3], typeof(T4));
        return (r1, r2, r3, r4);
    }
}


Measured memory_bytes: 27572
from fractions import gcd

n, k = map(int, input().split())
a = tuple(map(int, input().split()))

g = max(a)

if k > g:
    print("IMPOSSIBLE")
    exit()

for i in a:
    g = gcd(g, i)

if k % g == 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Measured memory_bytes: 16424
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main(){
    string s;
    cin >> s;
    while(1) {
        if ( s.empty() ) break;
        int n = s.length();
        s = s.substr(0, n-1);
        n = s.length();
        if ( n == 1 ) break;
        if ( n & 1 ) continue;
        bool ok = true;
        rep(i, n/2) {
            if ( s[i] != s[i+n/2] ) ok = false; 
        }
        if ( ok ) break;
    }
    cout << s.length() << endl;
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <algorithm>
 
using namespace std;
 
int main(){
    int H,W;
    while(true){
        scanf("%d %d",&H,&W);
        if(H == 0 && W == 0) break;
        for(int i = 0; i < W; i++) printf("#");
        printf("\n");
 
        for(int i = 1; i < H-1; i++){
            printf("#");
            for(int j = 1; j < W-1; j++) printf(".");
            printf("#\n");
        }
 
        if(H > 1){
            for(int i = 0; i < W; i++) printf("#");
            printf("\n");
        }
        printf("\n");
    }
}



Measured memory_bytes: 2700
#include<stdlib.h>
#include<iostream>
#include<cstdio>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<map>
#include<string.h>
#include<queue>
using namespace std;

signed main() {
	int a, b, c; cin >> a >> b >> c;
	if (a % 2 || b % 2 || c % 2)puts("0");
	else if (a == b&&b == c)puts("-1");
	else {
		int d = abs(a - b), e = abs(a - c), f = abs(b - c);
		for (int i = 0;; i++) {
			if (d % 2 || e % 2 || f % 2) {
				cout << i << endl;
				break;
			}
			d /= 2;
			e /= 2;
			f /= 2;
		}
	}
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int main(){
    int A,B,C;
    cin >> A >> B >> C;
    
    int maximum = 0;
    maximum = max(A,B);
    maximum = max(maximum,C);
    
    
    cout << maximum*9+(A+B+C) << endl;
    return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#define Mod 998244353
int gcd(int x,int y){return y?gcd(y,x%y):x;}
int n,a,b;
long long sum,k,t,inv[300100],C[300100];
int main(){
	scanf("%d%d%d%lld",&n,&a,&b,&k);
	inv[1]=1;for(int i=2;i<=n;++i)inv[i]=(Mod-Mod/i)*inv[Mod%i]%Mod;
	C[0]=1;for(int i=1;i<=n;++i)C[i]=C[i-1]*(n-i+1)%Mod*inv[i]%Mod;
	t=gcd(a,b);a/=t;b/=t;
	if(k%t!=0){puts("0");return 0;}else k/=t;
	for(long long i=0;i<=b;++i)if((k-a*i)%b==0){t=i;break;}
	for(long long i=t;i*a<=k&&i<=n;i+=b){
		if((k-i*a)/b<=n)sum=(sum+C[i]*C[(k-i*a)/b])%Mod;
	}
	printf("%lld\n",sum);
	return 0;
}

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

int main(){
  int N, A, B;
  cin >> N >> A >> B;
  vector<int> P(3);
  for(int i=0; i<N; i++){
    int p;
    cin >> p;
    if(p<=A) P.at(0)++;
    else if(p<=B) P.at(1)++;
    else P.at(2)++;
  }
  int ans = min({P.at(0), P.at(1), P.at(2)});
  cout << ans << endl;
}

Measured memory_bytes: 256
def main():
    from itertools import permutations

    N, C = map(int, input().split())
    change_cost = [[int(x) for x in input().split()] for _ in range(C)]
    init_color = [[int(x) - 1 for x in input().split()] for _ in range(N)]

    ctr = [[0] * C for _ in range(3)]
    for r in range(N):
        for c in range(N):
            p = (r + c) % 3
            color = init_color[r][c]
            ctr[p][color] += 1

    mi = 1000 * 500 * 500 + 1
    for perm in permutations(range(C), r=3):
        it = iter(perm)
        t = 0
        for p in range(3):
            color_to_be = next(it)
            for color, count in enumerate(ctr[p]):
                t += change_cost[color][color_to_be] * count
        mi = min(mi, t)
    print(mi)


if __name__ == '__main__':
    main()


Measured memory_bytes: 5492
#include<iostream>
#include<algorithm>
#include<vector>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int m,n,b[500],r[500],p[1000],v[1000];
vector<vector<int> >e;
bool match(int s){
	if(s<0)return 1;
	rep(i,e[s].size())if(!v[e[s][i]]){
		v[e[s][i]]=1;
		if(match(p[e[s][i]]))return p[s]=e[s][i],p[e[s][i]]=s,1;
	}
	return 0;
}
int main(){
	while(cin>>m>>n,m){
		rep(i,m)cin>>b[i];rep(i,n)cin>>r[i];
		
		e.clear();e.resize(m+n);
		rep(i,m)rep(j,n)if(__gcd(b[i],r[j])>1)e[i].push_back(m+j),e[m+j].push_back(i);
		
		int ans=0;rep(i,m+n)p[i]=-1;
		rep(i,m){
			rep(j,m+n)v[j]=0;
			if(match(i))ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 2392
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iomanip>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long
#define Size(v) (int)v.size()

using namespace std;
// using namespace __gnu_pbds;


void solve() {
	ll a, b;
	cin >> a >> b;
	cout << a * b;
}

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

	int t = 1;
	while (t--) 
		solve();
	return 0;
}

Measured memory_bytes: 3600
def q2():
    A, B, K = (int(i) for i in input().split())

    if A >= K:
        A -= K
    else:
        K -= A
        A = 0
        if B >= K:
            B -= K
        else:
            B = 0

    print('{} {}'.format(A, B))


if __name__ == '__main__':
    q2()


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

int main()
{
    int n, a[4];
    int i;
    
    cin >> n;
    
    for(i = 3; i >= 0; i--) {
        a[i] = n % 10;
        n = n / 10;
    }
    
    if(a[0]+a[1]+a[2]+a[3] == 7) {
        cout << a[0] << "+" << a[1] << "+" << a[2] << "+" << a[3] << "=7\n";
    } else if(a[0]+a[1]+a[2]-a[3] == 7) {
        cout << a[0] << "+" << a[1] << "+" << a[2] << "-" << a[3] << "=7\n";
    } else if(a[0]+a[1]-a[2]+a[3] == 7) {
        cout << a[0] << "+" << a[1] << "-" << a[2] << "+" << a[3] << "=7\n";
    } else if(a[0]+a[1]-a[2]-a[3] == 7) {
        cout << a[0] << "+" << a[1] << "-" << a[2] << "-" << a[3] << "=7\n";
    } else if(a[0]-a[1]+a[2]+a[3] == 7) {
        cout << a[0] << "-" << a[1] << "+" << a[2] << "+" << a[3] << "=7\n";
    } else if(a[0]-a[1]+a[2]-a[3] == 7) {
        cout << a[0] << "-" << a[1] << "+" << a[2] << "-" << a[3] << "=7\n";
    } else if(a[0]-a[1]-a[2]+a[3] == 7) {
        cout << a[0] << "-" << a[1] << "-" << a[2] << "+" << a[3] << "=7\n";
    } else if(a[0]-a[1]-a[2]-a[3] == 7) {
        cout << a[0] << "-" << a[1] << "-" << a[2] << "-" << a[3] << "=7\n";
    }
    
    return 0;
}

Measured memory_bytes: 256
import sys
from heapq import heappush, heappop, heapify
from collections import defaultdict

A,B,M=map(int, sys.stdin.readline().split())
a=map(int, sys.stdin.readline().split())
b=map(int, sys.stdin.readline().split())

ans=float("inf")
for _ in range(M):
    x,y,c=map(int, sys.stdin.readline().split())
    ans=min(ans,a[x-1]+b[y-1]-c)

a.sort()
b.sort()
print min(ans,a[0]+b[0])

Measured memory_bytes: 48992
#include <bits/stdc++.h>
#define lli long long int
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for(lli i=0, i##_len=(lli)(n); i<i##_len; i++)
#define repf(i, n) for(lli i=1, i##_len=(lli)(n+1); i<i##_len; i++)
#define per(i, n) for(lli i=((lli)(n))-1; i>=0; i--)
#define perf(i, n) for(lli i=((lli)(n)); i>0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<lli>
#define vvi vector<vector<lli>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
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 namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    lli n;
    cin >> n;
    cout << n*n*n << endl;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;
//using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;


    void solve()
    {
        WriteLine(700 + 100 * ReadLine().Count(i => i == 'o'));
    }

}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}

Measured memory_bytes: 13268
#include<stdio.h>
int main(){
  int i,n,c=0,a=-1,m[]={1,5,3,3,3,3,3,4,3,4};
  char s[2000],d[][6]={"",".,!? ","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
  scanf("%d",&n);
  while(n--){
    scanf("%s",s);
    for(i=0;s[i];c=s[i++]-'0'){
      if(s[i]-'0')a++;
      else{
	if(c)printf("%c",d[c][a%m[c]]);
	a=-1;
      }
    }
    printf("\n");
  }
  return 0;
}

Measured memory_bytes: 572
S = input()
T = input()
Q = int(input())

N = len(S)
M = len(T)
accSA = [0] * (N + 1)
accSB = [0] * (N + 1)
accTA = [0] * (M + 1)
accTB = [0] * (M + 1)

for i, s in enumerate(S, start=1):
    accSA[i] = accSA[i - 1]
    accSB[i] = accSB[i - 1]
    if s == 'A':
        accSA[i] += 1
    else:
        accSB[i] += 1

for i, t in enumerate(T, start=1):
    accTA[i] = accTA[i - 1]
    accTB[i] = accTB[i - 1]
    if t == 'A':
        accTA[i] += 1
    else:
        accTB[i] += 1

ans = []
for _ in range(Q):
    leftS, rightS, leftT, rightT = map(int, input().split())

    sA = accSA[rightS] - accSA[leftS - 1]
    sB = accSB[rightS] - accSB[leftS - 1]
    tA = accTA[rightT] - accTA[leftT - 1]
    tB = accTB[rightT] - accTB[leftT - 1]

    if (sA + sB * 2) % 3 == (tA + tB * 2) % 3:
        ans.append('YES')
    else:
        ans.append('NO')

print(*ans, sep='\n')

Measured memory_bytes: 16016
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	
	
	public static void main(String[] args) throws IOException {
		//Scanner sc = new Scanner(System.in);
		
		Scanner sc = new Scanner(System.in);
		
		final int w = sc.nextInt();
		final int h = sc.nextInt();
		final int n = sc.nextInt();
		
		int[] xs = new int[n];
		int[] ys = new int[n];
		for(int i = 0; i < n; i++){
			xs[i] = sc.nextInt();
			ys[i] = sc.nextInt();
		}
		
		int cost = 0;
		for(int i = 1; i < n; i++){
			final int dx = xs[i] - xs[i-1];
			final int dy = ys[i] - ys[i-1];
		
			if(dx * dy >= 0){
				final int common = Math.min(Math.abs(dx), Math.abs(dy));
				cost += (common + (Math.abs(dx) - common) + (Math.abs(dy) - common));
			}else{
				cost += (Math.abs(dx) + Math.abs(dy));
			}
		}
		System.out.println(cost);
	}
}

Measured memory_bytes: 25452
#include <bits/stdc++.h> 
using namespace std;
#define endl "\n"
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef long long ll;
#define MOD 1000000007;

int main() 
{ 
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int a,b,c; cin >>a >> b >> c;
    int k; cin >> k;
    int d = 0;
    while(b <= a){
      b = b * 2;
      d++;
    }
    while( c <= b){
      c = c * 2;
      d++;
    }
    if(d>k){
      cout << "No" << endl;
    }
    else cout << "Yes" << endl;
    
    
    return 0;
}

Measured memory_bytes: 3652
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>

using namespace std;
#define ll long long

int main(void){
    vector<ll> fib(100001);
    for(ll i=0;i<100001;i++){
        if(i==0) fib.at(0)=1;
        else if(i==1) fib.at(1)=1;
        else fib.at(i)=(fib.at(i-1)+fib.at(i-2))%1000000007;
    }
    ll N,M,ans=1;
    cin>>N>>M;
    if(M==0){
        cout<<fib.at(N)<<endl;
        return 0;
    }
    vector<ll> broken(M),ret(M+1);
    for(ll i=0;i<M;i++) cin>>broken.at(i);
    for(ll i=0;i<M+1;i++){
        if(i==0) ret.at(0)=broken.at(0)-1;
        else if(i==M) ret.at(M)=N-broken.at(M-1)-1;
        else{
            ret.at(i)=broken.at(i)-broken.at(i-1)-2;
            if(ret.at(i)==-1){
                cout<<0<<endl;
                return 0;
            }
            else if(ret.at(i)==-2) ret.at(i)=0;
        }
    }
    for(ll i=0;i<100000;i++){
        if(i==0) fib.at(0)=1;
        else if(i==1) fib.at(1)=1;
        else fib.at(i)=(fib.at(i-1)+fib.at(i-2))%1000000007;
    }
    
    for(ll i=0;i<M+1;i++) ans=(ans*fib.at(ret.at(i)))%1000000007;
    cout<<ans<<endl;
    return 0;
}


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


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

ll three_gcd(ll a, ll b, ll c) {
  return gcd(gcd(a, b), c);
}

void solve(ll K){
  ll sum = 0;
  for (int i=1; i<=K; i++) {
    for (int j=1; j<=K; j++) {
      for (int k=1; k<=K; k++) {
        sum += three_gcd(i, j, k);
      }
    }
  }
  cout << sum << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    ll K;
    scanf("%lld",&K);
    solve(K);
    return 0;
}


Measured memory_bytes: 3684
import fractions
N,M = map(int,input().split())
S = input()
T = input()
G = fractions.gcd(N,M)
L = (N * M) // G
div_N = N // G
div_M = M // G

i = 0
j = 0

while i < N and j < M:
  if S[i] != T[j]:
    print(-1)
    exit()
  else:
    i += div_N
    j += div_M
print(L)





Measured memory_bytes: 5432
#include<stdio.h>
#include<stdlib.h>
#define N 100000
#define NIL -1

struct Node {int p,l,r;};
struct Node T[N];

int getDepth(u){
  int d = 0;
  while(T[u].p != NIL){
    u = T[u].p;
    d++;
  }
  return d;
}

void printChildren(u){
  int c = T[u].l;
  while(c != NIL){
    if(T[c].r == NIL) printf("%d",c);
    else printf("%d, ",c);
    c = T[c].r;
  }
}

int main(){
  int n,i,j,d,v,c,l,r;
  
  scanf("%d",&n);

  for(i = 0; i < n; i++){
    T[i].p = NIL;
    T[i].l = NIL;
    T[i].r = NIL;
  }

  for(i = 0; i < n; i++){
    scanf("%d%d",&v,&d);
    for(j = 0; j < d; j++){
      scanf("%d",&c);
      if(j == 0) T[v].l = c;
      else T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  for(i = 0; i < n; i++){
    if(T[i].p == NIL) r = i;
  }
  for(i = 0; i < n; i++){
    printf("node %d: parent = %d, depth = %d, ",i,T[i].p,getDepth(i));
    if(T[i].p == NIL) printf("root, ");
    else if(T[i].l == NIL) printf("leaf, ");
    else printf("internal node, ");
    printf("[");
    printChildren(i);
    printf("]\n");
    
  }

}



Measured memory_bytes: 3024
#include <bits/stdc++.h>
using namespace std;
int grid[110][110];
int main(){
	int w,h;	cin>>w>>h;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++)	cin>>grid[i][j];
	}
	w++;	h++;
	queue<pair<int,int>> q;	q.push(make_pair(0,0));	grid[0][0]=-1;
	int mx[2][6]={{-1,0,-1,1,-1,0},{0,1,-1,1,0,1}};
	int my[2][6]={{-1,-1,0,0,1,1},{-1,-1,0,0,1,1}};
	while(!q.empty()){
		auto now=q.front();	q.pop();
		int y=now.first,x=now.second;
		int u=y%2;
		for(int i=0;i<6;i++){
			int ny=y+my[u][i],nx=x+mx[u][i];
			if(!(0<=ny&&ny<=h)||!(0<=nx&&nx<=w))	continue;
			if(grid[ny][nx]==0){
				grid[ny][nx]=-1;
				q.push(make_pair(ny,nx));
			}
		}
	}
	int ans=0;
	for(int i=1;i<h;i++){
		int u=i%2;
		for(int j=1;j<w;j++){
			if(grid[i][j]==1){
				for(int k=0;k<6;k++){
					if(grid[i+my[u][k]][j+mx[u][k]]==-1)	ans++;
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

using namespace std;

int main(void) {
	unsigned long long L,R,MIN;
	int ans=2020;
	cin >> L >> R ;


	for (unsigned long long I = L; (I < R) && ((I - L) < 2019); I++)
	{
		for (unsigned long long J = I + 1; (J <= R) && ((J - I) < 2019); J++)
		{
			ans = min((int)(I*J % 2019), ans);
		}
	}
	cout << ans << endl;

	return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
#include <utility>

using namespace std;

queue<string> qs;
queue<int> qi;

void push(string s, int i)
{
    qs.push(s);
    qi.push(i);
}

void pop()
{
    qs.pop();
    qi.pop();
}

int main() {
    int n,q;

    pair<string, int> pair;
    int time = 0;
    cin >> n >> q;
    while(cin >> pair.first >> pair.second)
    {
        push(pair.first,pair.second);
    }
    
    while(!qs.empty())
    {
        int rem = qi.front();
        if(rem <= q)
        {
            time += rem;
            cout << qs.front() << " " << time << endl;
        }
        else
        {
            time += q;
            push(qs.front(), rem - q);
        }
        pop();
    }
    
    return 0;
}

Measured memory_bytes: 5688
#include<stdio.h>
int n,m;
int t[101][101];

int que[2000];
int qa,qb;

int calc(int start,int goal){
  int i,pos;
  int visited[101];
  int cost[101];
  
  for(i=0;i<101;i++){
    visited[i]=0;
    cost[i]=0;
  }
  qa=0;
  qb=1;
  que[0]=start;
  visited[start]=1;
  
  while(qa<qb){
    pos=que[qa];
    qa++;
    if(pos==goal)return cost[pos];
    
    for(i=1;i<=n;i++){
      if(t[pos][i]==0)continue;
      if(visited[i]==1)continue;
      visited[i]=1;
      que[qb]=i;
      qb++;
      cost[i]=cost[pos]+1;
    }
  }
  return 9999;
}

int main(){
  int i,j;
  int id,k,l;
  int s,d,v;
  int ans;
  
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d %d",&id,&k);
    for(j=0;j<k;j++){
      scanf("%d",&l);
      t[id][l]=1;
    }
  }

  scanf("%d",&m);
  for(i=0;i<m;i++){
    scanf("%d %d %d",&s,&d,&v);
    ans=calc(s,d)+1;
    if(ans<=v&&ans!=10000)printf("%d\n",ans);
    else printf("NA\n");
  }
  return 0;
}

Measured memory_bytes: 652