akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
using ll = long long; using ull = unsigned long long;
#define int ll
using vb = vector<bool>; using vvb = vector<vb>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
using pii = pair<int,int>; using pll = pair<ll,ll>;
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(ll i = (ll)(b)-1;i >= a;--i)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9+7;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
pair<vl,vb> eratosthenes_sieve(ll n){
  vb is_prime(n+1,true);
  vl prime;
  is_prime[0] = false;
  is_prime[1] = false;
  FOR(i,2,sqrt(n)+1){
    if(!is_prime[i]) continue;
    for(ll j=2*i;j<=n;j+=i) is_prime[j] = false;
    prime.eb(i);
  }
  return {prime,is_prime};
}
signed main(){
  int n;
  cin >> n;
  vi a(n);
  REP(i,n) cin >> a[i];
  int q;
  cin >> q;
  set<int> st;
  for(int i=0;i<(1<<n);++i){
    int t = 0;
    REP(j,n) if(i>>j&1) t+=a[j];
    st.insert(t);
  }
  REP(i,q){
    int m;
    cin >> m;
    if(st.find(m)!=st.end()) puts("yes");
    else puts("no");
  }
}



Measured memory_bytes: 3112
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>

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

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

int main(){
    int h,w,k,ans=0; cin >> h >> w >> k;
    vector<string> c(h);
    rep(i,h) cin >> c[i];
    rep(is,1<<h)rep(js,1<<w){
        int cnt = 0;
        rep(i,h)rep(j,w){
            if(is>>i&1) continue;
            if(js>>j&1) continue;
            if(c[i][j] == '#') cnt++;
        }
        if(cnt == k) ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3536
k=int(input())

l0=[1,2,3,4,5,6,7,8,9]


for s in l0:
    temp=str(s)
    x=int(temp[-1])

    if x==0:
        l0.append(int(temp+"0"))
        l0.append(int(temp+"1"))
        #l0.append(int(temp+"9"))
    if x>=1 and x<=8:
        l0.append(int(temp+str(x-1)))
        l0.append(int(temp+str(x)))
        l0.append(int(temp+str(x+1)))
    if x==9:
        #l0.append(int(temp+"0"))
        l0.append(int(temp+"8"))
        l0.append(int(temp+"9"))
    
    if len(l0)>100002:
        break


print(l0[k-1])


Measured memory_bytes: 45272
# -*- coding: utf-8 -*-

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

if k == 0:
    print(n * n)
    exit()

cnt = 0
for b in range(k + 1, n + 1):
    q, r = divmod(n - k + 1, b)
    cnt += (b - k) * q + min((b - k), r)

print(cnt)


Measured memory_bytes: 2940
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

int main(){
    int x;
    while(cin>>x){
        int ans=8;
        if(x<600){
            ans=8;
        }else if(x<800){
            ans=7;
        }else if(x<1000){
            ans=6;
        }else if(x<1200){
            ans=5;
        }else if(x<1400){
            ans=4;
        }else if(x<1600){
            ans=3;
        }else if(x<1800){
            ans=2;
        }else{
            ans=1;
        }
        cout<<ans<<endl;
    }
    return 0;
}

Measured memory_bytes: 3192
import math
import sys
import numpy as np

#n, a, b= map(int, input().split())
w, h, n  = map(int, input().split())
#s = list(str(input()))
#n = int(input())
#a = list(map(int, input().split()))

x = [0]*n
y = [0]*n
a = [0]*n
for i in range(n):
  x[i], y[i], a[i] = map(int, input().split())
area = np.ones([w,h])

for i in range(n):
  if(a[i] == 1):
    area[:x[i],:] = 0
  if(a[i] == 2):
    area[x[i]:,:] = 0
  if(a[i] == 3):
    area[:,:y[i]] = 0
  if(a[i] == 4):
    area[:,y[i]:] = 0

print(np.count_nonzero(area == 1))

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

using namespace std;

long long lcm(long n, long m){
  return (n*m)/__gcd(n,m);
}

int main()
{
  long long n, m;
  cin >> n >> m;
  if (n > m)swap(n, m);
  long long i1 = n;
  long long i2 = m;
  while (i1%n || i2%n){
    i1 += n;
    i2 += m;
  }
  cout << i2 << '\n';
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h> 
using namespace std;
long long n,m,i,j,ans,x,y;
int main(){
	cin>>n;
	while (n--){
		cin>>x>>y;
		if (x==1&&y==1){
			cout<<"0\n";
			continue;
		}
		if (x==y||abs(x-y)==1){
			cout<<(min(x,y)-1)*2<<endl;
			continue;
		}
		ans=((long long)(sqrt(x*y)));
		if (ans*ans>=x*y) ans--; 
		if (ans*(ans+1)>=x*y){
			cout<<ans*2-2<<endl;
			continue;
		}
		cout<<ans*2-1<<endl;
	}
	return 0;
}

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

int main ()
{
	int m, f, r, total;
	while (cin >> m >> f >> r)
	{
		if (m==-1 && f==-1 && r==-1)
			break;
		else
		{
			total = m + f;
			if (m==-1 || f==-1) cout << "F\n";
			else if (r>=50)
				cout << "C\n";	
			else if (total>=80)
				cout << "A\n";
			else if (total>=65 && total<80)
				cout << "B\n";
			else if (total>=50 && total<65)
				cout << "C\n";
			else if (total>=30 && total<50)
				cout << "D\n";
			else if (total<30)
				cout << "F\n";					
		}	
	}
	return 0;
}

Measured memory_bytes: 3076
def main():
    N = int(input())
    A = [[int(i) for i in input().split()] for _ in range(N)]
    dp = [[0]*3 for _ in range(N)] # dp[i][j]:i日目に行動jをするときの最大値
    dp[0] = A[0]
    for i in range(1, N):
        dp[i][0] = A[i][0] + max(dp[i-1][1], dp[i-1][2])
        dp[i][1] = A[i][1] + max(dp[i-1][2], dp[i-1][0])
        dp[i][2] = A[i][2] + max(dp[i-1][0], dp[i-1][1])
    print(max(dp[N-1]))
main()

Measured memory_bytes: 74840
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,sum1,sum2,a[110];
int main(){
    scanf("%d",&n);
    sum1=0;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        sum1+=a[i];
    }
    scanf("%d",&m);
    sum2=sum1;
    while(m--){
        int p,x;
        scanf("%d%d",&p,&x);
        if(x>=a[p-1]) sum1+=x-a[p-1];
        else sum1-=a[p-1]-x;
        printf("%d\n",sum1);
        sum1=sum2;
    }
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <chrono>
#include <random>

#define int long

const int INF = 1000000009;
const long LINF = 1123456789012345678;

int n;
int h[105];
int ans;

signed main(){
	scanf("%ld", &n);
	for(int i = 1; i <= n; i++){
		scanf("%ld", &h[i]);
		if(h[i] >= h[i-1]){
			ans += h[i] - h[i-1];
		}
	}
	printf("%ld\n", ans);
	return 0;
}


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

int main(){
  int N;
  int A,B,C,D;
  std::string op1,op2,op3;
  
  std::cin >> N;
  A=N/1000;
  N-=A*1000;
  B=N/100;
  N-=B*100;
  C=N/10;
  N-=C*10;
  D=N;

  if(A+B+C+D==7){
    op1="+";
    op2="+";
    op3="+";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A+B+C-D==7){
    op1="+";
    op2="+";
    op3="-";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A+B-C+D==7){
    op1="+";
    op2="-";
    op3="+";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A+B-C-D==7){
    op1="+";
    op2="-";
    op3="-";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A-B+C+D==7){
    op1="-";
    op2="+";
    op3="+";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A-B+C-D==7){
    op1="-";
    op2="+";
    op3="-";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A-B-C+D==7){
    op1="-";
    op2="-";
    op3="+";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }
  else if(A-B-C-D==7){
    op1="-";
    op2="-";
    op3="-";
    std::cout << A << op1 << B << op2 << C << op3 << D << "=7" << std::endl;
  }    
}

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

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL<<62);


LL N, K;
vector<LL> A;
int main(int argc, char* argv[]){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> K;
	A.push_back(0);
	for(int i=0; i<N; i++){
		LL a;
		cin >> a;
		A.push_back(a);
	}

	for(int i=K+1; i<=N; i++){
		if(A[i] > A[i-K]){
			printf("Yes\n");
		}
		else{
			printf("No\n");
		}
	}
	return 0;
}

Measured memory_bytes: 5252
N = input()
A = map(str,raw_input().split())
Ab = A[:]
As = A[:]

def BubbleSort(A,N):
    for i in range(N):
        flag = 0
        for i in range(1,N):
            j = N - i
            if A[j][1] < A[j-1][1]:
                v = A[j]
                A[j] = A[j-1]
                A[j-1] = v
                flag = 1

def SelectionSort(A,N):
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j][1] < A[minj][1]:
                minj = j
        v = A[i]
        A[i] = A[minj]
        A[minj] = v

def Output(A):
    for i in range(N-1):
        print A[i],
    print A[-1]

def Stable(AA):
    n = []
    for i in range(N-1):
        if AA[i][1] == AA[i+1][1] and AA[i][1] not in n:
            n.append(AA[i][1])
    l = []
    m = []
    for item in n:
        for i in range(N):
            if AA[i][1] == item:
                l.append(AA[i])
            if A[i][1] == item:
                m.append(A[i])
    if l == m:
        msg = "Stable"
    else:
        msg = "Not stable"
    print msg

SelectionSort(As,N)
BubbleSort(Ab,N)

Output(Ab)
Stable(Ab)
Output(As)
Stable(As)

Measured memory_bytes: 6456
//#include <bits/stdc++.h>
//#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>


#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

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

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


int main(void) {
	vector<int> a(3);
	int count = 0;
	int now;

	FOR(i, 0, 2) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	now = a[0];
	count = 1;

	FOR(i, 1, 2) {
		if (a[i] != now) {
			count++;
			now = a[i];
		}
	}

	cout << count << endl;




	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>                                                 
                                                                  
int main(void){                                                   
   int A,B,C,D,t,a;                                               
   scanf("%d %d %d %d",&A,&B,&C,&D);                              
                                                                  
   while(0 < A && 0 < C){                                         
         C -= B;                                                  
         if(C <= 0){                                              
            printf("Yes");                                        
            break;                                                
         }                                                        
         A -= D;                                                  
         if(A <= 0){                                              
            printf("No");                                         
            break;                                                
         }                                                        
      }                                                           
   return 0;                                                      
}   

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

#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); ++(i))
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); --(i))

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using int32 = int;
using int64 = long long;

