akhauriyash/Code-Regression huggingface dataset:
N = gets.chomp.to_i
S = gets.chomp

w = 0
b = 0
bstart = -1
bend = -1

x = 0
while x < N
  if S[x] == "."
    w += 1
    bend = -1
  else
    if bstart == -1
      bstart = x
    end
    if bend == -1
      bend = x
    end
    b += 1
  end
  x += 1
end

if bend == -1
  bend = N
end

if w == 0 || b == 0
  puts 0
  exit
end

w2 = w - bstart
#b2 = b - (N-bend)
#answer = [w2,b2].min
answer = w2
a = w2

x = bstart
while x < bend
  if S[x] == "#"
    a += 1
  else
    a -= 1
    if a < answer
      answer = a
    end
  end
  x += 1
end

puts answer

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

static const int N = 1000;

int lcs(string X, string Y) {
    int c[N + 1][N + 1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for (int i = 1; i <= m; i++) c[i][0] = 0;
    for (int j = 0; j <= n; j++) c[0][j] = 0;
    
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (X[i] == Y[j] ) {
                c[i][j] = c[i -1][j -1] + 1;
            } else {
                c[i][j] = max(c[i - 1][j], c[i][j -1]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    
    return maxl;
}

int main() {
    string s1, s2;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}


Measured memory_bytes: 7072
#include <stdio.h>
    int  main(void) {
        int a,b,x;
        scanf("%d %d %d",&x,&a,&b);
        if(a>=b){
            printf("delicious" );
        }else if(b-a<=x && a<=b){
            printf("safe");
        }else{
            printf("dangerous");
        }
        return 0;
    }

Measured memory_bytes: 128
import java.util.*;

public class Main {
    public static void main(String[] args) {
        int count = 0;
        Scanner stdIn = new Scanner(System.in);
        
        String s = stdIn.next();
        char c[] = s.toCharArray();
        
        for(int i = 0; i < c.length; i++){
            if(c[i] == 'x'){
                count++;
            }
        }
        
        if(count >= 8){
            System.out.println("NO");
        }else{
            System.out.println("YES");
        }
    }
}


Measured memory_bytes: 22612
n,m,c=map(int,input().split())
b=list(map(int,input().split()))
z=0
for _ in range(n):
    a=list(map(int,input().split()))
    s=0
    for j in range(m):
        s+=b[j]*a[j]
    if s+c>0:
        z+=1
print(z)

Measured memory_bytes: 38384
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000009;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-4;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int n;
int trans[3] = { 3,5,7 };
bool ok(int x) {
	if (n < x)return false;
	int cop = x;
	bool exi[3] = {};
	while (cop) {
		int r = cop % 10; cop /= 10;
		if (r == 3)exi[0] = true;
		else if (r == 5)exi[1] = true;
		else if (r == 7)exi[2] = true;
	}
	rep(i, 3) {
		if (!exi[i])return false;
	}
	return true;
}
int time3[10],time10[10];
void init() {
	time3[0] = 1;
	rep1(i, 9) {
		time3[i] = time3[i - 1] * 3;
	}
	time10[0] = 1;
	rep1(i, 9) {
		time10[i] = time10[i - 1] * 10;
	}
}
int calc(int n) {
	int res = 0;
	rep(i, time3[n]) {
		int cop = i; int x = 0;
		rep(j, n) {
			int r = cop % 3; cop /= 3;
			x += trans[r] * time10[j];
		}
		if (ok(x))res++;
	}
	return res;
}
int main() {
	init();
	cin >> n;
	int out = 0;
	Rep1(j, 3, 9) {
		out += calc(j);
	}
	cout << out << endl;
	return 0;
}

Measured memory_bytes: 256
s = input()
a = []
down = []
a_sum = 0
for i,line in enumerate(s):
    if line == "\\":
        down.append(i)
    elif line == "/" and down:
        j = down.pop()
        a_tmp = i-j
        a_sum += a_tmp
        while a and a[-1][0] > j:
            a_tmp += a.pop()[1]
        a.append([j,a_tmp])

print(a_sum)
print(len(a),*[s for i,s in a])

Measured memory_bytes: 8192
N,K = map(int, input().split())
p = []
for i in range(N):
    a,b = map(int, input().split())
    p.append((a,b))
    
sx = sorted(p)
ans = 10**50
for x1,y1 in sx:
    for x2,y2 in sx:
        if x2 <= x1:
            continue
        l = sx.index((x1, y1))
        r = sx.index((x2, y2))
        if r-l+1 < K:
            continue
        sy = sorted(sx[l:r+1],key=lambda coor: coor[1])
        for j in range(len(sy)-K+1):
            if sy[j][1] <= y1 and y2 <= sy[j+K-1][1]:
                ans = min(ans, (x2 - x1) * (sy[j+K-1][1]-sy[j][1])) 
print(ans)

Measured memory_bytes: 3188
import itertools

N = int(input())
L = list(map(int, input().split()))
cnt = 0
for tri in itertools.combinations(L, 3):
    if max(tri) < sum(tri) - max(tri) and tri[0] != tri[1] and tri[0] != tri[2] and tri[1] != tri[2]:
        cnt += 1
print(cnt)

Measured memory_bytes: 9120
//-----------------------
// author : xyqkoala
// time : 2020-07-01
//-----------------------

#include<iostream>
#include<vector>
#include<string>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std;

typedef long long LL;
const int MOD = 1e9+7;

void solve();


int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


void solve(){
	int n,m,k;
	cin>>n>>m>>k;
	vector<int> numsa(n,0);
	vector<int> numsb(m,0);
	for(int i=0;i<n;i++) cin>>numsa[i];
	for(int i=0;i<m;i++) cin>>numsb[i];
	vector<LL> sif(m+1,0);
	for(int i=0;i<m;i++){
		sif[i+1] = sif[i]+numsb[i];
	}
	int res = 0;
	LL sum = 0;
	for(int i=0;i<=n;i++){
		if(sum>k) break;
		int l = 0;
		int r = m+1;
		while(l<r){
			int mid = (r-l)/2+l;
			if(sif[mid]>(k-sum)) r = mid;
			else l = mid+1;
		}
		res = max(res,i+l-1);
		if(i==n) break;
		sum+=numsa[i];
	}
	cout<<res<<endl;
}


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



int main(){
  double power[8]={},sho[5];
  power[0] = 1;  sho[0] = 1;
  for(int i=1;i<8;i++){
    power[i] = power[i-1]*2;
  }
  for(int i=1;i<5;i++){
    sho[i] = sho[i-1]/2;
  }

  while(1){
    double n;
    cin >> n;
    if(n < 0) break;
    int ans[16]={};
    for(int i=0;i<8;i++){
      if(n >= power[7-i]){
	n -= power[7-i];
	ans[i]=1;
      }
    }
    for(int i=0;i<4;i++){
      if(n >= sho[i+1]){
	n -= sho[i+1];
	ans[i+8]=1;
      }
    }
    if(n != 0) cout << "NA\n";
    else{ 
      for(int i=0;i<12;i++){
	cout << ans[i];
	if(i == 7) cout << ".";
      }
      cout << "\n";
    }



  }
}

Measured memory_bytes: 1224
#include <bits/stdc++.h>
using namespace std;
 
 string arr;
long long  int dp[18][180][2];
int maxs; 
 
 
long long int back(int in, long long int sum, bool flag  ){
 	if(in> arr.size()) {  if(sum>maxs) maxs = sum;  return sum;  }
 	
 	if(dp[in][sum][flag]!=-1) return dp[in][sum][flag];
 	
 	 int limit = 9;
 	 if(!flag) limit=  arr[in-1]-'0';
 	
 	int res=0;
 	for(int i=0; i<=limit; ++i){
 		res+= back(in+1, sum+i, flag || i<limit);		
 	}
 	return dp[in][sum][flag]=  res;
 	
 }
 
 
 
int main() {

	long long int s1;
	
	string a;
	 cin>>a;
	 	maxs=-1;
		memset(dp, -1, sizeof dp);
	 	arr=a;
	 
	    s1 = back(1,0,false);
		
	 	cout<< (maxs)<<'\n';
	 	
	 
	
	return 0;
}


Measured memory_bytes: 3644
// finish date: 2018/10/14

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int (i)=a;(i)<(b);(i)++)
#define rep(i, n)  FOR(i,0,n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>>
        vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<vector<vector<double>>>
        vvvd;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, pair<int, int>> plii;
const int bigmod = 1000000007;
const int INF = 1050000000;
const long long INFll = 100000000000000000;

//最大公約数
int gcd(int a, int b) {
    if (a < 0) a = -a;
    if (b < 0) b = -b;
    if (a < b) {
        swap(a, b);
    }
    int r = 1; //0以外であれば何でも
    while (r != 0) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main() {
    int N, M;
    cin >> N >> M;
    string S, T;
    cin >> S >> T;
    ll L = (ll)N / gcd(N, M) * M;
    map<ll, char> X;
    rep(i, N) {
        X[i * (L / N)] = S[i];
    }
    rep(i, M) {
        if (X[i * (L / M)] != T[i] && X[i * (L / M)]!=X[222222222222222222]) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << L << endl;

    return 0;
}



Measured memory_bytes: 10880
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define uint unsigned long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}
int sigma(int n){
    int sum=0;
    for (int i=1; i<=(n); i++){
        sum+=i;
    }
    return sum;
}
int distance(int yo,int xo,int yi,int xi){
    return (yo-yi)*(yo-yi)+(xo-xi)*(xo-xi);
}
signed main(){
    int A1,A2,A3;
    cin >> A1 >> A2 >> A3;
    if((A1+A2+A3)>=22){
        cout << "bust" << endl;
        return 0;

    }
    else{
        cout << "win" << endl;
        return 0;

    }
}


Measured memory_bytes: 256
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
Formatter frm=new Formatter();
double r=scan.nextDouble();
double S=Math.PI*r*r*1.0;
double d=2*Math.PI*r*1.0;
System.out.println(String.format("%5f",S)+" "+String.format("%5f",d));
}}

Measured memory_bytes: 28180
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main(){

  int n,j,i,uncount=0,x,count=0;
  int *A;

  scanf("%d",&n);

  A = malloc(sizeof(int)*n);

  for(i=0;i<n;i++){
    scanf("%d",&A[i]);
  }

  for(i=0;i<n;i++){
    if(A[i]==1)uncount++;
    else if(A[i]==2||A[i]==3);
    else if(A[i]%2==0){
      uncount++;
    }
    else
      for(j=3;j<=sqrt(A[i]);j=j+2){
	if((A[i]%j)==0){
	  uncount++;
	  break;
	}
      }
  }


  count = n - uncount;

  printf("%d\n",count);

  free(A);
  return 0;
}

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

using lint = long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
template <typename T> using VVV = vector<vector<vector<T>>>;
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define repu(i, a, b, s) for(int i = int(a); i < int(b); i += int(s))
#define repd(i, a, b, s) for(int i = int(a); i > int(b); i += int(s))
#define endl "\n"
constexpr lint INF = 2e18;
constexpr lint MOD = 1e9 + 7;

void solve() {
    string L;
    cin >> L;

    lint N = L.size();
    V<lint> dp1(N, 0);
    V<lint> dp2(N, 0);

    dp1[0] = 2;
    dp2[0] = 1;

    repu(i, 1, N, 1) {
        if(L[i] == '0') {
            dp1[i] = dp1[i - 1];
            dp2[i] = dp2[i - 1] * 3;
        } else {
            dp1[i] = dp1[i - 1] * 2;
            dp2[i] = dp2[i - 1] * 3 + dp1[i - 1];
        }
        dp1[i] %= MOD;
        dp2[i] %= MOD;
    }

    lint ans = dp1[N - 1] + dp2[N - 1];
    ans %= MOD;

    cout << ans << endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
    solve();
}

Measured memory_bytes: 5028
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<string,int> psi;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a*b/gcd(a,b);
}


