akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <queue>
#include <set>
#include <list>
#include <deque>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <iterator>
#define ll long long
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define powr(x,n,p) for(int axy=0 ; axy<p ; axy++){x=x*n;}
#define YES printf("YES\n")
#define Yes printf("Yes\n")
#define yes printf("yes\n")
#define NO printf("NO\n")
#define No printf("No\n")
#define no printf("no\n")
#define nl printf("\n")
#define fr0(n,q) for(q=0 ; q<n ; q++)
#define fr1(n,q) for(q=1 ; q<=n ; q++)
#define mp make_pair
#define scl(x) scanf("%lld",&x)
#define sci(x) scanf("%d",&x)
#define lst(x) x[x.size()-1]
#define llst(x) x[x.size()-2]
#define md 1000000007
#define check(x,y) (x&(1<<y))
#define set(x,y) x=x|(1<<y)
using namespace std;
string arr[3005];
int dp[3005][3005];
int h,w;
int solve(int i, int j){
	if(i==h-1 && j==w-1) return 1;
	if(dp[i][j]!=-1) return dp[i][j];
	int ret=0;
	if(i+1<h && arr[i+1][j]=='.') ret+=solve(i+1,j);
	if(j+1<w && arr[i][j+1]=='.') ret+=solve(i,j+1);
	return dp[i][j]=(ret%md);
}
int main()
{
	cin>>h>>w;
	for(int i=0 ; i<3005 ; i++){
		for(int j=0 ; j<3005 ; j++) dp[i][j]=-1;
	}
	for(int i=0 ; i<h ; i++) cin>>arr[i];
	cout<<solve(0,0);
	nl;
	return 0;
}


Measured memory_bytes: 40996
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    c = list(SS())

    # R...RW...Wにしたい
    # WRWWRWRR
    # RRWWRWRW
    # RRRWRWWW
    # RRRRWWWW
    cnt_r = c.count('R')
    ans = max(c[:cnt_r].count('W'), c[cnt_r:].count('R'))
    # print(c[:cnt_r], c[cnt_r:])
    print(ans)

if __name__ == '__main__':
    resolve()


Measured memory_bytes: 12880
#include <stdio.h>

#define MOD_BY 10007

int N;
char sekinin[1024];

int memo[8][1000];

int tansaku(int key,int day) {
	int result=0;
	int i;
	if(day>=N)return 1;
	if(memo[key][day]>0)return memo[key][day]-1;
	for(i=0;i<8;i++) {
		int j,valid=1;
		for(j=0;j<3;j++) {
			if(sekinin[day]=="JOI"[j] && !(i&(1<<j)))valid=0;
		}
		if(valid && (i&key)) {
			result+=tansaku(i,day+1);
		}
	}
	result%=MOD_BY;
	memo[key][day]=result+1;
	return result; 
}

int main(void) {
	scanf("%d",&N);
	scanf("%s",sekinin);
	printf("%d\n",tansaku(1,0));
	return 0;
}

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

void textIO()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
}


int main()
{
    //textIO();
    fastIO();

    string s, prefix, suffix;
    long long q, t, f;
    char c;
    cin >> s;
    cin >> q;
    bool rev = false;
    while(q--){
        cin >> t;
        if(t == 1){
            rev = !rev;
        }
        else{
            cin >> f >> c;
            if(rev){
                if(f == 1){
                    suffix+=c;
                }
                else{
                    prefix+=c;
                }
            }
            else{
                if(f == 1){
                    prefix+=c;
                }
                else{
                    suffix+=c;
                }
            }
        }
    }
    if(rev){
        reverse(suffix.begin(), suffix.end());
        reverse(s.begin(), s.end());
        cout << suffix << s << prefix << '\n';
    }
    else{
        reverse(prefix.begin(), prefix.end());
        cout << prefix << s  << suffix << '\n';
    }


    return 0;
}


Measured memory_bytes: 1040
from sys import stdin, stdout
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))
def readLine_int_set(): return set(list(map(int, stdin.readline().split())))
def g_twoD_list(p,q): return [[0 for i in range(p)] for j in range(q)]


def main():
    n,k,x,y = readAll_int()
    print(k*x + (n-k)*y if n-k > 0 else n*x)
    
if __name__ == "__main__":
    main()


Measured memory_bytes: 3064
import java.util.*;
 
public class Main {
	
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int N = sc.nextInt();
    	int M = sc.nextInt();
    	int C = sc.nextInt();
    	int[] B = new int[M];
    	for (int i = 0; i < M; i++) {
    		B[i] = sc.nextInt();
    	}
    	List<int[]> Ai = new ArrayList<>();
    	for (int i = 0; i < N; i++) {
    		int[] A = new int[M];
    		for (int j = 0; j < M; j++) {
    			A[j] = sc.nextInt();
    		}
    		Ai.add(A);
    	}
    	int count = 0;
    	for (int i = 0; i < Ai.size(); i++) {
    		int[] A = Ai.get(i);
    		int clear = C;
    		for (int j = 0; j < M; j++) {
    			clear += A[j] * B[j];
    		}
    		if (0 < clear) {
    			count++;
    		}
    	}
    	System.out.println(count);
    }
}

Measured memory_bytes: 22228
import java.util.*;

public class Main {              
    
