akhauriyash/Code-Regression huggingface dataset:
//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
struct UF{
	int par[2000005],ran[2000005];
	void init(){ for(int i=0;i<2000005;i++) par[i] = i, ran[i] = 0; }
	int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); }
	void unite(int x,int y){
		x = find(x); y = find(y); if(x==y) return;
		if(ran[x] < ran[y]) par[x] = y;
		else{
			par[y] = x;
			if(ran[x] == ran[y]) ran[x]++;
		}
	}
	bool same(int x,int y){ return find(x)==find(y); }
}uf;


ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
ll F[2000005],R[2000005];
void make(){
	F[0] = 1;
	for(int i=1;i<2000005;i++) F[i] = F[i-1]*i%mod;
	R[2000004] = modpow(F[2000004], mod-2);
	for(int i=2000003;i>=0;i--) R[i] = R[i+1] * (i+1) % mod;
}
ll C(int a,int b){
	return F[a]*R[b]%mod*R[a-b]%mod;
}
int n;
vector<int>edge[200005];

void solve(){
	cin >> n;
	ll ans = 0;
	repn(i, n) ans += 1LL * i * (i+1) / 2;
	rep(i, n-1){
		int a, b; cin >> a >> b;
		ans -= 1LL * min(a, b) * (n+1-max(a, b));
	}
	cout << ans << endl;
}

int main(){
	uf.init();
	make();
	solve();
}

Measured memory_bytes: 55148
#include <iostream>
#include <vector>

int main(){
 int N,K;
 std::cin>>N>>K;
 std::vector<int> flag(N,1);
 for(int i=0;i<K;i++){
  int di;
  std::cin>>di;
  for(int j=0;j<di;j++){
   int a;
   std::cin>>a;
   flag[a-1]=0;
  }
 }
 int ans=0;
 for(int i=0;i<N;i++){
  ans+=flag[i];
 }
 std::cout<<ans<<std::endl;
 return 0;
}

Measured memory_bytes: 3576
import sys
import copy
from collections import deque
import collections
stdin = sys.stdin

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

a = ni()

print(a+a*a+a*a*a)


Measured memory_bytes: 68232
from pprint import pprint
h, w, k = map(int, input().split())
maze = []
ts = []
for _ in range(h):
    l = input()
    l = list(map(int, list(l)))
    #print(l)
    x = 0
    tmps = []
    for j in range(w):
        x += l[j]
        tmps.append(x)
    ts.append(tmps)
    maze.append(l)
#pprint(maze)
#pprint("--")
#pprint(ts)

bin_a = ["{:09b}".format(a) for a in range(0, 2 ** (h - 1))]

res = 9999999
for cutpat in bin_a:
    cutpat = cutpat[::-1]
    #print("cutpat", cutpat)
    cutsum = [0] * 10
    gr = [-1] * 10
    gr[0] = 0
    grnum = 0
    for i in range(1, h):
        if cutpat[i - 1] == "1":
            grnum += 1
        gr[i] = grnum
    #print("grlist", gr)

    firstFail = False

    cutcount = cutpat.count("1") # 縦の切った回数
    for i in range(w): # 進む
        #print(" do", i)
        for j in range(h):
            cutsum[ gr[j] ] += maze[j][i]
        #print(" res", cutsum)
        isFail = False
        for j in range(h):
            #print("  chk gr", j)
            if cutsum[j] > k:
                #print("over! gr:", j)
                isFail = True
                if i == 0:
                    firstFail = True
        if isFail:
            #print("cut", i)
            cutcount += 1
            cutsum = [0] * 10
            for j in range(h):
                cutsum[gr[j]] += maze[j][i]
            for j in range(h):
                #print("  chk gr", j)
                if cutsum[j] > k:
                    isFail = True
                    if i == 0:
                        firstFail = True

    if firstFail:
        continue

    res = min(res, cutcount)

print(res)

Measured memory_bytes: 47580
import java.util.*;
import java.math.*;
class Main {
    //
    static final int MOD = 1_000_000_007; // 10^9+7
    //static final int MAX = 2_147_483_646; // intの最大値
    static final int INF = 1_000_000_000; // 10^9
    static final int MAX = 10_000_000;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int[][] bingo = new int[3][3];
        for(int i = 0; i < 3;i++){
            for(int j = 0;j < 3;j++){
                bingo[i][j] = sc.nextInt();
            }
        }
        int n = sc.nextInt();
        int[] ary = new int[n];
        for(int i = 0;i < n;i++){
            int tmp = sc.nextInt();
            for(int j = 0;j < 3;j++){
                for(int k = 0;k < 3;k++){
                    if(bingo[j][k] == tmp)bingo[j][k] = 0;
                }
            }
        }
        for(int i = 0;i < 3;i++){
            if(bingo[i][0] == bingo[i][1]&&bingo[i][1] == bingo[i][2]){
                System.out.println("Yes");
                return;
            }
            if(bingo[0][i]==bingo[1][i]&&bingo[1][i]==bingo[2][i]){
                System.out.println("Yes");
                return;
            }
        }
        if(bingo[0][0]==bingo[1][1]&&bingo[1][1]==bingo[2][2]){
            System.out.println("Yes");
            return;
        }
        if(bingo[0][2]==bingo[1][1]&&bingo[1][1]==bingo[2][0]){
            System.out.println("Yes");
            return;
        }
        System.out.println("No");
        
    }
}


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