int main(void) {
    int n;
    cin >> n;
    vector<psi> in(n);
    rep(i,n) cin >> in[i].first >> in[i].second;
    sort(all(in));
    int m;
    cin >> m;
    int tmp = 0;
    bool ans = true;
    rep(i,m){
        string s;
        cin >> s;
        int a = upper_bound(all(in),psi(s,tmp)) - in.begin();
        if(a < n && in[a].first == s){
            tmp = in[a].second;
        }else{
            ans = false;
        }
    }
    if(ans) cout << "Yes" << endl;
    else cout << "No" << endl;
}



Measured memory_bytes: 12392
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;

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

    int N = scanner.nextInt();
    int K = scanner.nextInt();
    Sushi[] sushis = new Sushi[N];
    for (int i = 0; i < N; i++) sushis[i] = new Sushi(scanner.nextInt(), scanner.nextInt());

    Arrays.sort(sushis);
    reverse(sushis);

    HashSet<Integer> tset = new HashSet<>();
    boolean[] first = new boolean[N];
    for (int i = 0; i < N; i++) {
      if (!tset.contains(sushis[i].t)) {
        first[i] = true;
        tset.add(sushis[i].t);
      }
    }

    long sum = 0;
    long tt = 0;
    PriorityQueue<Sushi> pq = new PriorityQueue<>();
    for (int i = 0; i < K; i++) {
      sum += sushis[i].d;
      if (first[i]) {
        sum += tt * 2 + 1;
        tt++;
      } else {
        pq.add(sushis[i]);
      }
    }
    long max = sum;
    for (int i = K; i < N && !pq.isEmpty(); i++) {
      if (first[i]) {
        sum += sushis[i].d - pq.poll().d + tt * 2 + 1;
        max = Math.max(max, sum);
        tt++;
      }
    }
    System.out.println(max);
  }

  private static class Sushi implements Comparable<Sushi> {
    public final int t;
    public final int d;

    private Sushi(int t, int d) {
      this.t = t;
      this.d = d;
    }

    @Override public int compareTo(Sushi sushi) {
      return this.d - sushi.d;
    }
  }

  private static void reverse(Sushi[] list) {
    int i = 0;
    int j = list.length - 1;
    while (i < j) {
      Sushi tmp = list[i];
      list[i] = list[j];
      list[j] = tmp;
      i++;
      j--;
    }
  }
}


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