    public static void main(String[] args){           
        
         Scanner sc = new Scanner(System.in);
                     
         int N = sc.nextInt();
         
         int a[] = new int[N];
         
         long sum = 0;
         
         for(int i = 0;i < N;i++){
             a[i] = sc.nextInt();
             sum += a[i];
         }
         
         long left = a[0],right = sum - a[0];
         long min = Math.abs(left - right);
         
         for(int i = 1;i < N-1;i++){
             left  += a[i];
             right -= a[i];
             if(Math.abs(left - right) < min){
                 min = Math.abs(left - right);
             }
         }
         
         System.out.println(min);
         
  }        
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, int> PILL;
typedef pair<ll, ll> PLL;

const int MAX_N = 1000111;

const ll MOD = 1000000007LL;

void solve() {

}

int n, D;

int countDist(vector<int> p1, vector<int> p2) {
    double ret = 0;
    for (int i=0; i<D; i++) {
        ret += (p1[i]-p2[i]) * (p1[i]-p2[i]);
    }
    return ret;
}

bool check(int x) {
    int z = 0;
    while (z*z < x) z++;

    return z*z == x;
}

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


    cin >> n >> D;
    vector<vector<int>> points(n);
    vector<int> dist;

    for (int i=0; i<n; i++) {
        for (int j=0; j<D; j++) {
            int x;
            cin >> x;
            points[i].push_back(x);
        }
    }

    for (int i=0; i<n; i++) {
        for (int j=i+1; j<n; j++) {
            dist.push_back(countDist(points[i], points[j]));
        }
    }

    int ans = 0;
    for (int i=0; i<dist.size(); i++) {
        if (check(dist[i])) ans++;
    }