int main() {
    string S;
    cin >> S;
    int a = S.size();
    char c = S.at(a-1);
    if(c == 's'){
        cout << S << "es" << endl;
    }
    else{
        cout << S << "s" << endl;
    }
}

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

int main() {
    int N; cin >> N;
    vector<int> v(N);
    rep(i,N) cin >> v.at(i);

    sort(v.begin(), v.end());
    double answer = v.at(0);
    double two = 2.0;
    for(int i=1; i<N; i++){
        answer = (answer+v.at(i))/(double)two;
    }

    cout << fixed << setprecision(10) << answer << endl;
}


Measured memory_bytes: 256
//////////////////////////
/*      _        ____   */
/*  U  /"\  u U /"___|  */
/*   \/ _ \/  \| | u    */
/*   / ___ \   | |/__   */
/*  /_/   \_\   \____|  */
/*   \\    >>  _// \\   */
/*  (__)  (__)(__)(__)  */
/*   Compro by emame©   */
//////////////////////////
#include<iostream>
#include<string>
using namespace std;
int main() {
  
  int x;

  cin >> x;
  cout << x*x*x << endl;
  
  return (0);
}


Measured memory_bytes: 3120
#include<bits/stdc++.h>
using namespace std;
 
#define PI acos(-1)
#define hell 1000000007
#define HELL 998244353
#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define fix(n) cout << fixed << setprecision(n)
#define mset(a,n) memset(a,n,sizeof a)
#define rep(i,a,b) for (__typeof((b)) i=(a);i<(b);i++)
#define repp(i,a,b,p) for(__typeof((b)) i=(a);i<(b);i+=p)
#define ren(i,a,b) for(__typeof((a)) i=(a);i>=(b);i--)
#define renn(i,a,b,p) for(__typeof((a) i=(a);i>=(b);i-=p)
#define lbd lower_bound
#define ubd upper_bound
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(v) (v).begin(), (v).end()
#define sz(x) (ll)x.size()
#define endl "\n"
#define out(n) cout<<n<<" "
#define outl(n) cout<<n<<endl
#define line cout<<endl
#define bug(n) {outl(n);return;}
#define N 1005
ll a,b;
void solve(){
	cin>>a;
	b=1e18;
    rep(i,1,(ll)sqrt(a)+1){
        if(a%i==0)b=min(b,a/i+i-2);
    }
    bug(b);
}
void prep(){
	
}
int main(){
    io;
    ll t=1;
    // cin>>t;
    prep();
    fix(12);
    while(t--)
        solve();
    return 0;
}

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

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

    int east[N] = {};
    if(S[0] == 'E')
        east[0] = 1;
    for(int i=1;i<N;i++){
        if(S[i] == 'E')
            east[i] = east[i-1] + 1;
        else
            east[i] = east[i-1];
    }

    int ans[N];
    // i番目の西側：i人，東側：N-(i+1)人
    for(int i=0;i<N;i++){
        ans[i] = 0;
        if(i>0)
            ans[i] += i - east[i-1];
        if(i<N-1)
            ans[i] += east[N-1] - east[i];
    }
    // 昇順ソート
    sort(ans, ans + N);
    cout << ans[0] << endl;
    return 0;
}

Measured memory_bytes: 3076
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  while (n--) {
    int y, m, d; cin >> y >> m >> d;
    int res = 0;
    while (y != 1000 or m != 1 or d != 1) {
      int c = y % 3 and ~m & 1 ? 19 : 20;
      if (d < c) ++d;
      else {
        d = 1;
        if (m < 10) ++m;
        else {
          m = 1;
          ++y;
        }
      }
      ++res;
    }
    cout << res << '\n';
  }
}


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

typedef long long ll;