#define For(i,a,b) for(int (i) = (a);(i) < (b);i++)
#define Rep(i,n) For((i),0,(n))
#define All(a) (a).begin(),(a).end()
#define sp " "
#define INF 1e18
#define INT_INF 1e9
#define MAX 1000

typedef long long ll;
const ll MOD = 1000000007;
const ll MOD_9 = 998244353;

int main(){
    int N,M;
    cin >> N >> M;
    cout << N*M << endl;
}


Measured memory_bytes: 3084
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=4010;
int dp[N][N],v[N][N];
char a[N],b[N];
void print_path(int i,int j)
{
//	printf("i:%d j:%d\n",i,j); 
    if(!i && !j) return;
    if(v[i][j]==0)
    {
        print_path(i-1,j-1);
        putchar(a[i-1]);
    }
    else if(v[i][j]==-1)
    {
        print_path(i-1,j);
//        putchar(a[i-1]);
    }
    else
    {
        print_path(i,j-1);
//        putchar(b[j-1]);
    }
}
int main()
{
	scanf("%s%s",a,b); 
    memset(dp,0,sizeof(dp));
    int la=strlen(a),lb=strlen(b);
    for(int i=1;i<la;i++) v[i][0]=-1;
    for(int i=1;i<lb;i++) v[0][i]=1;
    for(int i=1;i<=la;i++)
    {
        for(int j=1;j<=lb;j++)
        {
            if(a[i-1]==b[j-1])
            {
                v[i][j]=0;
                dp[i][j]=dp[i-1][j-1]+1;
            }
            else
            {
                if(dp[i][j-1]>dp[i-1][j])
                {
                    v[i][j]=1;
                    dp[i][j]=dp[i][j-1];
                }
                else
                {
                    v[i][j]=-1;
                    dp[i][j]=dp[i-1][j];
                }
            } 
        }
    }
    if(dp[la][lb])
    print_path(la,lb);
//        puts("");
    return 0;
}

Measured memory_bytes: 111616
#include <bits/stdc++.h>  // ver1.9.4
#define int long long
#define ALL(v) (v).begin(),(v).end()
#define Vi vector<int>
#define pb push_back
#define mp make_pair
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define Vout(a) rep(i,0,a.size()-1)cout<<a[i]<<' ';cout<<a[a.size()-1]<<endl;
#define INF 9000000000000000000  //  9.0 * 10^18
#define MOD 1000000007  //  10^9 + 7
using namespace std;

signed main() {
  cout << fixed << setprecision(15);
  int n;
  cin >> n;
  Vi a(n);
  bool zero=false;
  int minus = 0,sum=0;
  
  rep(i,0,n){
    cin >> a[i];
    if(a[i]==0) zero = true;
    if(a[i]<0) {
      minus++;
      a[i] *= -1;
    }
    sum += a[i];
  }
  if(zero||minus%2==0) {cout<<sum<<endl;return 0;}
  sort(ALL(a));
  sum -= 2*a[0];
  cout << sum << endl;
  
  return 0;
}


Measured memory_bytes: 1024
# -*- coding: utf-8 -*-
# E
# 文字列比較をローリングハッシュを用いると高速

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

n = int(input())
s = input()[:-1]
# s = list(map(ord, list(input()[:-1])))
# print(s)

lb = 0
ub = n//2 + 1


def check(len):
    for l1 in range(n-len):
        for l2 in range(l1+len, n-len+1):
            if s[l1:l1+len] == s[l2:l2+len]:
                return True
    return False