const int   mod   = 1e9 + 7;
const int32 inf   = 1001001001;
const int64 infll = 1001001001001001001ll;

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

struct IoSetup { IoSetup(int x = 10){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = int64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.first << " " << p.second; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

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

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void solver() {
  int N = input();
  vector< int > L(N), R(N);
  range(i, 0, N) cin >> L[i] >> R[i];

  int64 ans = -infll;

  auto f = [&]() {
    vector< bool > used(N);
    vector< pair< int, int > > l, r;
    range(i, 0, N) {
      l.emplace_back(L[i], i);
      r.emplace_back(-R[i], i);
    }
  
    whole(sort, l);
    whole(sort, r);
    whole(reverse, l);
    whole(reverse, r);
  
    int idx_l = 0, idx_r = 0;
    int64 sum = 0;
    while (true) {
      while (idx_l != N && used[l[idx_l].second]) idx_l++;
      if (idx_l != N) {
        sum += 2 * l[idx_l].first;
        used[l[idx_l].second] = true;
      }
      
      chmax(ans, sum);
  
      while (idx_r != N && used[r[idx_r].second]) idx_r++;
      if (idx_r != N) {
        sum += 2 * r[idx_r].first;
        used[r[idx_r].second] = true;
      }
  
      chmax(ans, sum);
  
      if (idx_l == N) break;
    }
  };

  range(_, 0, 2) {
    f();
    range(i, 0, N) {
      swap(L[i], R[i]);
      L[i] *= -1;
      R[i] *= -1;
    }
  }

  cout << ans << endl;
}

signed main(int argc, char *argv[]) {
  solver();
}


Measured memory_bytes: 4044
X = int(input())

def dfs(i, sum):
  if sum > X:
    return False
  if i==6:
    return sum == X
  val = 100 + i
  for j in range((X-sum)//val + 1):
    if dfs(i+1, sum + val * j):
      return True
  return False

if dfs(0, 0):
  print(1)
else:
  print(0)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std; 
int main()
{
	int n,x,t,use=0,np=0;
	cin>>n>>x>>t;
	while(np<n)
	{
		use++;
		np+=x;
	}
	cout<<use*t<<endl;
	return 0;
}

Measured memory_bytes: 3632
#include <cstdio>
#include <cctype>
#include <utility>
#include <sstream>
#include <iomanip>
#include <string>
#include <array>
#include <algorithm>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
_T _DEF(T,in,int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_DEF(int,in,){return in<int>(gcu());}
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_DEF(bool, scan, int &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_SCAN(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}}
#endif
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{
	int e,b=0,s=1; range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it { int v, s; it (int _v, int _s) : v(_v), s(_s) {} operator int()const{return v;} operator int&(){return v;} int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin() {return {b, s};} it end() {return {e, s};}};

int loop(int n) {
	static array<int, 10000> m {};
	if (n == 6174)
		return 0;
	else if (m[n])
		return m[n];
	stringstream s;
	s << setw(4) << setfill('0') << n;
	string a = s.str();
	sort(begin(a), end(a));
	int S {stoi(a)};
	reverse(begin(a), end(a));
	int L {stoi(a)};
	return m[n] = loop(L - S) + 1;
}

int main() {
	for (;;) {
		string a;
		scan(a);
		if ([&] {
			for (int i: range(1, 4))
				if (a[0] != a[i])
					return false;
			return true;}()) {
			if (a[0] == '0')
				return 0;
			else {
				outl("NA");
				continue;
			}
		}
		outl(loop(stoi(a)));
	}
}


Measured memory_bytes: 3168
n,k = map(int,input().split())
v = list(map(int,input().split()))

m=[]
for i in range(k+1):
 for j in range(min(k,n)-i+1):
  # print(i,j)
  t=sorted(v[:i]+v[:~j:-1])

  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m.append(sum(t))
print(max(m))


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int h, w, M = 1e9+7;
    cin >>h >> w;
    vector<string> s(h);
    vector<vector<int>> dp(h, vector<int> (w));
    for (auto &i: s)
        cin >> i;
    dp[0][0] = 1;
    for (int i=1; i<w; i++){
        dp[0][i] = dp[0][i-1];
        if (s[0][i]=='#')
            dp[0][i] = 0;
    }
    for (int i=1; i<h; i++){
        dp[i][0] = dp[i-1][0];
        if (s[i][0]=='#')
            dp[i][0] = 0;
    }
    for (int i=1; i<h; i++)
        for (int j=1; j<w; j++){
            dp[i][j] = dp[i-1][j]+dp[i][j-1] ;
            if (s[i][j]=='#') dp[i][j] = 0;
            if (dp[i][j]>M) dp[i][j] -= M ;
        }
    cout << dp.back().back() << "\n";
}

Measured memory_bytes: 8536
//
//  RedAndBlack.cpp
//  
//
//  Created by ??°????????? on 2015/10/30.
//
//

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20], check[20][20];

void dfs(int x, int y) {
//    cout << x << ", " << y << endl;
    
    if (room[y][x] == 0) return;
    
    num++;
    
//    cout << "num: " << num << endl;
    
    room[y][x] = 0;
    
    // ??????????????¢?´¢
    if (y-1 > -1) dfs(x, y-1);
    if (y+1 < h) dfs(x, y+1);
    if (x-1 > -1) dfs(x-1, y);
    if (x+1 < w) dfs(x+1, y);

    return;
}

int main() {
    while (cin >> w >> h) {
        if (w == 0 && h == 0) break;
        
        int manX, manY;
        
        for (int i = 0; i < h; i++) {
            string s;
            cin >> s;
            for (int j = 0; j < w; j++) {
                if (s[j] == '.') {
                    room[i][j] = 1;
                } else if (s[j] == '#') {
                    room[i][j] = 0;
                } else if (s[j] == '@') {
                    room[i][j] = 1;
                    manX = j;
                    manY = i;
                }
            }
        }
        
        dfs(manX, manY);
        
        cout << num << endl;
        
        num = 0;
    }
    
    return 0;
}

Measured memory_bytes: 1212
import java.util.Arrays;
import java.util.Scanner;

//Power Calculus
public class Main{

	int n;
	int[] k;
	boolean[] u;

	boolean reach(long b, long x, int rest){
		long r = b+x;
		while(rest--!=0)r+=r;
		return n<=r;
	}

	boolean dfs(int d, int x, int max, int len){
		if(d==0){
			return u[n];
		}
		if(d==1){
			return u[Math.abs(x-n)];
		}
//		if(!reach(x, max, d))return false;
		for(int i=len-1;i>=0;i--){
			int s = k[i]+x;
			if(!(2*n<s||u[s]||!reach(s, Math.max(max, s), d-1))){
				if(d!=1||u[Math.abs(s-n)]){
					u[s] = true;
					k[len] = s;
					if(dfs(d-1, s, Math.max(max, s), len+1))return true;
					u[s] = false;
				}
			}
			s = x-k[i];
			if(!(s<0||!reach(s, max, d-1)||u[s])){
				if(d!=1||u[Math.abs(s-n)]){
					u[s] = true;
					k[len] = s;
					if(dfs(d-1, s, max, len+1))return true;
					u[s] = false;
				}
			}
			s = k[i] - x;
			if(!(s<0||!reach(s, max, d-1)||u[s])){
				if(d!=1||u[Math.abs(s-n)]){
					u[s] = true;
					k[len] = s;
					if(dfs(d-1, s, max, len+1))return true;
					u[s] = false;
				}
			}
		}
		return false;
	}

	void debug(Object...o){
		System.out.println(Arrays.deepToString(o));
	}

	void run(){
//		long T = System.currentTimeMillis();
//		for(n=1;n<=1000;n++){
//			long t = System.currentTimeMillis();
//			int x = 1, res = 0;
//			while(x<n){
//				x*=2; res++;
//			}
//			for(;;){
//				u = new boolean[2500];
//				k = new int[res+1];
//				u[1] = true;
//				k[0] = 1;
//				if(dfs(res, 1, 1, 1)){
//					System.out.println("N:"+n+" ANS:"+res+" Time:"+(System.currentTimeMillis()-t)); break;
//				}
//				res++;
//			}
//		}
//		System.out.println("Time:"+(System.currentTimeMillis()-T));
				Scanner sc = new Scanner(System.in);
				for(;;){
					n = sc.nextInt();
					if(n==0)break;
					int x = 1, res = 0;
					while(x<n){
						x*=2; res++;
					}
					for(;;){
						u = new boolean[2500];
						k = new int[res+1];
						u[1] = true;
						k[0] = 1;
						if(dfs(res, 1, 1, 1)){
							System.out.println(res); break;
						}
						res++;
					}
				}
	}

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

Measured memory_bytes: 12000
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
const double PI = acos(-1.0);
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
int main(){
  int n,m,e,f;
  cin >> n >> m;
  vector<int> a(n),b(n),c(m),d(m),l(m);
  REP(i,n)cin >> a[i] >> b[i];
  REP(i,m)cin >> c[i] >> d[i];
  REP(i,n){
    REP(j,m){
      l[j]=abs(c[j]-a[i])+ abs(d[j]-b[i]);
    }
    SORT(l);
    REP(j,m){
      if(l[0]==abs(c[j]-a[i])+abs(d[j]-b[i])){
        cout << j+1 << endl;
        break;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	scanner.Split(bufio.ScanWords)
	n := nextInt()
	for i := 0; i < n; i++ {
		a := -1
		b := -1
		ans := true
		// それぞれの筒の一番上にあるたまの数字に着目すればよい
		for j := 0; j < 10; j++ {
			x := nextInt()
			if x > a {
				a = x
			} else if x > b {
				b = x
			} else {
				ans = false
			}
		}
		if ans {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}


Measured memory_bytes: 1064
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

int main() {
  string S; cin >> S;
  int L = S.size();

  int vmin = 1e9;
  for (int i = 1; i <= L; ++i) {
    if (S[i - 1] != S[i]) {
      vmin = min(vmin, max(i, L-i));
    }
  }
  cout << vmin << endl;
}


Measured memory_bytes: 3780
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,time


MAP = [list('z' + input() + 'z') for _ in range(8)]
MAP = [list('z'*10)] + MAP + [list('z'*10)]

def check(x0,y0,turn):
    if MAP[y0][x0] != '.':
        return 0, []

    if turn:
        me,you = 'o','x'
    else:
        me,you = 'x','o'

    ans = 0
    dx = [-1, 0, 1,-1, 1,-1, 0, 1]
    dy = [-1,-1,-1, 0, 0, 1, 1, 1]
    rev = []
    for i in range(8):
        x,y = x0,y0
        cnt = 0
        tmprev = []
        while True:
            x += dx[i]
            y += dy[i]
            if MAP[y][x] == you:
                tmprev.append([x,y])
                cnt += 1
                continue
            elif MAP[y][x] == me:
                break
            else:
                cnt = 0
                tmprev = []
                break
        ans += cnt
        rev = tmprev + rev
    return ans,rev

btmp = -1
turn = True
while True:
    maxtmp = 0
    maxrev = []
    my = []
    ra = list(range(1,9))
    if not turn:
        ra = list(reversed(ra))

    #print(turn,ra)
    for y in ra:
        for x in ra:
            #print(turn,x,y)
            tmp,rev = check(x,y,turn)
            if maxtmp < tmp:
                maxtmp = tmp
                maxrev = rev[:]
                my = [x,y]

    if my:
        if turn:
            MAP[my[1]][my[0]] = 'o'
        else:
            MAP[my[1]][my[0]] = 'x'

    for x,y in maxrev:
        if MAP[y][x] == 'o':
            MAP[y][x] = 'x'
        else:
            MAP[y][x] = 'o'

    #print(maxtmp,my,rev)

    #for m in MAP:
    #    print(''.join(m))

    if btmp == 0 and maxtmp == 0:
        break

    btmp = maxtmp
    turn = not turn

for y in range(1,9):
    print(''.join(MAP[y][1:9]))



Measured memory_bytes: 7424
#include      <iostream>
#include       <iomanip>
#include        <string>
#include        <cstdio>
#include         <cmath>
#include       <cstring>
#include     <algorithm>
#include        <vector>
#include         <queue>
#include         <deque>
#include           <map>
#include <unordered_map>
#include           <set>
#include        <bitset>
#include       <fstream>
#include         <ctime>
#include         <stack>

#define               P(n, f) cout<<n<<(f?'\n':' ')
#define               Po(n, f) out<<n<<(f?'\n':' ')
#define                            pi pair<int,int>
#define                                LL long long
#define                             pLL pair<LL,LL>
#define                      ULL unsigned long long
#define                            lowbit(x) x&(-x)
#define                                mp make_pair
#define                                elif else if
#define               irange(i,arr) for(auto&i:arr)
#define        range(i, a, b) for(int i=a;i<=b;++i)
#define     itrange(i, a, b) for(auto i=a;i!=b;++i)
#define     rerange(i, a, b) for(auto i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define  fill(arr, tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
string in;
int num[500005],tail=0,ans=0;
void init() {
    cin>>in;
    itrange(iter,in.rbegin(),in.rend())num[++tail]=*iter-'0';
}

void ad(int &res,int i) {
    ans -= num[i];
    num[i] += res;
    res = num[i] / 10;
    num[i] %= 10;
    ans += num[i];
}

void big_mul() {
    int res = 0;
    range(i, 1, tail) {
        res = num[i] * 9 + res;
        num[i] = res % 10;
        ans += num[i];
        res /= 10;
    }
    ans += res;
    if (res)num[++tail] = res;
}

void big_add() {
    int res = 9;
    range(i, 1, tail) {
        ad(res, i);
        if (res == 0)break;
    }
    ans += res;
    if (res)num[++tail] = res;
}

void solve() {
    big_mul();
    range(i, 1, tail) {
        big_add();
        if (!(ans % 9) and i * 9 >= ans) {
            cout << i << '\n';
            return;
        }
    }
}

int main() {
    IOS;
    init();
    solve();
    return 0;
}

Measured memory_bytes: 2844
#include <chrono>

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <time.h>
#include <random>
#include <string>
#include <cassert>
#include <vector>
#include <ostream>
#include <istream>
#include <stack>
#include <deque>
#include <queue>
#include <functional>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

//ostream& operator << (ostream &a, const vector<int> &b) {
//    for (auto k : b) cout << k << " ";
//    return a;
//}

#ifdef LOCAL
    #define dbg(x) cout << #x << " : " << (x) << "\n";
//    const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 10;
//    const ld PI = 3.1415926535;
#else
    #define dbg(x)
//    const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 179;
//    const ld PI = 3.1415926535;
#endif

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("fast-math")
//#pragma GCC target("avx2")
//#pragma GCC optimize("section-anchors")
//#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
//#pragma GCC optimize("vpt")
//#pragma GCC optimize("rename-registers")
//#pragma GCC optimize("move-loop-invariants")
//#pragma GCC optimize("unswitch-loops")
//#pragma GCC optimize("function-sections")
//#pragma GCC optimize("data-sections")

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifdef LOCAL
    #else
//        freopen("capitals.in", "r", stdin);
//        freopen("capitals.out", "w", stdout);
    #endif
    int n;
    cin >> n;
    vector<set<int> > kek(30);
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
        kek[s[i] - 'a'].insert(i);
    }
    int q;
    cin >> q;
    int t, l, r;
    char nw;
    while (q--) {
        cin >> t;
        if (t == 1) {
            cin >> l >> nw;
            l--;
            kek[s[l] - 'a'].erase(l);
            kek[nw - 'a'].insert(l);
            s[l] = nw;
        } else {
            cin >> l >> r;
            l--, r--;
            int ans = 0;
            for (int i = 0; i < 30; i++) {
                if (kek[i].lower_bound(l) != kek[i].end() && *(kek[i].lower_bound(l)) <= r) {
                    ans++;
                }
            }
            cout << ans << "\n";
        }
    }
}
/*

*/


Measured memory_bytes: 26232
# coding:utf-8

import sys
from collections import deque, defaultdict
from operator import itemgetter

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


# def topological_sort(graph: list, indegree: list):
#     from collections import deque
#     que = deque(i for i in range(len(graph)) if indegree[i] == 0)
#     order = []
#     while que:
#         v = que.popleft()
#         order.append(v)
#         for next in graph[v]:
#             indegree[next] -= 1
#             if indegree[next] == 0:
#                 que.append(next)
#
#     return order


n, m = LI()

G = [[] for _ in range(n)]
indegree = [0] * n

for _ in range(n + m - 1):
    a, b = LI_()
    G[a].append(b)
    indegree[b] += 1

que = deque()
for i in range(n):
    if indegree[i] == 0:
        que.append(i)
        break

ans = [0] * n
while que:
    v = que.popleft()
    for v2 in G[v]:
        indegree[v2] -= 1
        if indegree[v2] == 0:
            que.append(v2)
            ans[v2] = v + 1

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


Measured memory_bytes: 25504
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n;
int a[maxn], d[maxn], used[maxn], ans[maxn];
set<int> s;
set<pair<int, int> > e;
vector<int> b;
void dfs(int p, int ban) {
	if(p == n + 1) {
		for(int i = 1; i <= n; ++i) {
			cout << ans[i] << ' ';
		}
		cout << '\n';
		exit(0);
	}
	for(int i = 0; i < b.size(); ++i) {
		if(!used[b[i]] && b[i] != ban) {
			used[b[i]] = 1;
			ans[p] = b[i];
			dfs(p + 1, a[b[i]]);
			used[b[i]] = 0;
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		++d[a[i]];
	}
	for(int i = 1; i <= n; ++i) {
		s.emplace(i);
	}
	for(int i = 1; i <= n; ++i) {
		e.emplace(-d[i], i);
	}
	int ban = 0;
	for(int i = 1; i <= n - 5; ++i) {
		int u;
		if(-e.begin() -> first == n - i) {
			u = e.begin() -> second;
		} else if(*s.begin() != ban) {
			u = *s.begin();
		} else {
			u = *next(s.begin());
		}
		s.erase(u);
		e.erase({-d[u], u});
		used[u] = 1;
		if(!used[a[u]]) {
			e.erase({-d[a[u]], a[u]});
			--d[a[u]];
			e.emplace(-d[a[u]], a[u]);
		}
		ban = a[u];
		ans[i] = u;
	}
	b.assign(s.begin(), s.end());
	dfs(max(1, n - 4), ban);
	cout << -1 << '\n';
	return 0;
}

Measured memory_bytes: 11776
#include <stdio.h>
#include <stdlib.h>
#define MAX 200000
int main(){
  int i,n,maxv,minv;
  int R[MAX];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&R[i]);
  }
  minv=R[0];
  maxv=R[1]-R[0];
  for(i=1;i<n;i++){
    if(maxv<R[i]-minv){
      maxv=R[i]-minv;
    }
    if(minv>R[i])minv=R[i];
  }
  printf("%d\n",maxv);
  return 0;
}



Measured memory_bytes: 2532
import java.util.*;

public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int x = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0; i < N; i++)
            a[i] =  sc.nextInt();
        sc.close();
        
        long count = 0;

        for(int i = 0; i < N-1; i++){
            int sum = a[i] + a[i+1];
            if(sum > x){
                count += sum-x;
                a[i+1] = Math.max(0, a[i+1]-(sum-x)); 
            }
        }

        System.out.println(count);
    }
}

Measured memory_bytes: 49696
/*{{{*/
//#define HOME
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define F first
#define S second

template<class T> void _R(T &_x) { cin >> _x; }
void _R(int &_x) { scanf("%d", &_x); }
void _R(int64_t &_x) { scanf("%" SCNd64, &_x); }
void _R(double &_x) { scanf("%lf", &_x); }
void _R(char &_x) { scanf(" %c", &_x); }
void _R(char *_x) { scanf("%s", _x); }
template<class T, class U> void _R(pair<T, U> &_x) {_R(_x.F); _R(_x.S);}
void R() {}
template<class T, class... U> void R(T &_head, U &... _tail) { _R(_head); R(_tail...); }

template<class T> void _W(const T &_x) { cout << _x; }
void _W(const int &_x) { printf("%d", _x); }
void _W(const int64_t &_x) { printf("%" PRId64, _x); }
void _W(const double &_x) { printf("%.16f", _x); }
void _W(const char &_x) { putchar(_x); }
void _W(const char *_x) { printf("%s", _x); }
template<class T, class U> void _W(const pair<T, U> &_x) {_W(_x.F); putchar(' '); _W(_x.S);}
template<class T> void _W(const vector<T> &_x) { for (auto i = _x.begin(); i != _x.end(); _W(*i++)) if (i != _x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &_head, const U &... _tail) { _W(_head); putchar(sizeof...(_tail) ? ' ' : '\n'); W(_tail...); }
template<class T, class... U> void DB(const T &_head, const U &... _tail) {
#ifdef HOME
    W('#', _head, _tail...);
#endif
}

#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define RAN(x,n) (x).begin(), (x).begin()+(n)
#define UNIQUE(x) {sort(ALL(x)); (x).erase(unique(ALL(x)), (x).end());}

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
//#define ITER(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define IT3(i,st,ed) for (auto i = st; i != ed; ++i)
#define IT2(i,range) IT3(i,range)

#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define MS(X, v) memset((X), (v), sizeof((X)))

template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

typedef int64_t ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

inline int LG (const unsigned int &_x) { return 31-__builtin_clz(_x); }  // floor: LG(x), ceil: LG(x-1)+1
/*}}}*/

const int MOD = 998244353;

int n;

/*
2 * C(n, k) * 2^(n-k), k = ceil(n/2) ... n
*/

int ADD(int a, int b){ return (a + b) % MOD; }
int MUL(int a, int b){ return (ll)a * b % MOD; }
int POW(int a, int p){
    int r = 1, t = a;
    for(; p; p >>= 1, t = MUL(t, t))
        if(p & 1) r = MUL(r, t);
    return r;
}
int INV(int a){ return POW(a, MOD-2); }

int main(){
    R(n);

    int ans = POW(3, n);
    int p2 = 1, c = 1;
    REPD(k, n, n/2+1){
        ans = ADD(ans, MOD - MUL(2 * p2, c));
        p2 = MUL(p2, 2);
        c = MUL(MUL(c, k), INV(n-k+1));
    }

    W(ans);

    return 0;
}


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

#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << e << ' '; cerr << '\n'; //vector, set
#define what_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << x[i] << ' '; cerr <<  '\n'; //n first element of array
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define st first
#define nd second
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << cerr_pair(e) << ' '; cerr << '\n'; //vector, set
#define pwhat_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << cerr_pair(x[i]) << ' '; cerr <<  '\n'; //n first element of array
#define endl '\n'
// #define int long long

typedef pair<int,int> pii;
const int N = 2e5+5;
const int INF = 1e9;
const int MOD = 1e9+7;

int n;
pii a[N];

int32_t main()
{
	IOS
	// freopen("input.txt", "r", stdin);
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> a[i].st >> a[i].nd;
	}
	sort(a, a+n, [](pii a, pii b){
		return a.nd < b.nd;
	});
	int time = 0;
	for(int i=0; i<n; i++){
		if (time + a[i].st > a[i].nd) return 0*puts("No");
		time += a[i].st;
	}
	puts("Yes");
	
	return 0;
}


Measured memory_bytes: 1792
#include <cassert>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

char buf[2][60];

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, 2) {
    scanf("%s", buf[i]);
  }