int main() {
    ll N, M;
    cin >> N >> M;
    string S, T;
    cin >> S;
    cin >> T;
    ll g = (M*N)/__gcd(N, M);

    if (S[0] != T[0]) {
        cout << -1 << endl;
        return 0;
    } else {
        if (g == M*N) {
            cout << g << endl;
            return 0;
        } else {
            int a = g/N;
            int b = g/M;
            int i = a;
            int j = b;
            while (i < M && j < N) {
                if (S[j] != T[i]) {
                    cout << -1 << endl;
                    return 0;
                } else {
                    i += a;
                    j += b;
                }
            }
            cout << g << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 512
import sys
S = input()
T = input()

for i in range(len(S)-(len(T)-1))[::-1]:
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == "?":
            pass
        else:
            break
        if j == len(T)-1:
            ans = S[:i] + T + S[i+len(T):]
            ans = ans.replace("?","a")
            print(ans)
            sys.exit()
print("UNRESTORABLE")

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

typedef long long ll;

static ll N = 200000000;

using namespace std;

void BoatInfo(vector< vector<ll> >& data){
  int a, b, c;
  cin >> a >> b >> c;
  if(data[a-1][b-1] > c){
    data[a-1][b-1] = c;
    data[b-1][a-1] = c;
  }
}

void TravelarInfo(const vector< vector<ll> >& data){
  int s, g;
  cin >> s >> g;
  --s;
  --g;
  vector<bool> is_reached(data.size(), false);
  vector<ll> ans(data.size(), N);
  int i, j;
  ans[s] = 0;
  i = s;
  while(i != g){
    is_reached[i] = true;
    for(j=0; j<data[i].size(); ++j){
      ans[j] = min(ans[j], ans[i] + data[i][j]);
    }
    for(i=-1, j=0; j<is_reached.size(); ++j){
      if(!is_reached[j] && (i < 0 || ans[i] > ans[j])) i = j;
    }
  }
  if(ans[g] < N) cout << ans[g] << endl;
  else cout << "-1" << endl;
}

int main(){
  int n, m, x;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    vector< vector<ll> > data(n, vector<ll>(n, N));
    for(int i=0; i<m; ++i){
      cin >> x;
      if(x == 1) BoatInfo(data);
      else TravelarInfo(data);
    }
  }
  return 0;
}

Measured memory_bytes: 1292
/*
    author: Salam_35
    Created at :

*/

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

#define ll long long int
#define pb push_back
#define sc(n) scanf("%d",&n)
#define Max 100005
#define scan(n) scanf("%lld",&n)
#define pi acos(-1.0)
#define _fastio  ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ALL(x) x.begin(),x.end()
#define sci(n) scanf("%d",&n);
#define scl(n) scanf("%lld",&n);
#define load(array,size)  for(ll i=0 ; i<size ; i++) cin>>array[i] ;
#define load1(array,size)  for(ll i=1 ; i<=size ; i++) cin>>array[i] ;
#define INF	1LL<<19
#define pll pair<ll,ll>
#define mst(arr,value) memset(arr,value,sizeof(arr));
#define tst() int t;cin >> t;while(t--)


const ll MOD = 1e9+7;

typedef vector<ll>vec;
typedef pair<ll,ll> pii;
typedef map<ll,ll>mpll;
typedef set<ll>seet;

int main()
{
    int n;
  cin >> n;
  int cnt=0,cnt2=0;
  for(int i=0;i<n;i++){
    int a;
    cin >> a;
    if(a%2==0)
    {
      cnt++;
      if(a%5==0 or a%3==0)
        cnt2++;
    }
  }
 	if(cnt==cnt2)cout<<"APPROVED"<<endl;
  else cout<<"DENIED"<<endl;
}





Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i,k,n)  for(int i = (k);i < (n);++i)
#define REP(i,n)    FOR(i,0,n)
#define ALL(x)      begin(x),end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

bool ok(const vecint& a, const int th) {
  ll n = a.size();
  vecint v(n*4);
  int offset = n*2;
  ll cnt = 0;
  ll sum = 0;
  REP(i,n) {
    v[offset] += 1;
    if (a[i] >= th) {
      offset -= 1;
      sum -= v[offset];
    } else {
      sum += v[offset];
      offset += 1;
    }
    cnt += sum;
  }
  ll m = n * (n+1) / 2;
  ll mid = m/2 + 1;
  return cnt >= mid;
}

int main()
{
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  cin>>n;
  vecint a(n);
  REP(i,n) cin>>a[i];
  vecint b = a;
  b.push_back(0);
  b.push_back(1000000007);
  sort(ALL(b));
  b.erase(unique(ALL(b)), end(b));
  int hi = b.size()-1;
  int lo = 0;
  while ((hi - lo) > 1) {
    int mid = (hi + lo) / 2;
    if (ok(a, b[mid])) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  cout << b[lo] << endl;
  return 0;
}


Measured memory_bytes: 2660
// Problem : C - Flip,Flip, and Flip......
// Contest : AtCoder Regular Contest 091
// URL : https://atcoder.jp/contests/arc091/tasks/arc091_a
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define size(x) (int)x.size()

const int N = 1e6 + 1, mod = 1e9 + 7, inf = 2e9;

int n, m;

int main() { cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	if (n > m) swap(n, m);
	if (n == 1 && m == 1) cout << 1;
	else if (n == 1) cout << 1ll * n * m - 2;
	else cout << 1ll * n * m - 2ll * (n + m - 2);
}


Measured memory_bytes: 256
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

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

const ll mod = 1e9 + 7;

int main() {
  ll N;
  string str;

  cin >> N >> str;

  for (size_t i = 0; i < str.size(); i++) {
    char tmp;
    tmp = (((str[i] - 'A') + N) % 26) + 'A';
    printf("%c", tmp);
  }
  cout << endl;

  return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#define EPS 1e-10
struct vektor{
	double x,y;
	vektor(){}
	vektor(double xx,double yy){
		x=xx;
		y=yy;
	}
	vektor operator +(vektor v){
		return vektor(x+v.x,y+v.y);
	}

	vektor operator -(vektor v){
		return vektor(x-v.x,y-v.y);
	}
	vektor operator *(double d){
		return vektor(x*d,y*d);
	}
};

int n;
vektor vek[5];

bool check(){
	vektor v[5];
	for(int i=1;i<=4;i++){
		v[i]=vek[i]-vek[0];
	}
	double s=(v[3].x*v[2].y-v[3].y*v[2].x)/(v[1].x*v[2].y-v[1].y*v[2].x);
	double t=(v[3].x*v[1].y-v[3].y*v[1].x)/(v[1].y*v[2].x-v[1].x*v[2].y);
	int data3,data4;
	if(s>0 && t>0 && s+t<1)data3=1;
	else data3=-1;
	s=(v[4].x*v[2].y-v[4].y*v[2].x)/(v[1].x*v[2].y-v[1].y*v[2].x);
	t=(v[4].x*v[1].y-v[4].y*v[1].x)/(v[1].y*v[2].x-v[1].x*v[2].y);
	if(s>0 && t>0 && s+t<1)data4=1;
	else data4=-1;
	return data3*data4<0;
}

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<5;j++){
			scanf("%lf %lf",&vek[j].x,&vek[j].y);
		}
		bool flag=check();
		printf("%s\n",flag?"OK":"NG");
	}
	return 0;
}