mod = 10**9+7
base = 1234
power = [1]*(n+1)
for i in range(1, n+1):
    power[i] = power[i-1]*base%mod


def check2(m):
    res = 0
    for i in range(m):
        res+=s[i]*power[m-i-1]
        res%=mod

    dic = {res: 0}
    # defaultdictが早い
    # dic = defaultdict(int)
    # dic[res] = 0

    for i in range(n-m):
        res = ((res-s[i]*power[m-1])*base
                             +s[i+m])%mod
        if res in dic.keys():
            index = dic[res]
            if index +m<=i+1:
                return True
        else:
            dic[res] = i+1
    return False


def check3(m):
    dic = {}
    for i in range(n-m+1):
        s_ = s[i:i+m]
        if s_ in dic.keys():
            if dic[s_]+m<=i:
                return True
        else:
            dic[s_] = i
    return False


while ub > lb+1:
    x = (lb + ub)//2
    if check3(x):
        lb = x
    else:
        ub = x
    # print(lb, ub)


'''
print(3, check(3))
print(4, check(4))
print(5, check(5))
'''

print(lb)

Measured memory_bytes: 10056
#include <bits/stdc++.h>
using namespace std;
//constant
const long long MOD = 1e9+7;
//const long long MOD = ;
const long long INF_LL = 1e18+3; const int INF_int = 1e9;
//Macro
#define ll long long
#define rep(i,n) for(int i = 0;i<n;i++)
#define lrep(i,n) for(long long i = 0;i<n;i++)
#define All(a) a.begin(),a.end()
//Debuging tools
#define Debug_Output_ALL(a) for(auto itr = a.begin();itr!=a.end();itr++) cout << *itr << " ";cout << endl;
///library
//@brief x^n
//@detail if MOD==0 then not modulo. 
template <typename T>
T power(T x,T n,long long MOD = MOD){
    T ret = 1;
    while(n>0){
        if(n%2==1) ret*=x;
        n/=2,x*=x;
        if(MOD!=0) x%=MOD,ret%=MOD;
    }
    return ret;
}

//@brief a^-1 (mod=MOD)
template <typename T>
T modinv(T a,long long MOD = MOD){
    return power(a,MOD-2,MOD);
}

//@brief nCk
std::vector<long long> factorial,factorial_inv;
void makefactorial(long long n,long long MOD = MOD){
    factorial.push_back(1LL);
    for(long long i = 1;i<=n;i++) factorial.push_back(factorial[i-1]*i),factorial[i]%=MOD;
    for(long long i = 0;i<=n;i++) factorial_inv.push_back(modinv(factorial[i],MOD));
    return;
}
template <typename T>
T combi(T n,T k,long long MOD = MOD){
    T ret = factorial[n]*factorial_inv[k];
    ret%=MOD;
    ret*=factorial_inv[n-k];
    return ret%=MOD;
}
//DataStructure
//MainFunc
int main(){
    long long n,k;
    cin >> n >> k;
    makefactorial(n);
    long long ans = 0;
    lrep(i,min(n,k+1)){
        long long tmp = combi(n-1,i)*combi(n,i);
        ans+=(tmp%MOD);
        ans%=MOD;
    }
    cout << ans << endl;
}


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

int main() {
	int n;
	cin>>n;
	vector<int> x(n);
	
	
	for(int i=0;i<n;i++){
		cin>>x[i];
	}
	
	ll ans=2000000000;
	
	for(int i=1;i<=100;i++){//ｐが1から100で全探索
		ll sum=0;
		for(int j=0;j<n;j++){
			sum+=(x[j]-i)*(x[j]-i);
		}
		ans=min(sum,ans);
	}
	
	cout<<ans<<endl;
}

//模範解答と同じに見えるがRE


Measured memory_bytes: 256
n,x,y,t;
main(){
    char a[101],b[101];
    for(scanf("%d",&n);n--;){
        scanf("%s%s",a,b);
        t=strcmp(a,b);
        x+=t>0?3:t==0?1:0;
        y+=t<0?3:t==0?1:0;
    }
    printf("%d %d\n",x,y);
}

Measured memory_bytes: 1792
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader input =new BufferedReader (new InputStreamReader (System.in));
		String str = input.readLine( );
		String[] xy = str.split(" ");
		int x = Integer.parseInt(xy[0]);
		int y = Integer.parseInt(xy[1]);
		int answer = 0;

		if(x<y){
			if(0<=x){
				answer = y - x;
			}else if(0<y){
				answer = Math.abs(Math.abs(y) - Math.abs(x)) + 1;
			}else{
				answer = y - x;
			}
		}else{
			if(0<y){
				answer = x - y + 2;
			}else if(0<=x){
				answer = Math.abs(Math.abs(x) - Math.abs(y)) + 1;
			}else{
				answer = x - y + 2;
			}
		}

		System.out.println(answer);

	}
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

vector<ll> divisor(ll n){
  vector<ll> res;
  for(ll i=1;i*i<=n;i++){
    if(n%i==0){
      res.push_back(i);
      if(i!=n/i)res.push_back(n/i);
    }
  }
  return res;
}

ll N,K;
ll A[500];

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

  cin>>N>>K;
  rep(i,N)cin>>A[i];

  ll sum=0;
  rep(i,N)sum+=A[i];

  vector<ll> ds=divisor(sum);

  ll res=0;
  for(ll d : ds){
    ll add=0;
    vector<ll> vs;
    rep(i,N){
      add+=((A[i]+d-1)/d)*d - A[i];
      vs.push_back(((A[i]+d-1)/d)*d - A[i]);
    }
    if(add%d==0){
      ll cnt=N-(add/d);
      sort(all(vs));
      ll tot=0;
      rep(i,cnt)tot+=vs[i];
      if(tot<=K)maxch(res,d);
    }
  }
  cout<<res<<endl;

  return 0;
}


Measured memory_bytes: 256
require 'pp'

