akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
#include<cassert>
#include<climits>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
#define ceil(a, b) a / b + !!(a % b)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    map<int, int> mp;
    rep(i, n) {
        int a; cin >> a;
        mp[a]++;
        mp[a - 1]++;
        mp[a + 1]++;
    }
    int key, val = -1;
    for (auto e : mp) {
        if (e.second > val) {
            val = e.second;
            key = e.first;
        }
    }
    cout << val << endl;
}




Measured memory_bytes: 4736
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int main(){
    char matrix[2][3];

    while(scanf("%s", matrix[0]) != -1){
        scanf("%s", matrix[1]);


        if(matrix[0][0] == matrix[1][2] && matrix[0][1] == matrix[1][1] && matrix[0][2] == matrix[1][0]){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    }

    return 0;
}

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

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}

void solve() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    sort(all(A));
    reverse(all(A));

    ll sum = 0;
    for (int i = 1; i < N; i++) {
        sum += A[i];
    }
    if (A[0] < sum) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();
}

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

int main() {
  int N, K, S = 0, x;
  cin >> N >> K;
  for(int i = 0; i < N; i++){
    cin >> x;
    if(x < K - x){
      S = S + 2 * x;
      continue;
    }
    S = S + 2 * K - 2 * x;
  }
  cout << S;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,b,e) for(int i = (b); i <= (e); i++)
#define INF (1001001001)
#define EPS (1e-15)

#define pr(x) do{cout << (#x) << " = " << (x) << endl;}while(0)
#define pri(x,i) do{cout << (#x) << "[" << i << "] = " << (x[i]) << endl;}while(0)
#define pra(x,n) rep(__i,n) pri(x,__i);
#define prar(x,b,e) repr(__i,b,e) pri(x,__i);

typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;

ll MOD = 1000000007;

int in() {
	int a;
	scanf("%d ", &a);
	return a;
}

bool comp(const pint &a, const pint &b) {
	if (a.first != b.first) return a.first < b.first;
	return a.second > b.second;
}
bool comp2(const pint &a, const pint &b) {
	return a.second > b.second;
}