Measured memory_bytes: 1048
// https://qiita.com/e869120/items/eb50fdaece12be418faa

#include <bits/stdc++.h>
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9+7;
const ll LINF = 1LL<<60;

int main()
{
    int n;
    cin >> n;
    vector<int> A(n), B(n), C(n);
    rep(i, n) {
        cin >> A[i];
    }
    rep(i, n) {
        cin >> B[i];
    }
    rep(i, n) {
        cin >> C[i];
    }

    sort(A.begin(), A.end());
    sort(C.begin(), C.end());

    ll res = 0;
    vector<int> bp(n, 0);
    rep(i, n) {
        ll a = lower_bound(A.begin(), A.end(), B[i]) - A.begin();
        ll c = n - (upper_bound(C.begin(), C.end(), B[i]) - C.begin());
        res += a * c;
    }

    cout << res << endl;
    return 0;
}

Measured memory_bytes: 1792
import sys
input = sys.stdin.readline

# ユニオンファインド
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n,m= map(int, input().split())
a= [list(map(int, input().split())) for i in range(m)]
ans=0
for i in range(m):
    uf=UnionFind(n)
    for j in range(m):
        if j==i:
            continue
        else:
            uf.union(a[j][0]-1,a[j][1]-1)
    if not uf.same(a[i][0]-1,a[i][1]-1):
        ans+=1
print(ans)

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

int main() {
    string s;
    int64_t k;
    cin >> s >> k;
    int64_t ans = 0;
    vector<int> cnt_same = {1};
    REP(i, s.size() - 1) {
        if(s[i] == s[i + 1]) {
            cnt_same.back() ++;
        } else {
            cnt_same.push_back(1);
        }
    }
    if(cnt_same.size() == 1){
        cout << cnt_same.front() * k / 2 << endl;
        return 0;
    }
    int cnt_front = cnt_same.front();
    int cnt_back = cnt_same.back();
    bool flag = false;
    if(s[0] == s.back()) {
        flag = true;
        // cnt_same.front() += cnt_same.back();
        // cnt_same.pop_back();
    }
    REP(i, cnt_same.size()) ans += cnt_same[i] / 2;
    ans *= k;
    if(flag) {
        // ans -= cnt_same.front() / 2;
        ans -= (k-1)*(cnt_front / 2 + cnt_back / 2 - (cnt_back + cnt_front) / 2);
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
abcd = list(map(int, input()))
op = {'0':'+', '1':'-'}
for i in range(8):
    result = abcd[0]
    code = str(abcd[0])
    pointer = 1
    for k in bin(i)[2:].zfill(3):
        if op[k] == '+':
            code += '+'
            result += abcd[pointer]
        else:
            code += '-'
            result -= abcd[pointer]
        code += str(abcd[pointer])
        pointer += 1
    if result == 7:
        code += "=7"
        break
print(code)


Measured memory_bytes: 3064
x,n = map(int,input().split())
if n == 0:
	print(x)
else:
	p = list(map(int,input().split()))
	t = abs(min(p)-x)+1
	u = min(p)-1
	for i in range(min(x-1,min(p)-1),max(x+1,max(p)+2)):
		if not( i in p ):
			if abs(i-x)<t:
				t = abs(i-x)
				u = i
			elif  abs(i-x)==t:
				if i < u:
					u = i
	print(u)

Measured memory_bytes: 9152
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n,q;
long a[1<<17],sum[1<<17];
long ans[1<<17];
main()
{
	cin>>n>>q;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum[i+1]=sum[i]+a[i];
	}
	vector<pair<int,int> >query;
	for(int i=0;i<q;i++)
	{
		int x;cin>>x;
		query.push_back(make_pair(x,i));
	}
	sort(query.begin(),query.end());
	int L=0,R=0;
	long left=0;
	for(int i=0;i<q;i++)
	{
		long x=query[i].first;
		while(L<n&&lower_bound(a,a+n,x+(x-a[L]))-lower_bound(a,a+n,a[L])
			>a+n-lower_bound(a,a+n,x+(x-a[L])))
		{
			left=sum[L+1]-left;
			L++;
		}
		while(R<n&&lower_bound(a,a+n,a[R])-lower_bound(a,a+n,x-(a[R]-x))
			<a+n-lower_bound(a,a+n,a[R])-1)
		{
			R++;
		}
		ans[query[i].second]=(R-L<n-R?sum[L]-left:left)+sum[n]-sum[R];
	}
	for(int i=0;i<q;i++)cout<<ans[i]<<endl;
}


Measured memory_bytes: 4980
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
#include <queue>
#include <map>
#include <cmath>
#include <numeric>
#include <tuple>

using namespace std;

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

const long long mod = 1e9 + 7;

int main() {
    

    
    ll n;
    string s;
    cin >> n >> s;
    
    ll ret = 0;
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 10; ++j)
        {
            for (int k = 0; k < 10; ++k)
            {
                bool f1 = 0;
                bool f2 = 0;
                bool f3 = 0;
                
                for (int l = 0; l < s.size(); ++l)
                {
                    int num = s[l] - '0';
                    if (!f1 && num == i)
                    {
                        f1 = 1;
                        continue;
                    }
                    if (f1 && !f2 && num == j)
                    {
                        f2 = 1;
                        continue;
                    }
                    if (f1 && f2 && num == k)
                    {
                        f3 = 1;
                        break;
                    }
                }
                if (f1 && f2 && f3)
                {
                    ret++;
                }
            }
        }
    }
    
    cout << ret << endl;
    
    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A)		(A).begin(),(A).end()