# 空白区切の入力値を数値の配列で返却する
def gets_i_list()
  gets.chomp.split(" ").map(&:to_i)
end

A, B, C, D = gets_i_list

tt, ta = A, B
at, aa = C, D

while true do
  at = at - ta
  if at <= 0
    puts "Yes"
    exit
  end
    
  tt = tt - aa
  if tt <= 0
    puts "No"
    exit
  end
end

Measured memory_bytes: 14496
import java.util.Collections;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main{
	public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int i=1; i<=n; i++){
			h[i] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2] - h[1]);
		for(int i=3; i<=n; i++){
			dp[i] = (int)Math.min(dp[i-1] + Math.abs(h[i] - h[i-1]) , dp[i-2] + Math.abs(h[i]-h[i-2]));
		}
		//System.out.println(Arrays.toString(h));
		//System.out.println(Arrays.toString(dp));
		System.out.println(dp[n]);
	}

	static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	}
}

class Reader { 
	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tok = new StringTokenizer("");
	static String next() {
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		}
		return tok.nextToken();
	} 
	static int nextInt() {
		return Integer.parseInt(next());
	}
}


Measured memory_bytes: 42992
N = int(input())
A = list(map(int, input().split(" ")))
num_of_swp = 0

for i in range(N):
  mini = i

  for j in range(i + 1, N):
    if A[j] < A[mini]: mini = j

  if i != mini:
    A[i], A[mini] = A[mini], A[i]
    num_of_swp += 1

print(" ".join(map(str, A)))
print(num_of_swp)

Measured memory_bytes: 7660
import math
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
input = sys.stdin.readline
N,K = map(int,input().split())
ans = 0
if K == 0:
    print(N**2)
else:
    for b in range(K+1,N+1):
        chk1 = N%b
        chk2 = N//b
        ans += (b-K)*chk2+max(0,chk1-K+1)
    print(ans)

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

const int INF = 100100100;
const int MOD = (int)1e9 + 7;
typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)


int main() {
  int n,d;
  cin >> n >> d;
  d = 2*d + 1;
  int ans = n/d;
  if (n%d!=0) ans++;
  cout << ans << endl;
}

Measured memory_bytes: 3608
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#define int long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
#ifdef i_am_noob
#define debug(a) print1(a)
#else
#define debug(a) 
#endif
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
 
using namespace std;
using namespace __gnu_pbds;
 
const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod2;
 
inline int add(int a, int b){return (a+b>=MOD?a+b-MOD:a+b);}
inline int sub(int a, int b){return (a+MOD-b>=MOD?a-b:a+MOD-b);}
int Pow(int a, int b){
	if(!b) return 1;
	int tmp=Pow(a*a%MOD,b>>1);
	return (b&1?tmp*a%MOD:tmp);
}
inline int inverse(int n){return Pow(n,MOD-2);}
 
const int maxn=200005;
//i_am_noob
int a;

vector<int> prime;
bool isprime[maxn];

void find_prime(){
	isprime[0]=isprime[1]=false;
	rep2(i,2,maxn) isprime[i]=true;
	rep2(i,2,maxn){
		if(isprime[i]){
			prime.pb(i);
			int tmp=(maxn-1)/i+1;
			rep2(j,i,tmp) isprime[i*j]=false;
		}
	}
}
bool is_prime(int n){//find_prime first,n<maxn^2
	if(n<maxn) return isprime[n];
	rep(maxn){
		if(1ll*prime[i]*prime[i]>n) break;
		if(!(n%prime[i])) return false;
	}
	return true;
}

signed main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	find_prime();
	cin >> a;
	while(1){
		if(isprime[a]){
			print1(a);
			return 0;
		}
		a++;
	}
	return 0;
}

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

#define ll long long

ll nC2(int x){
    return ((ll)(x) * (ll)(x-1)) / 2;
}

int arr[200010];
char str[200010];
int cnt[10010];
int modinv10[200010];

int main()
{
    int i, j, n, p;
    ll ans = 0;
    scanf("%d%d", &n, &p);
    scanf("%s", str+1);
    if(p==2 || p==5){
        for(int i=1; i<=n; ++i){
            int x = str[i]-'0';
            if(x%p == 0) ans += (ll)i;
        }
        printf("%lld\n", ans);
        return 0;
    }
    int inv10;
    for(inv10=1;inv10<p;++inv10){
        if(inv10*10 % p == 1) break;
    }
    modinv10[0] = 1;
    for(int i=1; i<=n; ++i) modinv10[i] = (modinv10[i-1]*inv10)%p;
    ++cnt[0];
    int bef = 0;
    for(int i=1; i<=n; ++i){
        int x = str[i]-'0';
        bef = (bef*10 + x)%p;
        ++cnt[(bef*modinv10[i])%p];
    }
    for(int i=0; i<p; ++i) ans += nC2(cnt[i]);
    printf("%lld\n", ans);
}

Measured memory_bytes: 1280
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
#define for1(i,n) for (ll i=0;i<(n);i++)
#define for2(i,m,n) for (ll i=(m);i<(n);i++)
#define for3(i,m,n,d) for (ll i=(m);i<(n);i+=(d))
#define DEBUG 0
template <class T>
void dumplist(T list)
{
    for (auto item : list) {
        cout << item << " ";
    }
    cout << endl;
}

// 1000000007 で割ったあまりを扱う構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
const int MOD = 1000000007;
using mint = Fp<MOD>;


void solve()
{
    string S;
    cin >> S;
    ll le = S.length();
    mint ans = 0;
    vector<ll> csum(le+1, 0);
    ll c = 0;
    for1(i,le) {
        if (S[i] == '1') {
            ans += modpow(mint(2), c) * modpow(mint(3), le-i-1);
            c += 1;
        }
    }
    ans += modpow(mint(2), c);

    cout << ans << endl;
}

int main()
{
    do {
        solve();
    } while (DEBUG);
}