    cout << ans;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
//#include<cmath>
//#include<cstring>
//#include<vector>
#include<bits/stdc++.h>
//#include<algorithm>

using namespace std;
int main(){
  int x,c=0,i;
  string a,b;
  cin>>a>>b;
  x=a.size();
  for(i=0;i<x;i++){
    if(a[i]!=b[i]){

        c++;
    }
  }

  cout<<c;



  return 0;
}

/*---------
 double n,m,a;
    cin>>n>>m>>a;
    cout<<(ceil(n/a)*ceil(m/a));

*/




/* int n;
  cin >> n;_.-
  for (int y=0; y<2*n+1; y++) {
    int c = 0;
    for (int x=0; x<2*n+1; x++) {
      int nx = x;
      int ny = y;
      if (nx>n) {
        nx -= (nx-n)*2;
      }
      if (ny>n) {
        ny -= (ny-n)*2;
      }
      int p = nx+ny-n;
      if (p>=0) {
        cout << p;
        c++;
        if (c >= 2*ny+1) {
          break;
        } else {
          cout << " ";
        }
      } else {
        cout << "  ";
      }
    }
    cout << endl;
  }
  return 0;
}*/
 /*----------my code--------------
  int n,a,b,c,d,space,i,j,apply;
    cin>>n;
    b=2*n+1;
    for(i=0;i<b;i++){
        space=2(n-1);
        for(j=0;j<b;j++){
            if(space>0){
                apply=space/2;
                for()
            }
        }

        int n,a,b,c,d,space,i,j,k,apply,index;
    cin>>n;
    b=2*n+1;
    for(i=0;i<b;i++){
        space=2(n-i);
        apply=space/2;
        for(j=0,k=0;j<b;j++,k++){
            if(space>1){
                    if(k<i){
                            index=apply+1;
                a[index]=k;
            index++;
            }
            if(k>=i){
                a[]
            }
            }
            }
        }
    }

   for
    }*/
    /*--------------------

    int main() {
	int n;
	cin>>n;
	int x[n],y[n];
	for(int i=0;i<n;i++)
	{
		cin>>x[i]>>y[i];
	}
	int u=0,d=0,l=0,r=0,ans=0;
	for(int i=0;i<n;i++)
	{
		u=0;d=0;l=0;r=0;
		for(int j=0;j<n;j++)
		{
			if(x[i]==x[j]&&y[i]>y[j])d=1;
			if(x[i]==x[j]&&y[i]<y[j])u=1;
			if(y[i]==y[j]&&x[i]>x[j])l=1;
			if(y[i]==y[j]&&x[i]<x[j])r=1;
		}
		if(d&&u&&l&&r)ans++;
	}
	cout<<ans<<endl;
}-----------*/


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

int main(){
    string S, T;
    cin >> S >> T;
    int smallest = INT_MAX;
    for (int i = 0; i < S.length() - T.length() + 1; ++i){
        int unmatched = 0;
        for (int j = 0; j < T.size(); ++j){
            if (T[j] != S[j + i]) ++unmatched;
        }
        smallest = min(smallest, unmatched);
    }
    cout << smallest << "\n";
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e;
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>a>>b;
	if(a%b!=0) cout<<a; else cout<<-1;
	return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
par = [-1]*n
wei = [0]*n

def find(x):
  if par[x]<0:
    return x
  else:
    px=find(par[x])
    wei[x]+=wei[par[x]]
    par[x]=px
    return px

def weight(x):
  find(x)
  return wei[x]


def unite(x,y,w):
  w+=wei[x]-wei[y]
  x=find(x)
  y=find(y)    
  if x==y:
    return False
  else:
    if par[x]>par[y]:
      x,y=y,x
      w=-w
    par[x]+=par[y]
    par[y]=x
    wei[y]=w
    return True

def same(x,y):
    return find(x)==find(y)

def size(x):
    return -par[find(x)]

def diff(x,y):
    return weight(y)-weight(x)
flag=1
for i in range(m):
  l,r,d=map(int,input().split())
  if same(l-1,r-1):
    if d!=diff(l-1,r-1):
      flag=0
  else:
    unite(l-1,r-1,d)
print("No" if flag==0 else "Yes")

Measured memory_bytes: 8996
#include <algorithm>
//#include <cstdio>
//#include <functional>
//#include <bits/stdc++.h>
#include <iostream>
//#include <cfloat>
//#include <climits>
//#include <cstdlib>
//#include <cstring>
//#include <cmath>
//#include <map>
//#include <queue>
//#include <set>
//#include <sstream>
//#include <stack>
#include <string>
//#include <time.h>
#include <vector>
//#include <random>
//#include <unordered_set>
//#include <complex>
#define rt0 return 0
using namespace std;
 
int main()
{
	int n,i,x,y;

	cin>>n;
	vector <int> A(n);
	for(i=0; i<n; i++) 	{
		cin>>A[i];
	}
	sort(A.begin(),A.end());
	x=A[n-1];
	y=A[0];
	
	for(i=1; i<n-1; i++){
		if(abs(A[i]-x/2) <= abs(y-x/2)) y=A[i];
	}		
		
	cout<<x<<" "<<y;
    rt0;
}


Measured memory_bytes: 640
#!/usr/bin/env python3
N, K = [int(str) for str in input().strip().split()]
x = [int(str) for str in input().strip().split()]

sums = [0]
for i in x:
    sums.append(sums[-1] + (i + 1) / 2)

ans = 0
for i in range(N - K + 1):
    ans = max(sums[i + K] - sums[i], ans)

print(ans)

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

#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define MIN(c) (*min_element(ALL(c))) 
#define MAX(c) (*max_element(ALL(c)))
#define ACC(c) (accumulate(ALL(c), 0))
#define SLL(x, n) ((x) << (n))
#define SRL(x, n) ((x) >> (n))
#define MIN2(a, b) ((a) < (b)) ? (a) : (b)
#define MAX2(a, b) ((a) > (b)) ? (a) : (b)
#define MP(a, b) (make_pair((a), (b)))
#define ENDL "\n"
#define PRINT(x) cout << (x) << ENDL

template<typename T> inline void fill(T array[], T value) {for(T t: array) t = value;}
inline int in(int& a) {return scanf("%d", &a);}
inline void out(int n) {printf("%d\n", n);}

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

signed main(){
    ll r, D, x;
    cin >> r >> D >> x;
    REP(i, 10){
        x = r * x - D;
        PRINT(x);
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <climits>
#include <utility>

#define PRINT(STR) cout << STR << endl;
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define SPACE " "

typedef long long ll;

using namespace std;

int nextInt() { int n = -1; cin >> n; return n; }
double nextDouble() { double d = -1; cin >> d; return d; }
ll nextll() { ll n = -1; cin >> n; return n; }
string nextString() { string str; cin >> str; return str; }

int INF = 1LL << 25;
double EPS = 1e-9;

// BEGIN //////////////////////////////////////////////////////////////
int main() {
	map<string, int> d;
	string str;
	while (cin >> str) {
		d[str]++;
	}
	int d_max = -1;
	string ans = "";
	for (auto it = d.begin(); it != d.end(); it++) {
		d_max = max(d_max, it->second);
		if (ans.size() < it->first.size()) {
			ans.assign(it->first);
		}
	}
	for (auto it = d.begin(); it != d.end(); it++) {
		if (it->second == d_max) {
			cout << it->first << SPACE;
			break;
		}
	}
	PRINT(ans);

	return 0;
}
// END   //////////////////////////////////////////////////////////////

Measured memory_bytes: 3192
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
#define INF (1<<25)
int X,Y;
int sx,sy,gx,gy;

int x[5][1000];
int y[5][1000];
int memo[1000][5][5];
int sz[5];

int f(int w,int z,int s){
	if((z+1)%5==s) return abs(x[z][w]-gx)+abs(y[z][w]-gy);
	if(~memo[w][z][s]) return memo[w][z][s];
	int ans = INF;
	for(int i = 0 ; i < sz[(z+1)%5] ; i++){
		ans = min(ans,f(i,(z+1)%5,s)+abs(x[z][w]-x[(z+1)%5][i])+abs(y[z][w]-y[(z+1)%5][i]));
	}
	return memo[w][z][s] = ans;
}
int main(){
	while(cin >> X >> Y && X){
		memset(memo,-1,sizeof(memo));
		for(int i = 0 ; i < 5 ; i++) sz[i]=0;
		
		for(int i = 0 ; i < Y ; i++)
			for(int j = 0 ; j < X ; j++){
				char c; scanf(" %c",&c);
				if(c == 'S'){
					sx = j , sy = i;
				}else if( c == 'G'){
					gx = j , gy = i;
				}else if( c != '.'){
					c = c - '1';
					x[c][sz[c]] = j;
					y[c][sz[c]++] = i;			
				}
			}
		pair<int,int> ans(INF,-1);
		for(int i = 0 ; i < 5 ; i++){
			for(int j = 0 ; j < sz[(i+1)%5] ; j++){
				ans = min(ans,make_pair(abs(sx-x[(i+1)%5][j])+abs(sy-y[(i+1)%5][j])+f(j,(i+1)%5,i),i));
			}
		}
		if(~ans.second)cout << ans.second+1 << " " << ans.first << endl;
		else cout << "NA" << endl;
	}
		
}

Measured memory_bytes: 0
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [list(input()) for _ in range(n)]
#a = [int(input()) for _ in range(n)]

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

if (n%k==0):
    print(0)
else:
    print(1)



Measured memory_bytes: 9088
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int main() {
	int a, b, c, d, e;
	cin >> a >> b >> c >> d >> e;
	b += a * 60;
	d += c * 60;
	cout << d - b - e << endl;
	return 0;
}


Measured memory_bytes: 3624
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#ifdef _DEBUG
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
#else
#define debug(x)
#endif
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr double PI = 3.141592653589793238462643383279;
ll getDigit(ll x) {
  return x == 0 ? 1 : log10(x) + 1;
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

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

template <typename T>
void debugV(const vector<T> v) {
#ifdef _DEBUG
  rep(i, v.size()) {
    cout << i << ":" << v[i] << " ";
  }
  cout << endl;
#else
  (void)v;
#endif
}

vll a(100), p(100);

ll burger(ll n, ll x) {
  if (n == 0) {
    return x <= 0 ? 0 : 1;
  }

  if (x == 1) {
    return 0;
  }
  if (1 < x && x < 2 + a[n - 1]) {
    return burger(n - 1, x - 1);
  }
  if (x == 2 + a[n - 1]) {
    return p[n - 1] + 1;
  }
  if (2 + a[n - 1] < x && x < a[n]) {
    return p[n - 1] + 1 + burger(n - 1, x - a[n - 1] - 2);
  }
  if (x == a[n]) {
    return p[n];
  }
  return 0;
}

signed main() {
  ll N, X;
  cin >> N >> X;

  rep(i, 100) {
    if (i == 0) {
      a[i] = 1;
      p[i] = 1;
      continue;
    }
    a[i] = 2 * a[i - 1] + 3;
    p[i] = 2 * p[i - 1] + 1;
  }

  ll b = burger(N, X);
  COUT(b);
  return 0;
}

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

int main(void){
  long long a,b,n; cin>>a>>b>>n;
  long long res;
  if(n/b==0) res=a*n/b-a*(n/b);
  else res=a*(n/b*b-1)/b-a*((n/b*b-1)/b);
  cout<<res<<endl;
  return 0;
}


Measured memory_bytes: 3628
N,M=map(int,input().split())
n=[]
m=[]
for i in range(N):
    n.append(list(map(int,input().split())))
for l in range(M):
    m.append(list(map(int,input().split())))
L=[]
for i in n:
    M=[]
    for l in m:
        A=abs(i[0]-l[0])+abs(i[1]-l[1])
        M.append(A)
    print(M.index(min(M))+1)
    
# seek help from past solution

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

using namespace std;

int main () {

    int L;
    int R;

    cin >> L >> R;
    int Range = R - L;
    if (Range >= 2018) {
        cout << 0 << endl;
        return 0;
    }

    else {
        int S = L / 2019;
        int l = L - S * 2019;
        int r = R - S * 2019;
        if ((l <= 2019) && (r >= 2019)) {
            cout << 0 << endl;
            return 0;
        }
        else {

            int res1 = 2020 * 2021;
            for (size_t i = l; i <= r - 1; ++i) {
                for (size_t j = i + 1; j <= r; ++j) {
                    int k = i*j;
                    int res = k % 2019;
                    if (res <= res1) res1 = res;
                }
            }

            cout << res1 << endl;
            return 0;
        }

        return 0;
    }
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_map>
#include <map>
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
using ll = long long int;

int main(){
    int N;
    cin >> N;

    ll dp[100001];

    int i;

    for(i = 0; i < 100001; i++){
        dp[i] = 1000000;
    }

    ll v[] = {1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049};
    dp[0] = 0;
    for(i = 1; i <= N; i++){
        for(auto n : v){
            if(i - n >= 0){
                dp[i] = min(dp[i], dp[i-n]);
            }
        }
        dp[i] += 1;
    }

    cout << dp[N] << endl;
 }


Measured memory_bytes: 1024
#include <map>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;
typedef long long int LL;
const int Max_N(100050);

namespace io
{
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush (){fwrite (obuf, 1, oS - obuf, stdout);oS = obuf;}
	inline void putc (char x){*oS ++ = x;if (oS == oT) flush ();}
	template <class I>
	inline void gi (I &x) {for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;}
	template <class I>
	inline void print (I x){
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;while(x) qu[++ qr] = x % 10 + '0',  x /= 10;while (qr) putc (qu[qr--]);}
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::gi;
using io::putc;
using io::print;

struct node
{
	int u;
	LL d;
};
int N, Head[Max_N], Next[Max_N], Size[Max_N];
node V[Max_N];
map<LL, int> S;

inline void Add_Edge(int s, int t)
{
	Next[t] = Head[s], Head[s] = t;
}

inline bool comp(const node &a, const node &b)
{
	return a.d > b.d;
}

LL Sum;
void dfs(int u, int dep)
{
	Sum += dep;
	for (int v = Head[u];v;v = Next[v])
		dfs(v, dep + 1);
}

void dfs2(int u)
{
	if (S[Sum] != u)
	{
		print(-1);
		exit(0);
	}
	for (int v = Head[u];v;v = Next[v])
		Sum += (N - Size[v]) - Size[v], dfs2(v), Sum -= (N - Size[v]) - Size[v];
}

int main()
{
	gi(N);
	for (int i = 1;i <= N;++i)
		gi(V[i].d), S[V[i].d] = V[i].u = i, Size[i] = 1;
	sort(V + 1, V + 1 + N, comp);
	for (int i = 1, u, fa;i < N;++i)
	{
		u = V[i].u;
		LL d = V[i].d - (N - Size[u]) + Size[u];
		if (d >= V[i].d || !(fa = S[d]))
		{
			print(-1);
			return 0;
		}
		Add_Edge(fa, u), Size[fa] += Size[u];
	}
	dfs(V[N].u, 0);
	dfs2(V[N].u);
	for (int u = 1;u <= N;++u)
		for (int v = Head[u];v;v = Next[v])
			print(u), putc(' '), print(v), putc('\n');
	return 0;
}

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

// template {{{  0 
// using {{{ 1
using ll = long long int;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x,y) scanf("%d%d", &x, &y)
#define Scd3(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x,y) scanf("%lld%lld", &x, &y)
#define Scll3(x,y,z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x,y,z) for(ll x = z; x < y; x++)
#define Rep(x,y) Repe(x,y,0)
#define RRepe(x,y,z) for(ll x = y-z-1; x >= 0; x--)
#define RRep(x,y) RRepe(x,y,0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a)?printf("Yes\n"):printf("No\n")
#define YESNO(a) (a)?printf("YES\n"):printf("NO\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

int main() {

    int N;
    Scd(N);
    char s[501][501];
    Rep(i,N) Scs(s[i]);

    int ans = 0;
    vi clb(N,0); // |
    Rep(i,N) Rep(j,N) clb[i] += s[j][i]=='#';

    vi rwb(N,0); // -
    Rep(i,N) Rep(j,N) rwb[i] += s[i][j]=='#';

    // tatenuri
    Rep(i,N) if( clb[i] != N ) ans++;

    // yokoume
    int yoko = INF;
    int cnt = 0;
    Rep(i,N) if( clb[i] ){
        yoko = min( yoko, N-rwb[i] );
    }else{
        cnt++;
        yoko = min( yoko, N-rwb[i]+1 );
    }

    if( cnt == N ) yoko = -N-1;
    printf ("%d\n", ans+yoko );

    return 0;
}



Measured memory_bytes: 512
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 1000000010;

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

    int h[n];
    rep(i, n) {
        cin >> h[i];
    }

    int dp[n+1];
    rep(i, n+1) {
        dp[i] = INF;
    }

    dp[0] = 0;
    dp[1] = 0;
    for(int i = 1; i < n; i++) {
        rep(j, k) {
            if(i <= j) {
                break;
            }
            dp[i+1] = min( dp[i+1], dp[i-j] + abs(h[i-(j+1)] - h[i]) );
        }
    }

    cout << dp[n] << endl;
    return 0;
}

Measured memory_bytes: 1024
//高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INF 1999999999
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007 

int main() {
	int ans = 0;
	int tmp;
	string s;
	cin >> s;
	for(int i= 0; i < s.size()-1-i; i++){
		if(s[i] != s[s.size()-i-1]) ans ++;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,s,f) for(i=s;i<f;i++)
#define print(v) for(auto &z:v) cout<<z<<' ';cout<<'\n'
#define db cout<<"db: "
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B,v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int,int>>
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define clk1 clock_t start_time=clock()
#define clk2 cout<<(clock()-start_time)/(double)CLOCKS_PER_SEC
#define clean(arr) memset(arr,0,sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)

int main()
{
    int i,j,t;
    boost;

    ll n,x,m;
    cin>>n>>x>>m;
    map<ll,pair<ll,ll>> mp;
    mp[x]={0,0};

    ll sum=x;
    ll ans=0;
    ll len=1;
    ll clen=-1;
    ll cycle=-1;

    rep(i,2,n+1)
    {
        x= (x*x)%m;
        if(mp.count(x))
        {
            clen = len-mp[x].S;
            cycle=sum-mp[x].F;
            break;
        }

        mp[x]={sum,len};
        len++;
        sum+=x;
    }

    if(cycle==-1)
    {
        cout<<sum;return 0;
    }

    ans = sum-cycle;
    ll rem = n-(len-clen);
    ll cnt = rem/clen;
    ans+= cnt*cycle;
    rem-= cnt*clen;
    while(rem--)
    {
        ans+=x;
        x=(x*x)%m;
    }

    cout<<ans;

    return 0;
}

Measured memory_bytes: 6680
#include <iostream>

using namespace std;

const int MOD = 1e9 + 7;

int main() {
	int H, W;
	cin >> H >> W;

	static int isWall[1000][1000];
	for(int y = 0; y < H; y++) {
		for(int x = 0; x < W; x++) {
			char a;
			cin >> a;
			isWall[y][x] = (a == '#');
		}
	}

	static int result[1000][1000];
	for(int y = 0; y < H; y++) {
		for(int x = 0; x < W; x++) {
			if(isWall[y][x]) {
				result[y][x] = 0;
				continue;
			} else if(y == 0 && x == 0) {
				result[y][x] = 1;
			} else if(y == 0) {
				result[y][x] = result[y][x - 1];
			} else if(x == 0) {
				result[y][x] = result[y - 1][x];
			} else {
				result[y][x] = (result[y][x - 1] + result[y - 1][x]) % MOD;
			}
		}
	}

	cout << result[H - 1][W - 1] << endl;

	return 0;
}


Measured memory_bytes: 11440
'use strict'

function Main(input) {
  const N = Number(input.split('\n')[0]);
  const S = input.split('\n')[1].split('');

  let S_left, S_right;
  let change = 0;
  let changemin = 0;

  for(let i=0; i<N; i++){
    if(S[i+1] === 'E'){
      change++;
    }
  }
  changemin = change;

  for(let j=1; j<N; j++){
    if(S[j-1] === 'W'){
      change++;
    }
    if(S[j] === 'E'){
      change--;
    }
    if(changemin > change) {
      changemin = change;
    }
  }

/*
  for(let i=0; i<N; i++){
    S_left = S.slice(0,i);
    S_right = S.slice(i+1);
    

    for(let j=0; j<S_left.length; j++){
      if(S_left[j] === 'W'){
        change++;
      }
    }
    for(let k=0; k<S_right.length; k++){
      if(S_right[k] === 'E'){
        change++;
      }
    }
    changelist.push(change);
    change = 0;
  }
*/
    console.log(changemin);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());


Measured memory_bytes: 11084
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A, B;
  string S;
  cin >> A >> B;
  int counter = 0;
  
  for (int i = A; i <= B; i++)
  {
  	S = to_string(i);
    int len = (int)(S.length());
    
    for (int j = 0; j < len; j++)
    {
    	if (S[j] != S[len - j -1]) break;
      	if (j == len -1) counter++;
    }
  }
  
  cout << counter << endl;
  
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <map>
#include <vector>

using namespace std;
int main()
{
    map<int, pair<int,int>> m;
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            int t;
            cin >> t;
            m[t] = {i,j};
        }
    }
    int n;
    cin >> n;
    int d[3][3] = {};
    while(n--){
        int t;
        cin >> t;
        if(m.count(t) > 0){
            auto l = m[t];
            d[l.first][l.second] = 1;
            // cout << l.first << "," << l.second << endl;
        }
    }
    
    for(int i=0; i<3; i++){
        if(d[i][0] + d[i][1] + d[i][2] == 3){
            cout << "Yes" << endl;
            return 0;
        }
    }
    for(int i=0; i<3; i++){
        if(d[0][i] + d[1][i] + d[2][i] == 3){
            cout << "Yes" << endl;
            return 0;
        }
    }
    if(d[0][0] + d[1][1] + d[2][2] == 3 || d[0][2] + d[1][1] + d[2][0] == 3){
        cout << "Yes" << endl;
        return 0;
    }
    
    cout << "No" << endl;
    return 0;
}


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

int main() 
{
  int X , Y, S;
  cin >> X >> Y;
  int j = 0;
  for (int i = 0; i < 2; i++) 
  {
    if( X >= Y)
    {
      S = S + X;
      X = X-1;
    }
    else
    {
      S = S + Y;
      Y = Y - 1;
    }
  }
  cout << S <<endl;
 
}

Measured memory_bytes: 256
#C
p = 10**9+7

N=int(input())
A=list(map(int,input().split()))
B=[A[0]]
for i in range(1,N):
    B.append(B[-1]+A[i])
#print(B)

ans=0
for i in range(N-1):
    tmp= (A[i]*(B[-1]-B[i]))%p
    ans = (ans+tmp)%p
print(ans)

Measured memory_bytes: 31620
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<queue>
#include<utility>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
	int m,d;
	cin>>m>>d;
	int ans=0;
	if(d<22||m<4) {
		cout<<0;
		return 0;
	}
	for(int i=1;i<=m;i++){
		for(int j=22;j<=d;j++){
			int a=j%10;
			int b=(j-a)/10;
			if(a*b==i&&a>=2&&b>=2){
				ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}

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

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;
#define MAX_N 2010
#define MAX_2N 4010

int N;
char C[MAX_2N] = {};
int A[MAX_2N] = {};

// 1-indexed であることに注意
template<typename T>
class BITree {
    public:
        BITree(int n) : m_N(n + 1) { m_Tree.assign(m_N, 0); }
        // [1, i] の合計値を返す (log m_N)
        // 閉区間であることに注意
        T Sum(int i) {
            if(i < 1 || i >= m_N) { return 0; }
            T s = 0;
            while(i > 0) { s += m_Tree[i]; i -= i & -i; }
            return s;
        }
        // [i, j] の合計値を返す (log m_N)
        // i < j
        // 閉区間であることに注意
        T Sum(int i, int j) {
            if(i > j) { return 0; }
            return Sum(j) - Sum(i-1);
        }
        // i 番目の要素(1-indexed)に x を足して更新 O(log m_N)
        void Add(int i, T x){ while(i < m_N) { m_Tree[i] += x; i += i & -i; } }
    private:
        int m_N;            // 要素数
        vector<T> m_Tree;   // BIT 本体
};

int dp[MAX_N][MAX_N] = {};
int costB[MAX_N][MAX_N] = {};
int costW[MAX_N][MAX_N] = {};
int posB[MAX_2N] = {};      // B の i がある A のインデックス
int posW[MAX_2N] = {};      // W の i がある A のインデックス
BITree<int>* bit_Bs[MAX_2N] = {};
BITree<int>* bit_Ws[MAX_2N] = {};

signed main()
{
    cin >> N;
    REP(i, 2 * N) {
        cin >> C[i] >> A[i];
        if(C[i] == 'W') { posW[A[i]] = i; }
        else { posB[A[i]] = i; }
    }

    ZERO(dp);
    ZERO(costB);
    ZERO(costW);

    REP(i, 2 * N) {
        bit_Bs[i] = new BITree<int>(N);
        bit_Ws[i] = new BITree<int>(N);
        for(int ind = 0; ind < i; ++ind) {
            if(C[ind] == 'B') { bit_Bs[i]->Add(A[ind], 1); }
            else { bit_Ws[i]->Add(A[ind], 1); }
        }
    }

    for(int i = 0; i <= N; ++i) {
        for(int j = 0; j <= N; ++j) {
            costB[i][j] += bit_Bs[posB[i+1]]->Sum(i+2, N);
            costB[i][j] += bit_Ws[posB[i+1]]->Sum(j+1, N);
            costW[i][j] += bit_Bs[posW[j+1]]->Sum(i+1, N);
            costW[i][j] += bit_Ws[posW[j+1]]->Sum(j+2, N);
        }
    }

    dp[0][0] = 0;
    for(int i = 0; i <= N; ++i) {
        for(int j = 0; j <= N; ++j) {
            if(i == 0 && j == 0) { continue; }
            int cand1 = INF, cand2 = INF;
            if(i > 0) { cand1 = dp[i-1][j] + costB[i-1][j]; }
            if(j > 0) { cand2 = dp[i][j-1] + costW[i][j-1]; }
            dp[i][j] = min(cand1, cand2);
        }
    }

    REP(i, 2 * N) {
        delete bit_Bs[i];
        delete bit_Ws[i];
    }

    printf("%d\n", dp[N][N]);
    return 0;
}


Measured memory_bytes: 110720
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long ll;
ll mod_pow(ll x, ll n, ll mod){//x^nのmodのmodを計算する
    if(n == 0) return 1;
    if(n % 2 == 0){
        ll t = mod_pow(x, n / 2, mod); //nが偶数のときはx^n = x^(n / 2) * x^(n / 2)
        return t * t % mod;
    } 
    return x * mod_pow(x, n - 1, mod) % mod;//nが奇数のときはx^n = x * x^(n - 1)
}

int main(){
    ll n;
    ll a, b;
    cin >> n >> a >> b;
    ll ans = mod_pow(2, n, MOD);
    ll finv[b];
    ans--;
    for(int i = 0; i < b; i++){
        finv[i] = mod_pow(i + 1, MOD - 2, MOD);
    }
    ll na = 1, nb = 1;
    for(int i = 0; i < b; i++){
        nb = (nb * (n - i) % MOD) * finv[i] % MOD;
    }
    for(int i = 0; i < a; i++){
        na = (na * (n - i) % MOD) * finv[i] % MOD;
    }
    ans -= (na + nb) % MOD;
    if(ans < 0) cout << ans + MOD << endl;
    else cout << ans << endl;
}

Measured memory_bytes: 1792
var n,i:longint;
    a:array[0..100]of int64;
begin
  readln(n);
  a[1]:=2;
  a[2]:=1;
  for i:=3 to n+1 do 
    a[i]:=a[i-1]+a[i-2];
  writeln(a[n+1]);
end.

Measured memory_bytes: 128
#include<iostream>
using namespace std;
int main(){
  int n;
  while(cin>>n){
    if(n==0) break;
    int s[n+1][n+1];
    for(int i=0;i<n;i++){
      int a = 0;
      for(int j=0;j<n;j++){
	cin >> s[i][j];
	a += s[i][j];
      }
      s[i][n] = a;
    }
    for(int i=0;i<n;i++){
      int a = 0;
      for(int j=0;j<n;j++){
	a += s[j][i];
      }
      s[n][i] = a;
    }
    int a = 0;
    for(int i=0;i<n;i++) a += s[n][i]; 
    s[n][n] = a;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	if(s[i][j]>=10000) cout << s[i][j];
	else if(s[i][j]>=1000) cout << " " << s[i][j];
	else if(s[i][j]>=100) cout << "  " << s[i][j];
	else if(s[i][j]>=10) cout << "   " << s[i][j];
	else if(s[i][j]>=0) cout << "    " << s[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    n = [int(e) for e in lines[0].split(" ")][0]
    d = [int(e) for e in lines[1].split(" ")]

    return (n, d)


def solve(n, d):

    if d[0] != 0:
        return 0 
     
    d.sort()

    z = [] 
    tmp = d[0]
    count = 0
    for i in range(n):
        if tmp != d[i]:
            z.append((tmp, count))
            tmp = d[i]
            count = 1
        else:
            count = count + 1
    z.append((tmp, count))

    if z[0][0] != 0 or z[0][1] > 1:
        return 0

    result = 1
    m = len(z)
    for i in range(1, m):
        if z[i-1][0] + 1 != z[i][0]:
            result = 0
            break
        else:
            result = result * z[i-1][1] ** z[i][1]
    
    res = result % 998244353

    return res


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Measured memory_bytes: 14692
R,G,B,N = map(int,input().split())
ans = 0
for i in range(N//R+1):
  for j in range(N//G+1):
    nokori = N-i*R-j*G
    if nokori >=0 and nokori%B == 0:
      ans +=1
      #print(i,j,nokori//B)
print(ans)
    

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

using pii = pair<int, int>;
using ll  = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl

template <class T>
bool set_min(T &a, const T &b) {
    return a > b ? a = b, true : false;
}

template <class T>
bool set_max(T &a, const T &b) {
    return a < b ? a = b, true : false;
}
// vector
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
    for (T &a : v) is >> a;
    return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (const T &t : v) os << "\t" << t;
    return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
    return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

int main() {
    int N;
    cin >> N;
    vector<string> S(N);
    cin >> S;
    vector<pii> positive, negative;
    rep(i, N) {
        int mini = INF;
        int sum  = 0;
        rep(j, S[i].size()) {
            sum += S[i][j] == '(' ? 1 : -1;
            set_min(mini, sum);
        }
        (sum >= 0 ? positive : negative).emplace_back(sum, mini);
    }
    sort(all(positive), [](const pii a, const pii b) { return pii(-a.second, a.first) < pii(-b.second, b.first); });
    sort(all(negative), [](const pii a, const pii b) { return a.second - a.first < b.second - b.first; });

    bool flg = true;
    ll sum   = 0;
    priority_queue<pii> que;
    auto f = [&](pii p) {
        flg &= sum + p.second >= 0;
        sum += p.first;
        flg &= sum >= 0;
    };
    for (auto p : positive) f(p);
    for (auto p : negative) f(p);

    cout << (sum == 0 and flg ? "Yes" : "No") << endl;
    return 0;
}

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

typedef long long ll;
const int N = 100;

ll u[N], v[N];

int main(){
    int a, b, c;
    ll k;
    scanf("%d%d%d", &a, &b, &c);
    scanf("%lld", &k);
    printf("%d\n", k & 1 ? b - a : a - b);
    return 0;
}

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

#define NONE INT_MAX

int n,m,a,b,c,d,x1,x2,y1,y2;
vector< vector<int> >M;

int Dijkstra(int start,int goal)
{
  vector<int>d(n,NONE),visited(n,0);
  multimap<int,int>q;

  d[start]=0;
  q.insert(make_pair(0,start));

  while(q.size()){
    int u,v;
    u=q.begin()->second;
    q.erase(q.begin());
    if(visited[u]==true)continue;
    visited[u]=true;
    for(v=0;v<n;v++){
      if(M[u][v]==NONE)continue;
      if(visited[v]==true)continue;
      if(d[v]<=d[u]+M[u][v])continue;
      d[v]=d[u]+M[u][v];
      q.insert(make_pair(d[v],v));
    }
  }
  return d[goal];
}

int main()
{
  cin>>n>>m;
  M.resize(n);
  for(int i=0;i<n;i++)M[i].resize(n,NONE);
  for(int i=0;i<m;i++){
    scanf("%d,%d,%d,%d",&a,&b,&c,&d);
    a--;b--;
    M[a][b]=c;
    M[b][a]=d;
  }
  scanf("%d,%d,%d,%d",&x1,&x2,&y1,&y2);
  x1--;x2--;
  cout<<(y1-y2-Dijkstra(x1,x2)-Dijkstra(x2,x1))<<endl;
}

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

namespace ABC148C
{
    class Program
    {
        static void Main(string[] args)
        {
            var ab = Cin.LArr();
            var solver = new Solver(ab[0], ab[1]);

            Console.WriteLine(solver.Solve());
        }
    }

    class Solver
    {
        private long A;
        private long B;
        public Solver(long a, long b)
        {
            A = a;
            B = b;
        }

        public IConvertible Solve()
        {
            return A * B / Gcd(A,B);
        }
        
        public static long Gcd(long a, long b)
        {
            if (a < b)
                // 引数を入替えて自分を呼び出す
                return Gcd(b, a);
            while (b != 0)
            {
                var remainder = a % b;
                a = b;
                b = remainder;
            }

            return a;
        }
    }

    internal static class Cin
    {
        public static string String()
        {
            return Console.ReadLine();
        }

        public static int Int()
        {
            return int.Parse(String());
        }

        public static int[] IArr()
        {
            return Split().Select(int.Parse).ToArray();
        }

        public static long Long()
        {
            return long.Parse(String());
        }

        public static long[] LArr()
        {
            return Split().Select(long.Parse).ToArray();
        }

        public static double Double()
        {
            return double.Parse(String());
        }

        public static double[] DArr()
        {
            return Split().Select(double.Parse).ToArray();
        }

        private static IEnumerable<String> Split()
        {
            return String().Split(' ');
        }
    }
}

Measured memory_bytes: 13396
from itertools import permutations

n,m = map(int,input().split())
ans = 0
dp = list([ 0 for i in range(n)] for _ in range(n))

for _ in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    dp[a][b] = 1
    dp[b][a] = 1

l = [i for i in range(1,n)]
for j in permutations(l):
    bit = 1
    if dp[0][j[0]]!=1:
        bit = 0
    for i in range(n-2):
        if dp[j[i]][j[i+1]]!= 1:
            bit = 0
    if bit==1:
        ans += 1
print(ans)

Measured memory_bytes: 9164
id=input()
s=input()
k=0
if(len(s)==len(id)+1):
  for i in range(len(id)):
    if(id[i]!=s[i]):
      k=0
      break
    else:k=1
else:k=0
 
if(k==1):print("Yes")
else:print("No")

Measured memory_bytes: 9064
#include <bits/stdc++.h>
 
using namespace std;
int n;
long long  F[100000];
int h[100000];
 
int main()
{
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);  
  cin >> n;
    for (int i=0; i<n; i++) cin >> h[i];
    F[0]=0; F[1]=abs(h[1]-h[0]);
  
    for (int i=2; i<n; i++)
    {
        F[i]=min(F[i-1]+abs(h[i]-h[i-1]),F[i-2]+abs(h[i]-h[i-2]));
    }
    cout << F[n-1];
    return 0;
}

Measured memory_bytes: 1408
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll,ll>
#define vvll vector< vector<ll> >
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define tel(a){cout<<a<<"\n";}
#define tell(a,b){cout<<a<<" | "<<b<<"\n";}
#define telll(a,b,c){cout<<a<<" | "<<b<<" | "<<c<<"\n";}
#define teln(v,n){cout<<"v- ";rep(i,0,n)cout<<v[i]<<" ";cout<<"\n";}
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define tihs if(fopen("inp.txt","r"))freopen("inp.txt", "r", stdin),freopen("out.txt", "w", stdout);
using namespace std;
#define TRACE
 
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
 

#define M 1000010
 
int main(){
	IOS;
	tihs;
	
	ll a,b;cin>>a>>b;
	ll x=a/b;
	if(a%b!=0)x++;
	cout<<x;

	return 0;
}

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

ll gcl(ll x,ll y){
    if(y==0) return x;
    else return gcl(y,x%y);
}

int main(){
    int n;cin>>n;
    ll a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    ll ans=a[0];
    for(int i=1;i<n;i++){
        ans=ans*a[i]/gcl(ans,a[i]);
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 3120
#include <bits/stdc++.h>
template <typename T>
constexpr T INF() { return std::numeric_limits<T>::max() / 2; }
int main()
{
    int N, W;
    std::cin >> N >> W;
    std::vector<int> v(N), w(N);
    for (int i = 0; i < N; i++) { std::cin >> v[i] >> w[i]; }
    constexpr int V = 100 * 100;
    std::vector<int> dp(V + 1, INF<int>());
    dp[0] = 0;
    for (int i = 0; i < N; i++) {
        for (int j = V - v[i]; j >= 0; j--) { dp[j + v[i]] = std::min(dp[j + v[i]], dp[j] + w[i]); }
    }
    for (int i = V; i >= 0; i--) {
        if (dp[i] <= W) { return std::cout << i << std::endl, 0; }
    }
    return 0;
}


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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int MAXN = 1e5 + 5;

int x[MAXN], x0;
ll v[MAXN];

ll ans;
int x_last;

void rec_sol(int l, int r){
	if (x0 <= x[l]){
		ans += x[r] - x0;
		x_last = x[r];
		return;
	}
	if (x0 >= x[r]){
		ans += x0 - x[l];
		x_last = x[l];
		return;
	}
	if (v[l] >= v[r]){
		v[l] += v[r];
		rec_sol(l, r - 1);
		ans += x[r] - x_last;
		x_last = x[r];
	} else {
		v[r] += v[l];
		rec_sol(l + 1, r);
		ans += x_last - x[l];
		x_last = x[l];
	}
}

void solve(){
	int n;
	scanf("%d%d", &n, &x0);
	REP(i, 0, n){
		scanf("%d%lld", &x[i], &v[i]);
	}
	rec_sol(0, n - 1);
	printf("%lld\n", ans);
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}



Measured memory_bytes: 3968