#define DUMP(A)    cout<<#A<<"="<<(A)<< endl
#define SIZE(A)    (int)((A).size())
#define MP  make_pair
using namespace std;
typedef long long ll;

int vx[]={1,0,-1,0},vy[]={0,1,0,-1};

const int MOD=10000;
int dp[100][3][3],bs[100];
int main(){
    int N,K;
    scanf("%d%d",&N,&K);
    for(int i=0;i<N;i++){
        bs[i]=-1;
    }
    int a,b;
    for(int i=0;i<K;i++){
        scanf("%d%d",&a,&b);
        a--;b--;
        bs[a]=b;
    }
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if((bs[0]==i||bs[0]==-1)&&(bs[1]==j||bs[1]==-1)) dp[1][i][j]=1;
            else dp[1][i][j]=0;
        }
    }
    for(int i=2;i<N;i++){
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++){
                dp[i][j][k]=0;
                if((bs[i-1]==j||bs[i-1]==-1)&&(bs[i]==k||bs[i]==-1)){
                    for(int l=0;l<3;l++){
                        if(l==j&&j==k) continue;
                        dp[i][j][k]+=dp[i-1][l][j];
                        dp[i][j][k]%=MOD;
                    }
                }
            }
        }
    }
    int ret=0;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            ret+=dp[N-1][i][j];
            ret%=MOD;
        }
    }
    printf("%d\n",ret);
}

Measured memory_bytes: 0
from sys import stdin, stdout
 
ii = lambda: int(stdin.readline())
mi = lambda: map(int, stdin.readline().strip().split())
li = lambda: list(mi())

mod = int(1e9) + 7

def GCD(x, y): 
   while(y): 
       x, y = y, x % y 
   return x 

N = ii()
A = li()
lcm = A[0]
for i in range(1, len(A)):
	lcm = (lcm // GCD(lcm, A[i])) * A[i]

ans = 0
for i in range(0, len(A)):
	ans = (ans + (lcm // A[i]))

ans %= mod

print(ans)

Measured memory_bytes: 4084
import sys
input = sys.stdin.readline

M = int(input())
D = [list(map(int,input().split())) for i in range(M)]

ans = 0
digitsum = 0
digit = 0

for i in range(M):
    digitsum += D[i][0]*D[i][1]
    digit += D[i][1]
print((digitsum-1)//9+digit-1)

Measured memory_bytes: 72924
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    double ans;

    int n;

    std::cin >> n;

  while( n != 0 ){
    double dev = 0;
    double sum = 0;
    double avg = 0;
    int s[1000];

    for(int i=0; i<n; ++i){
      std::cin >> s[i];
      sum += s[i];
    }

    avg = sum/n;
    for(int i=0; i<n; ++i){
      dev += (s[i]-avg)*(s[i]-avg);
    }
     std::cout << std::fixed;
     std::cout << std::setprecision(8) << sqrt(dev/n) << std::endl;
     std::cin >> n;
  }

  return 0;
}


Measured memory_bytes: 3204
#include <stdio.h>
#include <string.h>

int n;
int p;
int d1, d2;
int dist[10005];
int maxp;

int main()
{
    while (scanf("%d", &n), n != 0) {
        memset(dist, 0, sizeof(dist));

        for (int i = 0; i < n; i++) {
            scanf("%d %d %d", &p, &d1, &d2);
            dist[p] = d1 + d2;

            if (dist[maxp] < dist[p]) {
                maxp = p;
            }
        }

        printf("%d %d\n", maxp, dist[maxp]);
    }

    return 0;
}



Measured memory_bytes: 1984
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a=1,b=1,c=0,i,j,k;
    while(1){
     cin>>a>>b;
     if(a==0&&b==0)break;
   for(i=1;i<=a;i++){
      for(j=i+1;j<=a;j++){
        for(k=j+1;k<=a;k++){
            if(i+j+k==b)c++;
	}
      }
    }
    printf("%d\n",c);
    c=0;
    }
    return 0;
}



Measured memory_bytes: 3204
// 計算量は O(max(H*W, Q))
// これが O(H*W*Q) だと死ぬらしい
// 入力に対し行列を二重配列で保持するのではなく、各数字の位置座標を単純配列で保持する


#include <cstdio>
#include <complex.h>
using namespace std;


struct point{
    int x, y;
};


int dist(point p, point q){
    return abs(p.x-q.x)+abs(p.y-q.y);
}


int main(){
	int h, w, d, a;
    int q, l, r;
    point M[90001];
    int di[90001]={0};
    int MP[100001]={0};

    scanf("%d %d %d", &h, &w, &d);

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            scanf("%d", &a);
            M[a].x = j;
            M[a].y = i;
        }
    }

    // ここまでの入力により、移動する際に通る点の系列が確定する
    // 各系列において、最初の点からの距離を記憶しておく
    for(int i=1;i+d<=h*w;i++){
        di[i+d] = dist(M[i], M[i+d]) + di[i];
    }


    scanf("%d", &q);

    for(int i=0;i<q;i++){
        scanf("%d %d", &l, &r);
        MP[i] = di[r] - di[l];
    }

    for(int i=0;i<q;i++){
        printf("%d\n", MP[i]);
    }

	return 0;
}