Measured memory_bytes: 1280
from math import factorial
n = int(input())
ans_list = []
num_list = []
for i in range(n):
    s = list(input())
    s.sort()
    ans_list.append("".join(s))
ans_list.sort()
count = 1
for i in range(n-1):
    if ans_list[i] == ans_list[i+1]:
        count += 1
    else:
        num_list.append(count)
        count = 1
num_list.append(count)
ans = 0
for i in range(len(num_list)):
    if num_list[i] != 1:
        ans += factorial(num_list[i]) // factorial(2) // factorial(num_list[i]-2)
print(ans)


Measured memory_bytes: 11392
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))

l = []
g = 0
for i in range(n):
  tmp = gcd(g, a[i])
  if tmp < g or g == 0:
    l.append(i)
  g = tmp

ans = 0
for i in l:
  g = 0
  for j in range(n):
    if i != j:
      g = gcd(g, a[j])
  ans = max(ans, g)
print(ans)

Measured memory_bytes: 16136
import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int X = sc.nextInt();
		int Y = sc.nextInt();
		int Z = sc.nextInt();
		int buf;
		buf = X;
		X = Y;
		Y = buf;
		buf = X;
		X = Z;
		Z = buf;
		System.out.print(X + " ");
		System.out.print(Y + " ");
		System.out.println(Z);
	}
}

Measured memory_bytes: 22100
#include <stdio.h>

int main()
{
    int a, b;
    
    scanf("%d %d", &a, &b);
    
    if (a == 0) {
        if (1868 <= b && b <= 1911) {
            printf("M");
            printf("%d\n", b - 1867);
        } else if (1912 <= b && b <= 1925) {
            printf("T");
            printf("%d\n", b - 1911);
        } else if (1926 <= b && b <= 1988) {
            printf("S");
            printf("%d\n", b - 1925);
        } else if (1989 <= b) {
            printf("H");
            printf("%d\n", b - 1988);
        }
    } else if (a == 1) {
        printf("%d\n", b + 1867);
    } else if (a == 2) {
        printf("%d\n", b + 1911);
    } else if (a == 3) {
        printf("%d\n", b + 1925);
    } else if (a == 4) {
        printf("%d\n", b + 1988);
    }
    
    return 0;
}

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

struct node{
  struct node *prev;
  struct node *next;
  int key;
};

typedef struct node *NodePointer;
NodePointer nil;

NodePointer search(int);
void insert(int);
void deleteN(NodePointer);
void delete(int);
void deleteF(void);
void deleteL(void);

int main(){
  int i;
  int  n,k,flag=1;
  char input[15];
  NodePointer x;

  nil=malloc(sizeof(struct node));
  nil->prev=nil;
  nil->next=nil;

  scanf("%d",&n);
  
  for(i=0;i<n;i++)
    {
      scanf("%s",input);
      if(strcmp(input,"insert")==0)
	{
	  scanf("%d",&k);
	  insert(k);
	}
      else if(strcmp(input,"delete")==0)
	{
	  scanf("%d",&k);
	  delete(k);	  
	}
      else if(strcmp(input,"deleteFirst")==0)
	{
	  deleteF(); 	  
	}
      else if(strcmp(input,"deleteLast")==0)
	{
	  deleteL(); 	  
	}
    }
  
  x=nil->next;
  
  while(1){
    if(x==nil) break;
    if(flag==0) printf(" ");
    printf("%d",x->key);
    x=x->next;
    flag=0;
  }
  printf("\n");
  
  return 0;
}

NodePointer search(int k){
  NodePointer x;

  x=nil->next;
  while(x!=nil && x->key!=k){
    x=x->next;
  }

  return x;
}

void insert(int k){
  NodePointer x;

  x=malloc(sizeof(struct node));
  x->key=k;
  x->next=nil->next;
  nil->next->prev=x;
  nil->next=x;
  x->prev=nil;
}

void deleteN(NodePointer x){
  x->prev->next=x->next;
  x->next->prev=x->prev;
}

void delete(int k){
  NodePointer x;

  x=search(k);
  if(x==nil) return;
  deleteN(x);
}

void deleteF(){
  NodePointer x;

  x=nil->next;
  deleteN(x);
}

void deleteL(){
  NodePointer x;

  x=nil->prev;
  deleteN(x);
}



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

using namespace std;
#define ll long long

#define rep(i, a) for (int i = 0; i < (int) (a); i++)
#define reps(i, a, b) for (int i = (int) (a); i < (int) (b); i++)
#define rrep(i, a) for (int i = (int) a-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int) (a)-1; i >= (int) (b); i--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x

#ifdef LOCAL
#define lcout(a) cout << a;
#define lcoutln(a) cout << a << endl;
#define lcerr(a) cerr << a;
#define lcerrln(a) cerr << a << endl;
#else
#define lcout(a) 
#define lcoutln(a) 
#define lcerr(a) 
#define lcerrln(a) 
#endif

#define INF 1e9

int H, W;
int mi;

void culc()
{
	if (H % 3 == 0) {
		mi = 0;
		return;
	} else mi = W;
	if (W % 3 == 0) {
		mi = 0;
		return;
	} else mi = min(mi, H);
	if (W % 3 == 1) {
		if (H % 2 == 0) mi = min(mi, H / 2);
		else mi = min(mi, W / 3 + H / 2 + 1 - min(0, H / 2 - W / 3));
	}
	swap(H, W);
	if (W % 3 == 1) {
		if (H % 2 == 0) mi = min(mi, H / 2);
		else mi = min(mi, W / 3 + H / 2 + 1 - min(0, H / 2 - W / 3));
	}
	if (W % 3 == 2) {
		if (H % 2 == 0) mi = min(mi, H / 2);
		else mi = min(mi, max((W / 3) * 2 + 1, H / 2 + W / 3 + 1));
	}
	swap(H, W);
	if (W % 3 == 2) {
		if (H % 2 == 0) mi = min(mi, H / 2);
		else mi = min(mi, max((W / 3) * 2 + 1, H / 2 + W / 3 + 1));
	}
}