  ll res = 1;
  int last = 0;
  for(int i = 0; i < n; ) {
    if(buf[0][i] == buf[1][i]) {
      res = res * (3-last) % MOD;
      last = 1;
      i += 1;
    } else {
      switch(last) {
      case 0:
        res = res * 6 % MOD;
        break;
      case 1:
        res = res * 2 % MOD;
        break;
      case 2:
        res = res * 3 % MOD;
        break;
      default:
        assert(false);
      }
      last = 2;
      i += 2;
    }
  }
  
  printf("%lld\n", res);

  return 0;
}


Measured memory_bytes: 256
/*input
7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1
*/

#include <bits/stdc++.h>
#define ll long long

using namespace std;

void run() {
	int n, i, j;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(3));
	for(i=0; i<n; i++) {
		cin >> a[i][0] >> a[i][1] >> a[i][2];
	}
	vector<vector<int>> dp(n+1, vector<int>(3, 0));
	dp[0][0] = a[0][0];
	dp[0][1] = a[0][1];
	dp[0][2] = a[0][2];
	for(i=1; i<n; i++) {
		for(j=0; j<3; j++) {
			int other = (j + 1) % 3, other2 = (j + 2) % 3;
			dp[i][j] = max({dp[i][j], dp[i-1][other] + a[i][j], dp[i-1][other2] + a[i][j]});
		}
	}
	cout << max({dp[n-1][0], dp[n-1][1], dp[n-1][2]}) << endl;	
}

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