Measured memory_bytes: 2432
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<string, int> P;
typedef pair<int, P> Q;

ll mod = 1000000007;
const ll INF = 100000000000000;
map<ll, ll> mp;
ll N, K, a[200010], sum[200010], ans;

ll pow_mod(ll x, ll y)
{
	if (y == 0) return 1;

	if (y % 2 == 0)
	{

	}
	else
	{

	}
}

int po(int x, int y)
{
	if (y == 0) return 1;
	return po(x, y - 1) * x;
}

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

	

	for (int i = 1; i <= N; i++)
	{
		cin >> a[i];
		a[i] %= K;
	}
	
	if (K == 1)
	{
		cout << 0 << endl;
		return 0;
	}

	for (int i = 1; i <= N; i++)
	{
		sum[i] = sum[i - 1] + a[i];
		sum[i] %= K;
	}
	mp[0]++;
	for (int i = 1; i <= N; i++)
	{
		ll tt = mp[(K + i - sum[i]) % K];
		ans += tt;
		mp[(K + i - sum[i]) % K]++;
		if (i >= K - 1)
		{
			mp[(K + (i - K + 1) - sum[i - K + 1]) % K]--;
		}
	}
	
	cout << ans << endl;

	//K�ȏ㗣��Ă͂Ȃ�Ȃ�

	int www;
	cin >> www;
}

Measured memory_bytes: 15872
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] chars = sc.next().toCharArray();
        int counter = 0;
        int length = chars.length;
        for (int i = 0; i < chars.length / 2; i++) {
            if (chars[i] == chars[length - 1 - i]) {
            } else {
                counter++;
            }
        }
        System.out.println(counter);
    }
}

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> chart(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> chart.at(i).at(j);
    }
  }
  vector<bool> line(H, false);
  vector<bool> row(W, false);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (chart.at(i).at(j) == '#') {
        line.at(i) = true;
        row.at(j) = true;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    if (line.at(i)) {
      for (int j = 0; j < W; j++) {
        if (row.at(j)) {
          cout << chart.at(i).at(j);
        }
      }
    cout << endl;
    }
  }
}


Measured memory_bytes: 3620
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
#include<boost/multiprecision/cpp_int.hpp> 
using namespace std;
using namespace boost::multiprecision;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1e18 + 1LL;
const ll INF = 1e16;
const double pi = acos(-1);

int main() {
    ll n;
    cin >> n;

    cpp_int ans = 1;
    bool zero = false;
    vector<cpp_int> a(n);
    rep(i, n) {
        cin >> a[i];
        if(a[i] == 0) {
            zero = true;
        }
    }

    if(zero) {
        out(0);
        re0;
    }

    rep(i, n) {
        ans *= a[i];
        if(ans > 1000000000000000000) {
            out(-1);
            re0;
        }
    }

    out(ans);
    re0;
}

Measured memory_bytes: 6372
#include<stdio.h>

int main()
{
  int r,l,d;
  int cnt=0;
  scanf("%d %d %d",&l,&r,&d);
  
  do
  {
    if(l%d == 0)
      cnt++;
    l++;
  }while(r-l>=0);

  printf("%d",cnt);

  return 0;
}

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

int main() {
	int n;
  	cin >> n;
  	vector<int> v(n);
  	for (int i=0; i<n; i++) cin >> v[i];
  	sort(v.begin(), v.end());
  	float res = (float) v[0];
  	for (int i=1; i<n; i++)
      res = (float) ((res + v[i]) / 2);
  
  	cout << res  << endl;
  	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a; i< (int)(b); ++i)
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
typedef long double ld;
using ll=long long;
using namespace std;
const int mod=1e9+7;
const ll inf = 1LL << 60;
const ld pi = (acos(-1));
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }



int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  
  int n,a,b ; cin >> n >> a >> b;
  cout << min(a*n,b)  << endl;
  
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 2e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

ll dp[55][55][2550];
int main() {
    int n, a;
    cin >> n >> a;
    int x[n];
    rep(i, 0, n) cin >> x[i];

    dp[0][0][0] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            for (int k = 0; k <= 2500; k++) {
                if (dp[i][j][k] == 0) continue;
                dp[i + 1][j + 1][k + x[i]] += dp[i][j][k];
                dp[i + 1][j][k] += dp[i][j][k];
            }
        }
    }

    ll ans = 0;
    rep(i, 1, n + 1) {
        ans += dp[n][i][i * a];
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 55552
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;

typedef long long LL;

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    LL n;
    cin >> n;
    cout << n * 2 / gcd(n, 2) << endl;
    return 0;
}

Measured memory_bytes: 888
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main(){
   int N;
   std::cin >> N;
   std::vector<int> T(N);
   for (int i=0; i<N; i++){
      std::cin >> T.at(i);
   }
   int M;
   std::cin >> M;
   std::pair<int, int> P;
   std::vector<std::pair<int, int>> PX(M);
   int sum = std::accumulate(T.begin(), T.end(), 0);
   for (int i=0; i<M; i++){
      int key, val;
      std::cin >> key >> val;
      P = {key, val};
      PX.at(i) = P;
   }
   for (int i=0; i<M; i++){
      std::cout << sum-T.at(PX.at(i).first-1)+PX.at(i).second << std::endl;
   }
}