int main() {
	int N = in();
	int M = in();
	vpint choku(N);
	rep(i,N) {
		choku[i].first = in();
		choku[i].second = in();
	}
	sort(choku.begin(), choku.end(), comp);
	vpint choku2;
	int t = 1;
	rep(i,N) {
		if (t <= M && choku[i].first >= t) {
			t++;
		}
		else {
			choku2.pb(choku[i]);
		}
	}
	sort(choku2.begin(), choku2.end(), comp2);
	int NN = choku2.size();
	int s = M;
	int ans = 0;
	rep(i,NN) {
		if (s >= t && choku2[i].second <= s) {
			s--;
		}
		else {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3956
a,b,c = map(int,input().split())

cnt = 0

if a == b:
    cnt += 1

if b == c:
    cnt += 1

if c == a:
    cnt += 1

if cnt == 0:
    print(3)
elif cnt == 1:
    print(2)
elif cnt == 3:
    print(1)
    

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll=long long;
int main(){
    const double pi=acos(-1);
    double a,b,x;
    cin>>a>>b>>x;
    double s=x/a;
    double rad;
    if(s>b*a/2){
        double h=(a*b-s)*2/a;
        rad=atan2(h,a);
    }
    else{
        double w=s/b*2;
        rad=atan2(b,w);
    }
    double ans=rad/(2*pi)*360;
    printf("%.10f",ans);
}

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

int main() {
  int N,i;
  cin >> N;
  vector<int> a(N);
  for(i=0;i<N;i++) cin >> a.at(i);
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  int x;
  if(N == 1) cout << a.at(0) << endl;
  else if(N % 2 == 0){
    for(i=0;i<N/2;i++){
      x = x + a.at(2*i) - a.at(2*i+1);
    }
    cout << x << endl;
  }
  else{
    for(i=0;i<(N-1)/2;i++){
      x = x + a.at(2*i) - a.at(2*i+1);
    }
    cout << x + a.at(N-1) << endl;
  }
}




Measured memory_bytes: 3556
N, C = map(int, input().split())
D = [list(map(int, input().split())) for i in range(C)]
G = [list(map(int, input().split())) for i in range(N)]

change = [[0]*C for i in range(3)]
for c in range(C):
    for i in range(1, N+1):
        for j in range(1, N+1):
            change[(i+j) % 3][c] += (D[G[i-1][j-1]-1][c] if G[i-1][j-1] != c+1 else 0)

ans = float('inf')
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            if len(set((c1, c2, c3))) != 3:
                continue
            ans = min(ans, change[0][c1] + change[1][c2] + change[2][c3])

print(ans)


Measured memory_bytes: 45532
import sys
sys.setrecursionlimit(10**9)

memo = dict()


def dfs(v):
    if v == 1:
        return 1
    elif v in memo:
        return memo[v]
    else:
        memo[v] = 2 * dfs(v // 2) + 1
        return memo[v]


def main():
    H = int(input())

    print(dfs(H))


if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
s = int(input())
li = []
li.append(s)

for i in range(1, 1000000):
	if li[i-1] % 2 == 0:
		a = int(li[i-1] /2)
		if a in li:
			print(i + 1)
			break
		else:
			li.append(a)
           
	else:
		b = 3 * li[i-1] + 1
		if b in li:
			print(i + 1)
			break
		else:
			li.append(b)

Measured memory_bytes: 3060
#include <stdio.h>
 
int main(void)
{
 
    int a, b, c, d;
 
    scanf("%d %d %d", &a, &b, &c);
 
    d = c + b - a;
 
    if (d <= 0)
    {
        d = 0;
    }
    printf("%d\n", d);
    return 0;
}

Measured memory_bytes: 128
use std::io::{stdin, Read};
use std::str::FromStr;
use std::collections::HashSet;

fn read<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let s = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok()
}
 
fn main() {
    let n: usize = read().unwrap();
    let mut d: Vec<u64> = Vec::new();
 
    for _i in 0..n {
        let x: u64 = read().unwrap();
        d.push(x);
    }
    d.sort();

    let m: usize = read().unwrap();
    let mut t: Vec<u64> = Vec::new();
 
    for _i in 0..m {
        let x: u64 = read().unwrap();
        t.push(x);
    }
    t.sort();

    let mut used_d: HashSet<usize> = HashSet::new();

    let mut i: usize = 0;
    for x in &t {
        while i < n && d[i] < *x {
            i += 1;
        }
        if i < n && d[i] == *x {
            used_d.insert(i);
            i += 1;
            //continue;
        } else { // x < d[i]
            //break;
        }
    }
    if used_d.len() == m {
        println!("YES");
    } else {
        println!("NO");
    }
}


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

int main() {
  int N,X;
    cin >> N >> X;
  vector<int> L(N);
  for (int i = 0; i < N; i++) 
    cin >> L.at(i);
  
  int TOKORO = 0;
  int HANERU = 1;
  for (int i=0; i<N; i++){
    TOKORO += L.at(i);
      if (TOKORO > X ){
        break;
      }
    HANERU++;
  }
  cout << HANERU << endl;
}

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

ll num[sz];
ll cnt[sz];
int main()
{


   ll n,d=1,p,i,j,k,f=0,a,b,x,y,s=1,c=0;
   cin>>n;

   for(i=1;i<=n;i++)
   {
       cin>>num[i];
       cnt[num[i]]++;
       if(num[i]==0 && i!=1)f=1;
       if(i==1 && num[i]!=0 )
       {
           f=1;
       }

   }
   if(f==1)
   {
       cout<<0<<endl;
       return 0;
   }

   for(i=1;i<n;i++)
   {
       a=cnt[i];
       b=cnt[i+1];
       d=1;
       for(j=1;j<=b;j++)
       {
           d=((d%md)*(a%md))%md;
       }
       //cout<<d<<endl;
       s=((s%md)*(d%md))%md;
   }
   cout<<s<<endl;

    return 0;
}


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


int main(){
  int N,S,T,H,W,x,y;// S->y T->x
  while(cin >> N && N){
    cin >> W >> H;
    int imap[H+140][W+140],graph[H+140][W+140];
    for(int i=0;i<=H+110;i++)for(int j=0;j<=W+110;j++)imap[i][j] =graph[i][j] = 0;
    for(int i=0;i<N;i++){
      cin >> x >> y;
      imap[y][x] = 1;
    }

    int plus;
    //Create DP-graph
    for(int i=1;i<=H+100;i++){
      plus = 0;
      for(int j=1;j<=W+100;j++){
	if(imap[i][j] == 1){
	  plus++;
	}
	graph[i][j] = plus+graph[i-1][j];
	//cout << graph[i][j] << " ";
      }
      //cout << endl;
    }



    int ans = 0;
    cin >> S >> T;
  for(int i=1;i<=H;i++){
      for(int j=1;j<=W;j++){
	//if(imap[i][j] == 1){
	  //cout << "graph[i+T-1][j+S-1] = " << graph[i+T-1][j+S-1] << ", graph[i+T-1][j-1] = " << graph[i+T-1][j-1] << ", graph[i-1][j+S-1] = " << graph[i-1][j+S-1] << ", graph[i-1][j-1] = " << graph[i-1][j-1] << endl;
	  ans = max(ans,graph[i+T-1][j+S-1]-graph[i+T-1][j-1]-graph[i-1][j+S-1]+graph[i-1][j-1]);
	  //}
      }
    }
  cout << ans << endl;



  }
  return 0;
}

Measured memory_bytes: 0
#define _CRT_SECURE_NO_WARNINGS
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
typedef long long ll;
using namespace std;

ll w[100000];
ll n, k;

ll f(ll P) {
	vector<ll> track;
	for (int i = 0;i < k;i++) track.push_back(P);
	ll wi=0,ti=0;
	while (ti < k&&wi<n) {
		if (w[wi] <= track[ti]) {
			track[ti] -= w[wi];
			wi++;
		}
		else {
			ti++;
		}
	}
	return wi;
}

int main() {
	cin >> n >> k;
	for (int i = 0;i < n;i++) cin >> w[i];
	ll P;
	ll left = 0, right = 1000000000;
	while(left<right){
		P = (left + right) / 2;
		if (f(P) == n) {
			right = P;
		}
		else if (f(P) < n) {
			left = P+1;
		}
	}
	P = (left + right) / 2;
	while(f(P-1)==n) P--;
	cout << P << endl;
}

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

int main() {
    int n;
    cin >> n;
    int ac = 0, wa = 0, tle = 0, re =0;
    
    string s;
    
    for (int i = 0; i < n; i++) {
        cin >> s;
        if ("AC"==s) ac++;
        else if ("WA" == s) wa++;
        else if ("TLE" == s) tle++;
        else if ("RE" == s) re++;
    }
    
    cout << "AC x " << ac << endl;
    cout << "WA x " << wa << endl;
    cout << "TLE x " << tle << endl;
    cout << "RE x " << re << endl;
    return 0;
}


Measured memory_bytes: 3616
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
long long A; int B;
int main() {
	cin >> A >> B;
	long long l = max(A - B, 2LL), r = A + B + 1;
	int p = sqrt(r);
	vector<long long> f(r - l);
	vector<bool> ok(r - l, true); vector<int> x(r - l, 999999999);
	for (int i = 0; i < r - l; i++) f[i] = l + i;
	for (int i = 2; i <= p; i++) {
		long long w = l / i * i;
		if (w < l) w += i;
		for (long long j = w; j < r; j += i) {
			int cnt = 0;
			while (f[j - l] % i == 0) f[j - l] /= i, cnt++;
			if (cnt >= 1) {
				if (x[j - l] < cnt) ok[j - l] = false;
				x[j - l] = cnt;
			}
		}
	}
	int ret = 0;
	for (int i = 0; i < r - l; i++) ret += (ok[i] ? 1 : 0);
	cout << ret << endl;
	return 0;
}

Measured memory_bytes: 14696
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair

vector<pair<int, int> > ps;
set<pair<int, int> > vis;

void rec(const pair<int, int>& p) {
    if (vis.count(p)) return ;
    vis.insert(p);
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            const pair<int, int> np(p.first+dx, p.second+dy);
            if (binary_search(ps.begin(), ps.end(), np)) rec(np);
        }
    }
}

int main() {
    int N;
    double R;
    scanf("%d%lf", &N, &R);
    R *= 1.05;
    rep (i, N) {
        double x, y;
        scanf("%lf%lf", &x, &y);
        x += 1e7, y += 1e7;
        ps.push_back(mp(int(x/R), int(y/R)));
    }
    sort(ps.begin(), ps.end());
    int ans = 0;
    rep (i, ps.size()) if (!vis.count(ps[i])) rec(ps[i]), ans++;
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 10900
#include <stdio.h>
#include <stdlib.h>
#define int long long
#define inf (int)(1e15)
#define datatype distance

typedef struct {
	int a;
	int b;
	int d;
}distance;

int min(int a, int b){
	if(a < b){
		return a;
	}
	else{
		return b;
	}
}

//比較関数
int compare(datatype a, datatype b){
	return a.d - b.d;
}

//昇順
void sort_sub(datatype *origin, int left, int right, datatype *tmp){
	if(right - left > 1){
		int i, j, k, half = (left + right) / 2;
		sort_sub(origin, left, half, tmp);
		sort_sub(origin, half, right, tmp);
		for(i = left; i < right; i++){
			tmp[i] = origin[i];
		}
		for(i = left, j = left, k = half; i < right; i++){
			if((compare(tmp[j], tmp[k]) <= 0 && j < half) || k == right){
				origin[i] = tmp[j];
				j++;
			}
			else{
				origin[i] = tmp[k];
				k++;
			}
		}
	}
}

void sort(datatype *origin, int N){
	datatype *tmp = (datatype *)malloc(sizeof(datatype) * N);
	sort_sub(origin, 0, N, tmp);
	free(tmp);
}

signed main(){
	int N, i, j, k, ans = 0;
	scanf("%lld", &N);
	int **A = (int **)malloc(sizeof(int *) * N);
	int **B = (int **)malloc(sizeof(int *) * N);
	distance *D = (distance *)malloc(sizeof(distance) * (N * (N - 1) / 2));
	for(i = 0, k = 0; i < N; i++){
		A[i] = (int *)malloc(sizeof(int) * N);
		B[i] = (int *)malloc(sizeof(int) * N);
		for(j = 0; j < N; j++){
			scanf("%lld", &A[i][j]);
			if(i < j){
				D[k].a = i;
				D[k].b = j;
				D[k].d = A[i][j];
				k++;
			}
			if(i == j){
				B[i][j] = 0;
			}
			else{
				B[i][j] = inf;
			}
		}
	}
	sort(D, N * (N - 1) / 2);
	for(k = 0; k < N * (N - 1) / 2; k++){
		if(B[D[k].a][D[k].b] < D[k].d){
			printf("-1\n");
			return 0;
		}
		else if(B[D[k].a][D[k].b] > D[k].d){
			for(i = 0; i < N; i++){
				for(j = 0; j < N; j++){
					B[i][j] = min(B[i][j], min(B[i][D[k].a] + D[k].d + B[D[k].b][j], B[i][D[k].b] + D[k].d + B[D[k].a][j]));
					B[j][i] = B[i][j];
				}
			}
			ans += D[k].d;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 3712
from collections import Counter
n = int(input())
ans = 0
def dfs(A):
    if A and int(A) > n:
        return
    if len(A) >= 3:
        c = Counter(A)
        is753 = True
        for i in ['7','5','3']:
            if c[i] < 1:
                is753 = False
        if is753:
            global ans 
            ans += 1
    for nex in ['7','5','3']:
        A += nex
        dfs(A)
        A = A[:-1]
dfs('')
print(ans)

Measured memory_bytes: 9316
N = int(input())
A = list(map(int ,input().split()))
sA = sorted(A, reverse=True)
if N%2==0:
    print(sA[0]+2*sum(sA[1:N//2]))
else:
    print(sA[0]+2*sum(sA[1:(N-1)//2])+sA[(N-1)//2])


Measured memory_bytes: 31440
N=int(input());R,d,f,s,x=sorted(list(map(int,input().split()))for i in range(N))+[(2e9,0)],[0]*N+[1],[0]*N,1,N
for i in range(N-1,-1,-1):
	while R[x][0]<sum(R[i]):x=f[x]
	d[i]=s=(s+d[x])%998244353;f[i],x=x,i
print(s)

Measured memory_bytes: 64044
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using pdd = pair<double, double>;
using uint = unsigned int;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);

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

    int N;
    cin >> N;
    vector<ll> asum(N + 1);
    for (int i = 1; i <= N; ++i) {
        cin >> asum[i];
        asum[i] += asum[i - 1];
    }

    vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, 1e17));
    vector<vector<int>> spl(N + 1, vector<int>(N + 1));
    for (int w = 0; w <= N; ++w) {
        for (int l = 0; l + w <= N; ++l) {
            int r = l + w;
            if (w <= 1) {
                dp[l][r] = 0;
                spl[l][r] = l;
                continue;
            }

            for (int mid = spl[l][r - 1]; mid <= spl[l + 1][r]; ++mid) {
                ll tmp = dp[l][mid] + dp[mid][r] + asum[r] - asum[l];
                if (tmp < dp[l][r]) {
                    dp[l][r] = tmp;
                    spl[l][r] = mid;
                }
            }
        }
    }

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

    return 0;
}


Measured memory_bytes: 5064
#include<string>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
	int a,b,c;
	cin>>a>>b;
	c=a*b-a-b+1;
	cout<<c;
	return 0;
}

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

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

int X[10][10];

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

  int N, D;

  cin >> N >> D;
  REP(i, N) {
    REP(j, D) { cin >> X[i][j]; }
  }

  int ans = 0;
  float d1 = 0;
  float d2 = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      d1 = 0;
      for (int k = 0; k < D; ++k) {
        d1 += (abs(X[i][k] - X[j][k]) * abs(X[i][k] - X[j][k]));
      }
      d2 = sqrt(d1);
      if (d2 == ceil(d2)) {
        ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
n,m = map(int,input().split())
l_a = sorted(list(map(int,input().split())))
l_bc = [list(map(int,input().split())) for _ in range(m)]
l_bc.sort(key = lambda x:x[1],reverse = True)
for b,c in l_bc:
    l_a.extend([c]*b)
    if len(l_a) > 2*n:
        break

l_a.sort(reverse = True)
print(sum(l_a[:n]))

Measured memory_bytes: 34756
/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int ll;
const int mod = 1e9 + 7;
ll powmod(ll a, ll b) {
    ll res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}
int nxt() {
    int x;
    scanf("%d", &x);
    return x;
}

void add_self(int &a, int b) {
    a += b;
    if (a >= mod) {
        a -= mod;
    }
}

int main() {
    int n = nxt();
    VVI can(n, VI(n));
    REP(i, n)
    REP(j, n)
    can[i][j] = nxt();
    vector<int> dp(1 << n);
    dp[0] = 1;
    for (int mask = 0; mask < (1 << n) - 1; mask++) {
        int a = __builtin_popcount(mask);
        for (int b = 0; b < n; b++) {
            if (can[a][b] && !(mask & (1 << b))) {
                int m2 = mask ^ (1 << b);
                add_self(dp[m2], dp[mask]);
            }
        }
    }
    printf("%d\n", dp[(1 << n) - 1]);
    return 0;
}


Measured memory_bytes: 8448
#!/usr/bin python3
# -*- coding: utf-8 -*-

k = int(input())
a = list(map(int, input().split()))
a= a[::-1]
if a[0]!=2:
    print(-1)
    exit()
mi = a[0]
mx = a[0]*2-1
for i in range(1,k):
    ai = a[i]
    mi = (mi+ai-1)//ai
    mx = mx//ai
    if mi>mx:
        print(-1)
        exit()
    else:
        mi = mi*ai
        mx = (mx+1)*ai-1
print(mi, mx)

Measured memory_bytes: 20348
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define loop(i, a, b) for(ll i = a; i < b; i++)
#define rloop(i, e, s) for(ll i = e; i >= s; i--)
#define mod 1000000007
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(nullptr);

int main()
{
    ll n, x, ans = 0;
    cin >> n;
    string str;
    map<string, ll> m;
    loop(i, 0, n){
        cin >> str;
        sort(str.begin(), str.end());
        if(m.find(str) != m.end()) m[str]++;
        else m[str] = 1;
    }
    auto it = m.begin();
    for(;it != m.end(); it++){
        x = it->second;
        ans += (x)*(x- 1)/2;
    }
    cout << ans << endl;
}

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

#define int long long
#define otp(n) cout << n;
#define ct(n) cout << n << endl;
#define cts(n) cout << n << " ";
#define cte cout << endl;
#define ctv(v) rep(i,(v).size()) otp(v[i]) cte
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define repo(i,o,n) for(int i=o;i<n;i++)
#define repm(i,n) for(int i=n-1;i>=0;i--)
#define repv(i,v) for(int i=0;i<(v).size();i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sperase(v,n) (v).erase(remove(all(v), n), (v).end());
#define vdelete(v) (v).erase(unique(all(v)), (v).end());
#define vcopy(v1,v2) copy((v1).begin(), (v1).end(), back_inserter((v2)));
#define pb(n) push_back(n);

int n,m;
char a[50][50],b[50][50];

main() {
    cin >> n >> m;
    rep(i,n) rep(j,n) cin >> a[i][j];
    rep(i,m) rep(j,m) cin >> b[i][j];
    rep(i,n-m+1) rep(j,n-m+1) {
        repo(y,i,i+m) repo(x,j,j+m) if (a[y][x] != b[y-i][x-j]) goto hoge;
        ct("Yes")
        return 0;
        hoge:;
    }
    ct("No")
}


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

int main() {
	int n = 0;
	cin >> n;
	int* all = new int[n];
	for (int i = 0; i < n; i++)
		cin >> all[i];
	long long ans = 0;
	int sum = 0;
	int last = 0;
	long long ans2 = 0;
	int sum2 = 0;
	int last2 = 0;
	bool flag = false;
	if (all[0] > 0)
		last = -1;
	else if (all[0] < 0)
		last = 1;
	else {
		last = -1;
		all[0] = 1;
		ans++;
		flag = true;
	}
	for (int i = 0; i < n; i++) {
		sum += all[i];
		if (last == 1) {
			if (sum >= 0) {
				ans += sum + 1;
				sum = -1;
			}
			last = -1;
		}
		else if (last == -1) {
			if (sum <= 0) {
				ans += -1 * sum + 1;
				sum = 1;
			}
			last = 1;
		}
	}
	if (all[0] > 0) {
		if (flag) {
			last2 = 1;
			ans2++;
			all[0] = -1;
		}
		else {
			last2 = 1;
			ans2 += all[0] +1;
			all[0] = -1;
		}
	}
	else if (all[0] < 0) {
		last2 = -1;
		ans2 += all[0] * -1 +1; 
		all[0] = 1;
	}
	for (int i = 0; i < n; i++) {
		sum2 += all[i];
		if (last2 == 1) {
			if (sum2 >= 0) {
				ans2 += sum2 + 1;
				sum2 = -1;
			}
			last2 = -1;
		}
		else if (last2 == -1) {
			if (sum2 <= 0) {
				ans2 += -1 * sum2 + 1;
				sum2 = 1;
			}
			last2 = 1;
		}
	}
	if (ans < ans2)
		cout  << ans;
	else
		cout  << ans2;
	system("pause");

}


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

typedef long long ll;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 1e6 + 5;
const int LOG = 21;
int n;
int a[maxn], nxt[maxn];
int w;
int q;
int table[LOG+1][maxn];

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

  cin>>n;
  for (int i=0; i<n; i++) {
    cin>>a[i];
  }
  cin>>w;

  nxt[n-1]=n-1;
  for (int i=0; i<n-1; i++) {
    int idx = std::lower_bound(a,a+n,min(a[n-1],a[i]+w)) - a;
    if (a[idx] > a[i]+w) idx--;
    assert(idx>i); //we should be able to make progress
    nxt[i] = idx;
  }
  
  for (int i=0; i<n; i++) {
    table[0][i] = nxt[i];
  }
  
  for (int j=1; j<=LOG; j++) {
    for (int i=0; i<n; i++) {
      table[j][i] = table[j-1][table[j-1][i]];
    }
  }

  cin>>q;
  while (q--) {
    int l,r; cin>>l>>r; l--;r--;
    if (l>r) swap(l,r);
    int ans = 0;
    for (int j=LOG; j>=0; j--) {
      if (table[j][l] < r) {
	ans = ans | (1<<j);
	l = table[j][l];
      }
    }
    ++ans;
    cout<<ans<<"\n";
  }

  
  return 0;
}


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

typedef long long ll;

#define REP(i, n) for(int i=0; i<(n); i++)
#define REPS(i, n) for(int i=0; i<=(n); i++)
#define FOR(i, a, b) for(int i=a; i<(b); i++)
#define FORS(i, a, b) for(int i=a; i<=(b); i++)
// for container x
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define SIZE(v) ((ll)(v).size())
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;

bool judge_odd(int N){
    int cnt = 0;
    while(N){
        N/=10;
        cnt++;
    }
    if (cnt%2==1) return true;
    else return false;
}

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

    int ans = 0;
    FORS(i, 1, N){
        if(judge_odd(i)) ans++;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
h,w=map(int,input().split())
l=[input() for i in range(h)]
ans=[[0]*(w+2) for i in range(h+2)]

for i in range(h):
  for j in range(w):
    if l[i][j]=='#':
      ans[i+1][j+1]=10
      
      ans[i][j]+=1
      ans[i][j+1]+=1
      ans[i][j+2]+=1
      
      ans[i+1][j]+=1
      ans[i+1][j+2]+=1
      
      ans[i+2][j]+=1
      ans[i+2][j+1]+=1
      ans[i+2][j+2]+=1
      
ans=[i[1:-1] for i in ans][1:-1]
ans=[['#' if j>=10 else str(j) for j in i] for i in ans]
for i in ans:
  print(''.join(i))





Measured memory_bytes: 40304
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int k = Integer.parseInt(br.readLine());
		br.close();

		if (k % 2 == 0 || k % 5 == 0) {
			System.out.println(-1);
			return;
		}

		long now = 0;
		int ans = 0;
		while (true) {
			for (int i = 0; i <= 9; i++) {
				long val = now + k * i;
				String s = String.valueOf(val);
				boolean flg = true;
				for (int j = 0; j < s.length(); j++) {
					if (s.charAt(j) != '7') {
						flg = false;
						break;
					}
				}
				if (flg) {
					ans += s.length();
					System.out.println(ans);
					return;
				}
				if (s.charAt(s.length() - 1) == '7') {
					now = val / 10;
					ans++;
				}
			}
		}
	}
}


Measured memory_bytes: 50832
//#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define dump(x)                             \
    if (dbg) {                              \
        cerr << #x << " = " << (x) << endl; \
    }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    } else
        return 0;
}
void Yes(bool flag = true) {
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
void No(bool flag = true) {
    Yes(!flag);
}
void YES(bool flag = true) {
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
void NO(bool flag = true) {
    YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
}  // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans
int N;

void solve() {
}

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

    cin >> N;
    int ans = 0;
    for (int i = 1; i < N; i++) {
        ans += (N - 1) / i;
    }
    cout << ans << endl;

    solve();
    return 0;
}


Measured memory_bytes: 3652
#include <bits/stdc++.h>
#define INF (1<<30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

int main(){
	int x, y; cin >> x >> y;
	int ans = INF;
	{
		int p = x, q = y;
		if( q-p >= 0 )
			ans = min( q-p, ans );
	}
	{
		int p = -x, q = y;
		if( q-p >= 0 )
			ans = min( q-p+1, ans );
	}
	{
		int p = x, q = -y;
		if( q-p >= 0 )
			ans = min( q-p+1, ans );
	}
	{
		int p = -x, q = -y;
		if( q-p >= 0 )
			ans = min( q-p+2, ans );
	}
	cout << ans << endl;
	return 0;
}

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

using namespace std;

int main() {
		int N, Q;
		cin >> N >> Q;
		string S;
		cin >> S;

		vector<int> l(Q,0), r(Q,0);
		for ( int i = 0; i < Q; i++ ) cin >> l[i] >> r[i];

		vector<int> cum_sum(N+1, 0);
		for ( int i = 1; i < N+1; i++ ) {
				if ( S[i - 1] == 'A' && S[i] == 'C' ) cum_sum[i + 1] = cum_sum[i] + 1;
				else cum_sum[i + 1] = cum_sum[i];
		}

		for ( int i = 0; i < Q; i++ ) {
				cout << cum_sum[r[i]] - cum_sum[l[i]] << endl;
		}

		return 0;
}


Measured memory_bytes: 2176
using System;

namespace AtCoderBeginnerContest165B
{
    class Program
    {
        static void Main(string[] args)
        {
            long x = long.Parse(Console.ReadLine());

            var m = 100L;
            var result = 0L;
            for (; (m >= x) == false;)
            {
                result++;
                m = m + m * 1L / 100L;

            }

            Console.WriteLine(result);
        }
    }
}


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

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using tsii = tuple<string, int, int>;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define m0(x) memset(x,0,sizeof(x))

int dx4[4] = {1,0,-1,0}, dy4[4] = {0,1,0,-1};


int main()
{
    ll k,x;
    cin>>k>>x;
    for (int i = x-k+1; i < x+k; i++)
    {
        if(i!=x-k+1) cout<<" ";
        cout<<i;
    }
    cout<<endl;

    return 0;
}

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

using ll = long long;
// #define int long long

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)

/***** MAIN *****/
signed main() {
  int n, ans = 0;
  scanf("%d", &n);
  int b[n-1];
  REP(i, n-1) {
    scanf("%d", &b[i]);
  }
  ans += b[0];
  REPS(i, 1, n-1) {
    ans += min(b[i], b[i-1]);

  }
  ans += b[n-2];
  cout << ans;

  
  cout << "\n";
  return 0;
}
/***** MAIN *****/

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<deque>
#include<queue>
using namespace std;
#define int long  long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int INF = 1e10;
const long double PI = (acos(-1));


signed main() {
	int N, H;
	cin >> N >> H;
	priority_queue<P>q;
	rep(i, N) {
		int a, b;
		cin >> a >> b;
		P A = { a,INF };
		q.push(A);
		P B = { b,1 };
		q.push(B);
	}
	int ans = 0;
	while (H > 0) {
		P p = q.top();
		q.pop();
		if (p.second == 1) {
			H -= p.first;
			ans++;
		}
		else {
			int num = (H + p.first - 1) / p.first;
			ans += num;
			H -= p.first * num;
		}
	}
	cout << ans << endl;
}

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

struct data {
  int a, b;
};

long long n;
vector<long long> ans;
vector<bool> ch;
vector<data> v, edge[100005];
vector<set<int>> memo;

long long solve();

int main() {
  cin >> n;
  ans.resize(n - 1, 0);
  for(int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    v.push_back({--a, --b});
    edge[a].push_back({b, i});
    edge[b].push_back({a, i});
  }

  cout << solve() << endl;
  for(int i = 0; i < n - 1; ++i) cout << ans[i] << endl;
  return 0;
}

long long solve() {
  int k = 0;
  queue<int> qu;
  qu.push(0);
  ch.assign(n, 0);
  memo.resize(n);
  ch[0] = 1;
  while(!qu.empty()) {
    int now = qu.front(), col = 0;
    qu.pop();
    for(int i = 0; i < edge[now].size(); ++i)
      if(!ch[edge[now][i].a]) {
        int to = edge[now][i].a;
        while(memo[now].find(col) != memo[now].end()) ++col;
        memo[to].insert(col);
        memo[now].insert(col);
        k = max(k, col);
        ans[edge[now][i].b] = col + 1;
        qu.push(to);
        ch[to] = 1;
      }
  }
  return k + 1;
}

Measured memory_bytes: 22132
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll A, B, N;

ll fx(ll t) {
    return floor(A * t / B) - A * floor( t / B );
}

void solve() {
    cin >> A >> B >> N;
    ll m = min(B - 1, N);
    cout << fx(m) << endl;
}

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


Measured memory_bytes: 3640
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define vel vector<long long>
#define vvel vector<vel>
#define int long long
#define rep(i,n) for(long long i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define V vector
long long p = 998244353;
vel kai(4001, 1);
vel ink(4001, 1);
vel uni(vel &list) {
	if (list.size() == 0) { return list; }
	sort(list.begin(), list.end());
	vel ql(1, list[0]);
	rep(i, list.size() - 1) {
		if (list[i] != list[i + 1]) { ql.push_back(list[i + 1]); }
	}
	return ql;
}
int ru(int a, int r) {
	if (r == 0) { return 1; }
	int ans = ru(a, r / 2);
	ans *= ans; ans %= p;
	if (r % 2 == 1) { ans *= a; }
	return ans % p;
}
int inv(int a) {
	return ru(a, p - 2);
}
void make_kai() {
	rep(i, 4000) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
	rep(i, 4001) { ink[i] = inv(kai[i]); }
}
int com(int n, int r) {
	int ans = kai[n] * ink[r];
	ans %= p; ans *= ink[n - r]; ans %= p;
	return ans;
}
signed main() {
	int n; cin >> n; int m; cin >> m;
	V<V<int>> way(n);
	rep(i, m) {
		int u, v, w; cin >> u >> v >> w; u--; v--;
		way[u].push_back(v);
		way[v].push_back(u);
	}
	V<bool> is_use(n, false);
	queue<int> qu;
	int ans = 0;
	rep(in, n) {
		if (!is_use[in]) {
			ans++;
			is_use[in] = true;
			qu.push(in);
			while (!qu.empty()) {
				int now = qu.front(); qu.pop();
				rep(i, way[now].size()) {
					int nex = way[now][i];
					if (!is_use[nex]) {
						is_use[nex] = true;
						qu.push(nex);
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 7412
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;

#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

int main()
{
    int n;
    cin >> n;
    
    long a[n+1];
    for (long i = 0; i < n+1; i++)
    {
        cin >> a[i];
    }
    
    long b[n];
    for (long i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    
    long count = 0;
    for (int i = 0; i < n; i++)
    {
        long diff = b[i] - a[i];
        if (diff > 0)
        {
            count += a[i];
            long overNum = a[i+1] - diff;
            if (overNum > 0)
            {
                a[i+1] -= diff;
                count += diff;
            }
            else
            {
                count += a[i+1];
                a[i+1] = 0;
            }
            
        }
        else
        {
            count += b[i];
        }
    }

    cout << count << endl;

    return 0;
}

Measured memory_bytes: 1792
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define UNIQUE(a) unique(a.begin(), a.end())
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <numeric>
#include <algorithm>
#include <queue>
#include <set>
#include <utility>
#include <ios>
#include <iomanip>
#include <sstream>
#include <map>

using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

const ll mod = 1e9 + 7;

int main() {
	int h, w;
	cin >> h >> w;
	vector<vector<char>> s(h,vector<char>(w,0));
	vector<vector<int>> memorow(h, vector<int>(w, -1));
	vector<vector<int>> memocol(h, vector<int>(w, -1));

	REP(i, h) {
		REP(j, w) {
			cin >> s[i][j];
		}
	}

	int max = 0;
	REP(i, h) {
		REP(j, w) {
			if (s[i][j] != '#') {
				int tmp = 0, tmprow = 0, tmpcol = 0;
				int up = i-1, down = i+1;
				int left = j-1, right = j+1;
				bool bmemocol = false, bmemorow = false;
				while (up >= 0 && s[up][j] != '#' && bmemocol==false) {
					if (memocol[up][j] != -1) {
						tmpcol = memocol[up][j];
						bmemocol = true;
						break;
					}
					up--;
					tmpcol += 1;
				}
				while (down < h && s[down][j] != '#' && bmemocol == false) {
					down++;
					tmpcol += 1;
				}
				while (left >= 0 && s[i][left] != '#'&& bmemorow == false) {
					if (memorow[i][left] != -1) {
						tmprow = memorow[i][left];
						bmemorow = true;
						break;
					}
					left--;
					tmprow += 1;
				}
				while (right < w && s[i][right] != '#' && bmemorow == false) {
					right++;
					tmprow += 1;
				}
				memocol[i][j] = tmpcol;
				memorow[i][j] = tmprow;
				if (tmprow+tmpcol > max) {
					max = tmprow + tmpcol;
				}
			}
		}
	}
	cout << max+1 << endl;
}

Measured memory_bytes: 35584
def sum_digits(n):

    n = str(n)
    summ = 0

    for d in n:
        summ += int(d)

    return summ

n = int(input())
min_summ = 1000000

for a in range(1, n):

    min_summ = min(min_summ, sum_digits(a) + sum_digits(n-a))

print(min_summ)


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

int main()
{
    string s; cin >> s;

    auto result = 0;
    auto height = 0;
    for (const auto c : s)
    {
        if (c == 'S') height++;
        else
        {
            if (height == 0) result++;
            else height--;
        }
    }
    result += height;

    cout << result << endl;
}


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

using namespace std;
typedef long long ll;
const int maxn = 2000 + 5;
const ll oo = 0x3f3f3f3f3f3f3f3f;
int N, M;
ll d1, d2;

int main()
{
	while (cin >> N >> M)
	{
		vector<int>a(M);
		vector<int>b(M);
		vector<ll>c(M);
		for (int i = 0; i < M; i++)
		{
			cin >> a[i] >> b[i] >> c[i];
			a[i]--,	b[i]--;
		}
		vector<ll>d(N, -oo);
		d[0] = 0LL;
		for (int i = 0; i < 2 * N; i++)
		{
			for (int j = 0; j < M; j++)
			{
				d[b[j]] = max(d[b[j]], d[a[j]] + c[j]);
			}
			if (i == N - 1)
				d1 = d[N - 1];
			if (i == 2 * N - 1)
				d2 = d[N - 1];
		}
		if (d1 == d2)
			cout << d1 << endl;
		else
			cout << "inf" << endl;
	}
	return 0;
}


Measured memory_bytes: 384
n = int(input())
d1 = [0] * n
d2 = [0] * n
for i in range(n):
    d1[i], d2[i] = map(int,input().split())

for i in range(n-2):
    if d1[i] == d2[i] and d1[i+1] == d2[i+1] and d1[i+2] == d2[i+2]:
        print('Yes')
        break
else:
    print('No')

Measured memory_bytes: 9188
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
	using namespace std;
	
	int xs,ys,w,h,n,ban[111][111],xg,yg;bool visited[101][101];
		int vecx[4]={1,0,-1,0},vecy[4]={0,1,0,-1};
	struct P{
		int xx,yy;
	};	
	
	queue<P> jo;
	bool bfs(void){	
		while(!jo.size()==0){
		P now=jo.front();jo.pop();
		if(now.xx==-1||now.yy==-1)return false;
		if(now.xx==xg&&now.yy==yg)return true;
		int x=now.xx,y=now.yy;
		for(int i=0;i<4;i++){
			if(ban[x+vecx[i]][y+vecy[i]]==ban[xs][ys]){
				if(!visited[x+vecx[i]][y+vecy[i]]){
				P next={x+vecx[i],y+vecy[i]};
				visited[x+vecx[i]][y+vecy[i]]=true;
				jo.push(next);
				}
			}
		}
	}
		return 0;
	}

int main(){

	while(cin>>w>>h&&w&&h){
	cin>>xs>>ys>>xg>>yg>>n;
		P start={xs,ys};
	jo.push(start);
	for(int i=0;i<101;i++)
	for(int j=0;j<101;j++)ban[i][j]=-1,visited[i][j]=false;
	
	for(int i=0;i<n;i++){
		int c,d,x,y;
		cin>>c>>d>>x>>y;
		if(!d){
			for(int i=x;i<x+4;i++){
				for(int j=y;j<y+2;j++){
					ban[i][j]=c;
				}
			}
		}
		else{
		for(int i=x;i<x+2;i++){
				for(int j=y;j<y+4;j++){
					ban[i][j]=c;
				}
			}
		}
		}
		if(bfs())cout<<"OK"<<endl;
	else cout<<"NG"<<endl;
	}
	return 0;
	}

Measured memory_bytes: 1268
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> V;

//行列の掛け算（正方行列）
V mult(V a,V b,int mod)
{
	V ret(a.size());
	for(int i=0;i<a.size();i++)
	{
		for(int j=0;j<a[i].size();j++)
		{
			ll tmp=0;
			for(int k=0;k<a[i].size();k++)
			{
				tmp+=a[i][k]*b[k][j];
				tmp%=mod;
			}
			ret[i].push_back(tmp);
		}
	}
	return ret;
}

//modで行列をp乗する
//p=0の場合機能しない
V ruijou(V base,ll p,int mod)
{
	V beki[40];//2べきの行列
	beki[0]=base;
	for(int i=1;i<40;i++)
		beki[i]=mult(beki[i-1],beki[i-1],mod);
	
	V ret(base.size());
	bool f=true;
	for(int i=0;i<40;i++)
	{
		ll tmp=(1LL<<(ll)i);
		if((tmp&p)>0)
		{
			if(f)
			{
				ret=beki[i];
				f=false;
			}else{
				ret=mult(ret,beki[i],mod);
			}
		}
	}
	return ret;
}

int main()
{
	while(1)
	{
		int n,m,a,b,c;
		ll t;
		cin>>n>>m>>a>>b>>c>>t;
		if(n==0)
			break;
		vector<ll> s;//元の行列
		for(int i=0;i<n;i++)
		{
			ll tmp;
			cin>>tmp;
			s.push_back(tmp);
		}
		V tt(s.size());//累乗する行列を作る
		for(int i=0;i<s.size();i++)
		{
			for(int j=0;j<s.size();j++)
				tt[i].push_back(0);
		}
		//ABCを入れる
		for(int i=0;i<s.size();i++)
		{
			if(i>0)
				tt[i-1][i]=c;
			tt[i][i]=b;
			if(i<s.size()-1)
				tt[i+1][i]=a;
		}
		if(t>0)
		{
			tt=ruijou(tt,t,m);//行列累乗
			//debug
			/*
			for(int i=0;i<tt.size();i++)
			{
				for(int j=0;j<tt[i].size();j++)
					cout<<tt[i][j]<<" ";
				cout<<endl;
			}*/
			//最後に掛け算する
			for(int i=0;i<s.size();i++)
			{
				ll ans=0;
				for(int j=0;j<s.size();j++)
				{
					ans+=tt[i][j]*s[j];
					ans%=m;
				}
				if(ans<0)
					ans+=m;
				cout<<ans;
				if(i!=s.size()-1)
					cout<<" ";
			}
			cout<<endl;
		}else{
			for(int i=0;i<s.size();i++)
			{	
				cout<<s[i];
				if(i!=s.size()-1)
					cout<<" ";
			}
			cout<<endl;
		}
	}
	return 0;
}


Measured memory_bytes: 4164
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
N = int(input())
ans = 0
carry = 0
for n in range(N):
    a = int(input())
    if carry and a > 0:
        ans += 1
        a -= 1
    ans += a // 2
    if a % 2 == 1:
        carry = 1
    else:
        carry = 0
print(ans)


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

int main()
{
    vector<char> S(3);

    for (int i = 0; i < 3;i++){
        cin >> S.at(i);
    }
    if(S.at(0)==S.at(2)){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 5678;
char s[MAXN];
int dp[MAXN][MAXN];

int main() {
    int N;
    scanf("%d", &N);
    scanf("%s", s);
    for (int i = 1; i < N; i++) {
        for (int j = N - 1; j >= i; j--) {
            if (s[j - i] != s[j]) dp[j - i][j] = 0;
            else if (j == N - 1) dp[j - i][j] = 1;
            else {
                int tmp = dp[j - i + 1][j + 1];
                if (j - i + tmp >= j) dp[j - i][j] = tmp;
                else dp[j - i][j] = tmp + 1;
            }
            //printf("i = %d j = %d dp = %d\n", j - i, j, dp[j - i][j]);
        }
    }
    int ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            ans = max(ans, dp[i][j]);
        }
    }
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 112896
//
//  main.cpp
//  Code Festival 2017 Relay ProblemJ
//
//  Created by 范泽玥 on 2018/7/1.
//  Copyright © 2018年 范泽玥. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
int n;
int p[200005];
double ans;
int main(){
    cin>>n;
    for (int i=1;i<=2*n;i++){
        cin>>p[i];
    }
    sort(p+1,p+2*n+1);
    reverse(p+1,p+2*n+1);
    for (int i=1;i<=2*n-1;i++){
        ans+=(double)((double)(2*n-i))/((double)2*n-1)*(double)p[i];
    }
    printf("%.12f",ans);
    return 0;
}


Measured memory_bytes: 1024
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

/* Macros reg. ends here */

const ll INF = 1LL << 50;
static const long long mod = 1000000007;

int main() {
  cout << fixed << setprecision(15);

  ll n;
  cin >> n;

  ll cur = 26, len = 1;
  string ans = "";
  while(true){
    if(n <= cur){
      n--;
      do {
        ans += 'a' + (n % 26);
        n /= 26;
      } while(n > 0);
      while(len>(ll)ans.size()){
        ans += 'a';
      }
      break;
    }
    n -= cur;
    cur *= 26;
    len++;
  }

  Reverse(ans);
  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 3592