Measured memory_bytes: 11264
import java.util.*;
public class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int a = 0;
    int b = 0;
    int cnt = 1;
    while(cnt<=n){
      a = sc.nextInt();
      if(cnt!=a){
        b++;
      }
      cnt++;
    }    
    if(b==2||b==0){
      System.out.println("YES");
    }else{
      System.out.println("NO");
    }
    
  } 
}

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

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x,y) CPP_CAT_I(x,y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x,y) x ## y

using i8  = int8_t;
using u8  = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end) for(i64 i = (start), CPP_CAT(i,xxxx_end)=(end); i < CPP_CAT(i,xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for(i64 i = 1; i <= n; ++i)
#define RREP(i, n) for(i64 i = n - 1; i >= 0 ; --i)
#define RREPS(i, n) for(i64 i = n; i >= 1; --i)

#define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c))

#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define pb push_back
#define mp make_pair

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

template<typename C>
i64 SIZE(const C& c) { return static_cast<i64>(c.size()); }

template<typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }


struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;


int main() {
    i64 N; cin >> N;
    using P = pair<i64, i64>;
    vector<P> interval(N);
    REP(i, N) {
        i64 x, l; cin >> x >> l;
        interval[i].first = x - l;
        interval[i].second = x + l;
    }

    sort(interval.begin(), interval.end(), [](const P &p1, const P &p2) {
        return p1.second < p2.second;
    });

    i64 cnt = 0;
    i64 last = -1e10;
    REP(i, N) {
        if (interval[i].first < last) continue;
        cnt++;
        last = interval[i].second;
    }
    cout << cnt << endl;

    return 0;
}

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

using namespace std;

int main(){
    long long N, X; cin >> N >> X;
    vector<long long> b(N), l(N), u(N);
    for(int i=0;i<N;i++) cin >> b[i] >> l[i] >> u[i];
    vector<pair<long long, int>> vp;
    for(int i=0;i<N;i++) vp.emplace_back((X-b[i])*u[i] + b[i]*l[i], i);
    sort(vp.rbegin(), vp.rend());
    long long thr = 0;
    for(int i=0;i<N;i++) thr += b[i] * l[i];
    long long L = -1, R = N*X;
    while(R-L > 1){
        long long mid = (L+R)/2;
        long long sum = 0;
        int div = mid/X;
        for(int i=0;i<div;i++) sum += vp[i].first;
        long long cur = 0;
        long long m = mid%X;
        for(int i=0;i<N;i++){
            int idx = vp[i].second;
            long long add = min(m, b[idx]) * l[idx] + max(0LL, m-b[idx]) * u[idx];
            if(i < div) cur = max(cur, sum - vp[i].first + vp[div].first + add);
            else cur = max(cur, sum + add);
        }
        if(cur >= thr) R = mid;
        else L = mid;
    }
    cout << R << endl;
}

Measured memory_bytes: 4724
#include <bits/stdc++.h>
#include <string>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <math.h>
using namespace std;
 
int main(){
    
    int H,W,h,w;
    cin >>H>>W>>h>>w;

    cout << (H*W) - ((h*W)+(w*H)-h*w);

    return 0;

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> a(n);
  for(int i=0;i<n;i++)
    cin>>a.at(i);
  a.push_back(0);
  int mi=a.at(0);
  int ma=a.at(0);
  int64_t s=1000;
  for(int i=1;i<=n;i++){
    if(a.at(i-1)>a.at(i)){
      s+=(ma-mi)*(s/mi);
      mi=a.at(i);
      ma=a.at(i);
    }else
      ma=a.at(i);
  }
  cout<<s<<endl;
}

Measured memory_bytes: 3628
# inverse x^(-1)
def inv(x):
    global mod
    return pow(x, mod - 2, mod)

# factorial x!
def fact(x):
    global mod
    res=1
    for i in range(2 , x + 1):
        res=res * i % mod
    return res

# combination nCr
def combi(n,r):
    if r < 0 or r > n:
        return 0
    else:
        return fact(n) * inv(fact(r)) * inv(fact(n - r)) % mod

mod = 10 ** 9 + 7
N, M, K = map(int,input().split())
x = (N - 1) * N * (N + 1) * inv(6) % mod
x = x * M * M % mod
y = (M - 1) * M * (M + 1) * inv(6) % mod
y = y * N * N % mod
z = combi(M * N - 2, K - 2)
ans = (x + y) * z % mod
print(ans)

Measured memory_bytes: 3064
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#include <assert.h>
#include <bitset>
#define Endl endl
#define mp make_pair
#define ll long long 
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define ceil(a,b) ((a-1)/b+1)
#define quickcin ios_base::sync_with_stdio(false);
const int mod=1e9+7;
const int inf=1039074182;
const ll llinf=1LL*inf*inf;
using namespace std;
template <typename T1,typename T2> inline void chmin(T1 &x,T2 b) {if(b<x) x=b;}
template <typename T1,typename T2> inline void chmax(T1 &x,T2 b) {if(b>x) x=b;}
template <typename T1,typename T2> inline void chadd(T1 &x,T2 b) {x+=b;if(x>=mod) x-=mod;}
template <typename T1,typename T2> inline void chmul(T1 &x,T2 b) {x=1LL*x*b%mod;}
template <typename T1,typename T2> inline void chmod(T1 &x,T2 b) {x%=b,x+=b;if(x>=b) x-=b;}
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
int n;
int f[505][505];
bool rem[505][505];

inline bool out(int x,int y)
{
	return (x<0 || y<0 || x>=n || y>=n);
}

void update(int x,int y,int value)
{
	int nx,ny,cost;
	for(int k=0;k<4;k++)
	{
		nx=x+dx[k];
		ny=y+dy[k];
		if(out(nx,ny)) continue;
		cost=value+(rem[x][y]);
		if(f[nx][ny]<=cost) continue;
		f[nx][ny]=cost;
		update(nx,ny,cost);
	}
}

int main()
{
//	freopen("input.txt","r",stdin);
	quickcin;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			f[i][j]=min({i,j,n-1-i,n-1-j});
		}
	}
	memset(rem,true,sizeof(rem));
	int res=0;
	for(int q=0;q<n*n;q++)
	{
		int id;
		cin>>id;
		id--;
		int x=id/n,y=id%n;
		chadd(res,f[x][y]);
//		cout<<f[x][y]<<endl;
		rem[x][y]=false;
		update(x,y,f[x][y]);
//		for(int i=0;i<n;i++)
//		{
//			for(int j=0;j<n;j++)
//			{
//				cout<<f[i][j]<<' ';
//			}
//			cout<<endl;
//		}
	}
	cout<<res<<endl;
	return 0;
}

Measured memory_bytes: 24172