Measured memory_bytes: 256
A = []
for i in range(3):
    A.append(input().split())
N = int(input())
for i in range(N):
    X = input()
    for j in range(3):
        if A[j][0] == X:
            A[j][0] = 'X'
        if A[j][1] == X:
            A[j][1] = 'X'
        if A[j][2] == X:
            A[j][2] = 'X'
result = []
for i in range(3):
    result.append(A[i])
if (result[0][0] == result[0][1] == result[0][2]) or (result[1][0] == result[1][1] == result[1][2]) or (result[2][0] == result[2][1] == result[2][2]):
    print('Yes')
elif (result[0][0] == result[1][0] == result[2][0]) or (result[0][1] == result[1][1] == result[2][1]) or (result[0][2] == result[1][2] == result[2][2]):
    print('Yes')
elif (result[0][0] == result[1][1] == result[2][2]) or (result[0][2] == result[1][1] == result[2][0]):
    print('Yes')
else:
    print('No')

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

int num[128];

int main() {
  num['I'] = 1;
  num['V'] = 5;
  num['X'] = 10;
  num['L'] = 50;
  num['C'] = 100;
  num['D'] = 500;
  num['M'] = 1000;

  string s;
  while(cin >> s) {
    reverse(s.begin(), s.end());
    int res = 0;
    for(int i = 0; i < s.size(); ++i) {
      if(i && num[s[i-1]] > num[s[i]]) {
	res -= num[s[i]];
      } else {
	res += num[s[i]];
      }
    }
    cout << res << endl;
  }
  return 0;
}

Measured memory_bytes: 1176
#  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_15_A&lang=jp
#  Greedy alogithms : python3
#  2019.2.3 yonezawa

import sys
input = sys.stdin.readline
#import cProfile

def main():
    s = int(input())
    cv = [25,10,5,1]
    n = 0
    for i in cv:
        n += s // i
        s = s % i
    print (n)

if __name__ == '__main__':
    main()
    #pr = cProfile.Profile()
    #pr.runcall(main)
    #pr.print_stats()



Measured memory_bytes: 5600
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <queue>
#include <list>
#include <limits>
#include <string>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;

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

int main(){
    int N,M,num;
    int dp[100001];
    const int INF = -9999;
    cin >> N >> M;
    dp[0] = 1;
    dp[1] = 1;
    for(int i=0;i<M;i++){
        cin >> num;
        dp[num] = INF;
    }
    if(dp[0] == INF) dp[0] = 0;
    if(dp[1] == INF) dp[1] = 0;
    for(int i=2;i<=N;i++){
        if(dp[i] == INF){
            dp[i] = 0;
        }else{
            dp[i] = (dp[i-1] + dp[i-2])%MOD;
        }
    }
    cout << dp[N];
}

Measured memory_bytes: 640
#define debug_interval ','
#define dump_interval ' '
#define debug_toggle 1
//{
#include<bits/stdc++.h>
using namespace std;
#define hi cerr<<"hi"<<endl;
#define int long long
#define INT_MAX LLONG_MAX
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define fi first
#define se second
#define mp make_pair
#define rev reverse
#define dans dump(ans)
#define MOD 1000000007
#define amp(v,n) (v).count(n)?v[n]++:v[n]=1
#define sysp system("pause")
#define PI acos(-1)

//{
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class...A> inline void dump(){cout<<endl;}
template<class...A> inline void dump_rest() {cout<<endl;}
template<class T, class...A> inline void dump_rest(const T& first, const A&... rest){cout<<dump_interval<<first;dump_rest(rest...);}
template<class T,class...A> inline void dump(const T&first,const A&...rest){cout<<first;dump_rest(rest...);}
template<class...A> inline void debug(){cerr<<endl;}
template<class...A> inline void debug_rest() {cerr<<endl;}
template<class T, class...A> inline void debug_rest(const T& first, const A&... rest){cerr<<debug_interval<<first;debug_rest(rest...);}
template<class T,class...A> inline void debug(const T&first,const A&...rest){if(debug_toggle)cerr<<first,debug_rest(rest...);}
//}

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

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
template<int m>class mint{private:int i;public:mint() : i(0){}mint(int i): i((i%m+m)%m){}mint operator+(const mint& o){return o.i+i;}mint operator*(const mint& o){return o.i*i;}mint operator-(){return -i;}operator int() {return i;}};

string s;

main(){
	cin>>s;
	int ans=0;
	rep(i,s.size()){
		if(i%2==0){
			if(s[i]=='p')ans--;
		}else{
			if(s[i]=='g')ans++;
		}
	}
	dans;
}