int main()
{
	cin >> H >> W;
	culc();
	cout << mi << endl;
}


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

int main(){
	int a, b, t = 0, n = 0;
	double s = 0;
	while(scanf("%d,%d", &a, &b) != EOF){t += a*b;s += b;n++;}
	printf("%d\n%d\n", t, (int)(s/n+0.5));
	return 0;
}

Measured memory_bytes: 3192
def main():
    cnt = 0
    for _ in range(m):
        x, y = map(int, input().split())
        unite(x, y)
    for i in range(1, n+1):
        if same(i, p[i]):
            cnt += 1
    print(cnt)
        
def root(x):
    if par[x] == x:
        return x
    par[x] = root(par[x])
    return par[x]

def same(x, y):
    rx = root(x)
    ry = root(y)
    return rx == ry

def unite(x, y):
    x = root(x)
    y = root(y)
    if x != y:
        par[x] = y

if __name__ == "__main__":
    n, m = map(int, input().split())
    p = [0] + list(map(int, input().split()))
    par = list(range(n+1))
    main()


Measured memory_bytes: 125548
#include <stdio.h>

int main() {
	int a[4][5], b[4][5];
	int x, y, dir, i, j;
	
	// get the wall data from input
	// 'a' is the data about the vertical side wall
	// 'b' is the data about the side wall
	for(i=0; i<4+5; i++) {
		if ((i%2)==0) {
			for(j=0; j<4; j++) {
				scanf("%1d", &a[j][i/2]);
			}
		}else {
			for(j=0; j<5; j++) {
				scanf("%1d", &b[(i-1)/2][j]);
			}
		}
	}
	
	// set first position
	x = 1;
	y = 0;
	dir = 0;

	// main loop
	while(1) {
		
		switch(dir) {
		case 0: //right
			// if there isnt the right-hand wall, turn right
			if (a[x-1][y]==0) {
				dir=1;
				y+=1;
				break;
			}
			// prevention of buffer-overflow (y>=1)
			if (y==0) x+=1;
			else if (b[y-1][x]==1){
				// if there is the front wall, turn left
				dir=3;
				printf("R");
				break;
			}else x+=1;
			printf("R");
			if (x==5) {
				// if x is 5, turn right
				dir=1;
				y+=1;
			}else if (a[x-1][y]==0) {
				// if there isnt the right-hand wall, turn right
				dir=1;
				y+=1;
			}
			break;
		case 1: //down
			if (b[y-1][x-1]==0) {
				dir=2;
				x-=1;
				break;
			}
			if (x==5) y+=1;
			else if (a[x-1][y]==1) {
				dir=0;
				printf("D");
				break;
			}else y+=1;
			printf("D");
			if (y==5) {
				dir=2;
				x-=1;
			}else if (b[y-1][x-1]==0) {
				dir=2;
				x-=1;
			}
			break;
		case 2: //left
			if (a[x-1][y-1]==0) {
				dir=3;
				y-=1;
				break;
			}
			if (y==5) x-=1;
			else if (b[y-1][x-1]==1) {
				dir=1;
				printf("L");
				break;
			}else x-=1;
			printf("L");
			if (x==0) {
				dir=3;
				y-=1;
			}else if (a[x-1][y-1]==0) {
				dir=3;
				y-=1;
			}
			break;
		case 3: //up
			if (b[y-1][x]==0) {
				dir=0;
				x+=1;
				break;
			}
			if (x==0) y-=1;
			else if (a[x-1][y-1]==1) {
				dir=2;
				printf("U");
				break;
			}else y-=1;
			printf("U");
			if (y==0) {
				dir=0;
				x+=1;
			}else if (b[y-1][x]==0) {
				dir=0;
				x+=1;
			}
			break;
		}
		
		// you are at goal?
		if ( (x==1)&&(y==0) ) break;
		if ( (x==0)&&(y==0) ) break;
		if ( (x==0)&&(y==1) ) break;
	}
	printf("\n");
	
	return 0;
}

Measured memory_bytes: 0
x, k, d = list(map(lambda x: int(x), input().split()))


x = abs(x)
min_pos = x % d
min_neg = min_pos - d

num_to_min_pos = x // d
if k <= num_to_min_pos:
    print(x - d * k)
    
else:
    if (k - num_to_min_pos) % 2 == 0:
        print(min_pos)
    else:
        print(abs(min_neg))

Measured memory_bytes: 9108
//#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int H, W;
int C[10][10];