Measured memory_bytes: 512
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    S, C = li_input()

    # Sを新たに作る必要がない場合
    if S * 2 >= C:
        print(C // 2)
        return

    ans = S
    C -= S * 2
    ans += (C // 4)

    print(ans)


main()


Measured memory_bytes: 5028
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 200000+7;

int a[MAXN];
int b[MAXN];

bool cmp(int x, int y)
{
    return x > y;
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(b, b+n, cmp);
    for(int i=0; i<n; i++)
    {
        if(b[0] > a[i])
            printf("%d\n", b[0]);
        else
        {
            printf("%d\n", b[1]);
        }
        
    }



    return 0;
}

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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod =(1e9)+7;
using P = pair<ll,ll>;

int main(){
     ll h,w,n;cin >>h>>w>>n;
     ll mx=max(h,w);
    ll ans=(n-1)/mx+1;
    cout<<ans <<endl;
    return 0;
}


Measured memory_bytes: 256
import sys

def reflect(A, B):
    if A > B:
        A, B = B, A

    ref = B // A
    if B % A == 0:
        return (ref * 2 - 1) * A
    else:
        return ref * 2 * A + reflect(B % A, A)


N, X = map(int, sys.stdin.readline().rstrip().split())
print(N + reflect(X, N - X))

Measured memory_bytes: 38256
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e1+5;
const int INF=0x7fffffff;
const int mod=1e9+7;
const double eps=1e-8;
const double pi=acos(-1);
int n,m;
char s[M][M];
bool vis[M][M];
bool used[M][M];
int mstep;
int D[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct node
{
    int x,y,step;
    node(int xx,int yy,int ss){
        x=xx;
        y=yy;
        step=ss;
    }
    node(){};
}point;
template<class T>
inline void read(T &x)
{
    char c;
    x=1;
    while((c=getchar())<'0'||c>'9')
        if(c=='-')
            x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9')
        res=res*10+c-'0';
    x*=res;
}
void bfs(int x,int y)
{
    queue<node> q;
    node now(x,y,0);
    used[x][y]=1;
    q.push(now);
    while(q.size()){
        now=q.front();
        q.pop();
        if(now.step>mstep){
            mstep=now.step;
            point.x=now.x;
            point.y=now.y;
        }
//        cout<<now.x<<' '<<now.y<<endl;
        FOR(i,0,3){
            int xx=now.x+D[i][0];
            int yy=now.y+D[i][1];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!used[xx][yy]&&s[xx][yy]=='.'){
                used[xx][yy]=1;
                node next(xx,yy,now.step+1);
                q.push(next);
            }
        }
    }
}
int main()
{
    r(n); r(m);
    FOR(i,1,n){
        FOR(j,1,m){
            s[i][j]=getchar();
        }
        getchar();
    }
    int ans=0;
    FOR(i,1,n){
        FOR(j,1,m){
            if(s[i][j]=='.'){
                memset(used,0,sizeof used);
                mstep=0;
                bfs(i,j);
                mstep=0;
                memset(used,0,sizeof used);
                bfs(point.x,point.y);
                ans=max(ans,mstep);
                break;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


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

long long chose(long long n){
    return (n)*(n-1)/2;
}

int main(){
    int n;cin>>n;
    map<int,int> ballcounts;
    vector<int>  balls(n);
    for(int i=0;i<n;i++){
        cin >> balls[i];
        ballcounts[balls[i]]++;
    }

    long long tot=0;

    for(auto itr=ballcounts.begin();itr!=ballcounts.end();itr++){
        tot+=chose(itr->second);
    }
    for(auto & e :balls){
        long long ans = tot;
        ans -= chose(ballcounts[e]);
        ans += chose(ballcounts[e]-1);
        cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 8320
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 1e9+7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
    int D;
    cin >> D;
    vector<int>c(26);
    for(int i = 0; i < 26; i++) {
        cin >> c[i];
    }
    vector<vector<int>>s(D,vector<int>(26));
    for(int i = 0; i < D; i++) {
        for(int j = 0; j < 26; j++) {
            cin >> s[i][j];
        }
    }
    vector<int>t(D);
    for(int i = 0; i < D; i++) {
        t[i] = rand()%26+1;
    }
    int ans = 0;
    vector<int>Last(26);
    for(int i = 0; i < D; i++) {
        ans+=s[i][t[i]-1];
        Last[t[i]-1] = i+1;
        for(int j = 0; j < 26; j++) {
            ans-=c[j]*(i+1-Last[j]);
        }
    }
    int M = 3000000;
    vector<P>horyuu;
    int cnt = 0;
    for(int i = 0; i < M; i++) {
        int d = rand()%D+1;
        int q = rand()%26+1;
        int ord = ans;
        int X = 0;
        int Y = 0;
        int A = 0;
        int B = 0;
        ans+=s[d-1][q-1]-s[d-1][t[d-1]-1];
        for(int j = 0; j < D; j++) {
            if(t[j] == t[d-1]) {
                if(j == d-1) {
                    Y = j+1;
                }
                else {
                    X = j+1;
                }
                A = j+1;
            }
            if(t[j] == q) {
                Y = j+1;
                B = j+1;
            }
            ans-=c[t[d-1]-1]*(j+1-X)-c[t[d-1]-1]*(j+1-A);
            ans+=c[q-1]*(j+1-B)-c[q-1]*(j+1-Y);
        }
        if(ans >= ord) {
            ord = ans;
            horyuu.push_back({d,q});
            cnt++;
        }
        else {
            ans = ord;
        }
        if(cnt == 3) {
            cnt = 0;
            t[horyuu[2].first-1] = horyuu[2].second;
            horyuu = {};
        }
    }
    for(int i = 0; i < D; i++) {
        cout << t[i] << "\n";
    }
}

Measured memory_bytes: 3720