int main(){
  scanf("%d%d", &H, &W);
  for (int i = 0; i < 10; i++){
    for (int j = 0; j < 10; j++){
      scanf("%d", &C[i][j]);
    }
  }
  for (int k = 0; k < 10; k++){
    for (int i = 0; i < 10; i++){
      for (int j = 0; j < 10; j++){
        C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < H * W; i++){
    int A; scanf("%d", &A);
    if (A == -1) continue;
    ans += C[A][1];
  }
  printf("%d\n", ans);

  return 0;
}


Measured memory_bytes: 3820
def main():
    n=int(input())
    a=sorted(map(int,input().split()),reverse=True)
    ans = a[0]
    c = 1
    for i in a[1:]:
        if c >= n-1:
            break
        c += 1
        ans += i
        if c >= n-1:
            break
        c += 1
        ans += i
    print(ans)

if __name__ == '__main__':
    main()

Measured memory_bytes: 105504
import math

n = int(input())
x = [int(x) for x in input().split()]
y = [int(y) for y in input().split()]

man = 0
for i in range(n):
    man += math.sqrt((x[i]-y[i])**2)
print(f'{man:.6f}')

euc = 0
for i in range(n):
    euc += (x[i]-y[i])**2
Euc = math.sqrt(euc)
print(f'{Euc:.6f}')

n3 = 0
for i in range(n):
    n3 += (math.sqrt((x[i]-y[i])**2))**3
N3 = n3**(1/3)
print(f'{N3:.6f}')

che = []
for i in range(n):
    che.append(math.sqrt((x[i]-y[i])**2))
Che = max(che)
print(f'{Che:.6f}')


Measured memory_bytes: 5752
import sys
input=sys.stdin.readline
from collections import deque

def main():
    n,m=map(int,input().split())
    Edges=[[] for _ in range(n)]
    for _ in range(m):
        a,b=map(lambda x: int(x)-1,input().split())
        Edges[a].append(b)
        Edges[b].append(a)
    Ans=[0]*n
    Ans[0]=-1
    q=deque()
    q.append(0)
    while q:
        fr=q.popleft()
        for to in Edges[fr]:
            if Ans[to]==0:
                Ans[to]=fr+1
                q.append(to)
    print('Yes')
    print(*Ans[1:],sep='\n')
    
if __name__=='__main__':
    main()

Measured memory_bytes: 94176
#include<iostream>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;

struct node
{
	long long num;
	int id;
};

int n;
long long a[100011];
node p[100011];
long long ans[100011];


bool cmp1(node xx,node yy)
{
	if(xx.num!=yy.num)
	{
		return xx.num>yy.num;
	}
	return xx.id<yy.id;
}

long long tr1[100011],tr2[100011];
long long res[100011];


int lb(int x)
{
	return x&(-x);
}

void add(long long t[],int pos,long long val)
{
	int i;
	for(i=pos;i<=n;i+=lb(i))
	{
		t[i]+=val;
	}
	return ;
}
long long get(long long t[],int pos)
{
	int i;
	long long ans=0;
	for(i=pos;i>0;i-=lb(i))
	{
		ans+=t[i];
	}
	return ans;
}


int main()
{
	int i,j,k;
	long long sum=0;
	int xx,yy;
	int pre=-1;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		p[i].num=a[i];
		p[i].id=i;
		sum+=p[i].num;
	}
	
	sort(p+1,p+n+1,cmp1);
	memset(tr1,0,sizeof(tr1));
	memset(tr2,0,sizeof(tr2));
	memset(res,0,sizeof(res));
	for(i=1;i<=n;i++)
	{
		long long tmp=get(tr1,n)-get(tr1,p[i].id);
		long long num=get(tr2,n)-get(tr2,p[i].id);
		//cout<<get(tr1,p[i].id)<<' '<<get(tr2,p[i].id)<<endl;
		ans[p[i].id]=tmp-num*p[i].num;
		
		
		//cout<<p[i].num<<' '<<p[i].id<<' '<<tmp<<' '<<num<<endl;
		
		long long has=get(tr2,p[i].id);
		if(has==0)
		{
			if(pre!=-1)
			{
				res[pre]=-ans[pre]+ans[p[i].id];
			}
			
			pre=p[i].id;
		}
		
		add(tr1,p[i].id,p[i].num);
		add(tr2,p[i].id,1);
		
		//cout<<get(tr1,p[i].id)<<' '<<get(tr2,p[i].id)<<endl;
	}
	res[pre]=sum-ans[pre];
	
	for(i=1;i<=n;i++)
	{
		printf("%lld\n",res[i]);
	}
	
	return 0;
} 

Measured memory_bytes: 6528
use std::process;
macro_rules! get {
      ($t:ty) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.trim().parse::<$t>().unwrap()
          }
      };
      ($($t:ty),*) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              let mut iter = line.split_whitespace();
              (
                  $(iter.next().unwrap().parse::<$t>().unwrap(),)*
              )
          }
      };
      ($t:ty; $n:expr) => {
          (0..$n).map(|_|
              get!($t)
          ).collect::<Vec<_>>()
      };
      ($($t:ty),*; $n:expr) => {
          (0..$n).map(|_|
              get!($($t),*)
          ).collect::<Vec<_>>()
      };
      ($t:ty ;;) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.split_whitespace()
                  .map(|t| t.parse::<$t>().unwrap())
                  .collect::<Vec<_>>()
          }
      };
      ($t:ty ;; $n:expr) => {
          (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
      };
}
fn main() {
    let s = get!(String);
    let t = get!(String);
    let mut  ans = 0;

    for i in 0..s.len() {
        if  s.chars().nth(i).unwrap() == t.chars().nth(i).unwrap() {
                ans += 1;
        }
    }

        println!("{}" , ans);

}


Measured memory_bytes: 4352
#include<stdio.h>
int Merge(int *,int,int,int);
int MergeSort(int *,int,int);
int sum=0;
int main()
{
  int i,n;
  int S[500000];

  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&S[i]);
  }
  
  MergeSort(S,0,n);

  for(i=0;i<n-1;i++){
    printf("%d ",S[i]);
  }
  printf("%d\n",S[i]);
  printf("%d\n",sum);
  return 0;
}

int MergeSort(int *A,int left,int right)
{  
  int mid;
  if(left+1<right){
    mid=(left+right)/2;
    MergeSort(A,left,mid);
    MergeSort(A,mid,right);
    Merge(A,left,mid,right);
  }
  else return 0;
}

int Merge(int *A,int left,int mid,int right)
{
  int k,n1,n2,L[500000],R[500000],i,j;
  n1=mid-left;
  n2=right-mid;
  for(i=0;i<n1;i++){
    L[i]=A[left+i];
  }
  for(i=0;i<n2;i++){
    R[i]=A[mid+i];
  }
  L[n1]=999999999;
  R[n2]=999999999;
  i=0;
  j=0;
  for(k=left;k<right;k++){
    if(L[i]<=R[j]){
      A[k]=L[i];
      i++;
      sum++;
    }
    else{
      A[k]=R[j];
      j++;
      sum++;
    }
  }
}

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

int main() {
  int m;
  int c_500, c_5;
  
  std::cin >> m;
  c_500 = m / 500;
  m -= c_500*500;
  c_5 = m / 5;

  int p = c_500*1000 + c_5*5;

  std::cout << p;

}

Measured memory_bytes: 3624