akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
using namespace std;

int main(void){
    int N;
    string s;
    cin >> N;
    cin >> s;
    
    string result, tmp;
    int count = 0;
    for (int i=0; i<N; i++) {
        result.push_back(s[i]);
        if (s[i]=='(') {
            count++;
        } else {
            if (count>0) {
                count--;
            } else {
                result = "(" + result;
            }
        }
    }
    while (count > 0) {
        result.push_back(')');
        count--;
    }

    cout << result << endl;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstring>

int solve(int, int);

int k, s;
int d[7500][4];

int main() {
	scanf("%d %d", &k, &s);
	memset(d, -1, sizeof(d));
	printf("%d\n", solve(0, 0));
	return 0;
}

int solve(int cur, int num) {
	if(cur==s && num==3) return 1;
	if(cur>s || num==3) return 0;
	int& ret=d[cur][num];
	if(ret!=-1) return ret;

	ret=0;
	for(int i=0; i<=k; i++) {
		ret+=solve(cur+i, num+1);
	}
	return ret;
}


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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

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

const int N = 2e5+23;
int a[N];

int fix(int d) {
  return d/10+d%10;
}

long long simplify(int d, long long c, int &dout) {
  dout = d;
  if (c == 1) return 0;
  long long ret = c/2;
  int origD = d;
  d += d;
  if (d >= 10) {
    ret += c/2;
    d = fix(d);
  }
  ret += simplify(d, c/2, dout);
  if (c%2) {
    dout += origD;
    ++ret;
    if (dout >= 10) {
      dout = fix(dout);
      ++ret;
    }
  }
  return ret;
}

void solve() {
  int n; scanf("%d", &n);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int d; long long c;
    scanf("%d%lld", &d, &c);
    ans += simplify(d, c, d);
    a[i] = d;
  }
  for (int i = 1; i < n; ++i) {
    a[i] += a[i-1];
    ++ans;
    if (a[i] >= 10) {
      a[i] = fix(a[i]);
      ++ans;
    }
  }
  printf("%lld", ans);
}

int main() {
  init();
  int t = 1; //scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


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

int main(){
    int x;
    cin >> x;
    int res = 0;
    while(x>=500){
        res+=1000;
        x=x-500;
    }
    while (x>=5){
        res+=5;
        x=x-5;
    }
    cout << res << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <bitset>
#include <cassert>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define dump(v) (cerr << #v << ": " << v << endl)
#define cerr                                                                   \
  if (true)                                                                    \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;
typedef pair<int, int> pp;       // int aa = pp.first;
typedef tuple<int, int, int> tp; // int aa = get<0>(tp);

// ll n = to_T<ll>("114514")
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}
template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

// BinaryIndexSearch
class BIT {
public:
  vector<int> bit;
  int M;

  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}

  int get(int i) const {
    if (!i)
      return 0;
    return bit[i] + get(i - (i & -i));
  }

  void add(int i, int x) {
    if (i > M)
      return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};

struct Node {
  int L, R, d;
  Node(int L, int R, int d) : L(L), R(R), d(d){};
};
bool operator<(Node &l, Node &r) { return l.d < r.d; }

void solve(long long N, long long M, vector<long long> l, vector<long long> r) {
  BIT bit(100010);

  vector<Node> v;
  REP(i, N) { v.push_back(Node(l[i], r[i], r[i] - l[i] + 1)); }
  sort(ALL(v));

  int pc = 0;
  FOR(m, 1, M + 1) {
    ll ans = 0;

    while (pc < N && v[pc].d < m) {
      bit.add(v[pc].L, 1);
      bit.add(v[pc].R + 1, -1);
      pc++;
    }

    ans += N - pc;

    for (int v = m; v <= 100000; v += m) {
      ans += bit.get(v);
    }

    cout << ans << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  long long N;
  long long M;
  cin >> N;
  vector<long long> r(N - 1 + 1);
  vector<long long> l(N - 1 + 1);
  cin >> M;
  for (int i = 0; i <= N - 1; i++) {
    cin >> l[i];
    cin >> r[i];
  }
  solve(N, M, l, r);
  return 0;
}


Measured memory_bytes: 16368
from itertools import accumulate

def divisors(n):
    divs = []
    for i in range(1, int(n ** 0.5) + 1):
        d, m = divmod(n, i)
        if m == 0:
            divs.append(i)
            divs.append(d)
    divs.sort(reverse=True)
    return divs

n, k = map(int, input().split())
a = list(map(int, input().split()))
divs = divisors(sum(a))

for div in divs:
    m = [ x % div for x in a ]
    m.sort()
    l = [0] + list(accumulate(m))
    r = list(reversed([0] + list(accumulate(reversed([div - x for x in m])))))
    for i in range(len(l)):
        if l[i] == r[i]:
            if l[i] <= k:
                print(div)
                exit()
            break
        elif l[i] > r[i]:
            break

Measured memory_bytes: 3316
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp;

fn main() {
    input!{
        a: i32,
        b: i32,
        c: i32,
        d: i32,
    }
    println!("{}", cmp::max(0, cmp::min(b, d) - cmp::max(a, c)));
}


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

int n, C;
int imos[30][100002];

int main() {
  cin >> n >> C;
  for (int i = 0; i < n; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    imos[c - 1][s]++;
    imos[c - 1][t + 1]--;
  }

  for (int i = 0; i < C; i++) {
    for (int j = 0; j < 100000; j++) {
      imos[i][j + 1] += imos[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i <= 100000; i++) {
    int cnt = 0;
    for (int j = 0; j < C; j++) {
      if (imos[j][i] > 0) cnt++;
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 11904
#include <bits/stdc++.h>
#define int long long
using namespace std;

int N, M;

signed main(){
  
  cin>>N>>M;
  
  int maxl = 0, minr = N;
  
  for(int i=0;i<M;i++){
    
    int l, r;
    cin>>l>>r;
    
    maxl = max(maxl,l);
    minr = min(minr,r);
    
  }
  
  if(minr-maxl+1<0) cout<<0<<endl;
  else cout<<minr-maxl+1<<endl;
  
  return 0;
}


Measured memory_bytes: 256
/*
    Author:zeke
    
    pass System Test!
    GET AC!!
*/
#include <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <utility>
#include <stack>
#include <bitset>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s) \
    ll(s);      \
    cin >> (s);
#define Scin(s) \
    ll(s);      \
    cin >> (s);
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;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll k,n;
    cin>>k>>n;
    V vec(n);
    rep(i,n)cin>>vec[i];
    sort(all(vec));
    ll sum=k;
    ll reg=0;
    rep3(i,1,n){
        chmax(reg,vec[i]-vec[i-1]);
    }
    chmax(reg,(k-vec[n-1])+vec[0]);
  //cout<<(k-vec[n-1])+vec[0]<<endl;
    cout<<sum-reg<<endl;
}


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

int k, a, b;

int main() {
	cin >> k;
	cin >> a >> b;
	for(int i=a;i<=b;++i)
		if ((i % k) == 0) {
			cout << "OK" << endl;
			return 0;
		}
	cout << "NG" << endl;
	return 0;
}

Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
 
int main(){
  string str;
  cin >> str;
  for(int i=0; i<str.size(); i+=2){
    if (str[i]!='h' || str[i+1]!='i'){
      cout << "No" <<endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}

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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define repi(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniqueV(x)          \
  sort(x.begin(), x.end()); \
  x.erase(unique(x.begin(), x.end()), x.end());
#define debug(x) cerr << #x << ": " << x << '\n'
#define debug2(x, y) cerr << "(" << #x << ", " << #y << ") = " \
                          << "(" << x << ", " << y << ")" << '\n'

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

const long long INF = 3e18 + 12;
const int inf = 1e9;

// ----------------------------------------------

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(m, vector<int>(1));
  rep(i, m) { cin >> a[i][0] >> a[i][1]; }

  rep(i, 1000) {
    string s = to_string(i);
    int keta = s.size();

    if (n != keta) {
      continue;
    }

    bool flag = 1;
    rep(j, m) {
      if (s[a[j][0] - 1] - '0' != a[j][1]) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      cout << i << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}

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

#define REP(i, n) for(int i = 0;i < n;i++)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;

int main()
{
    int N; cin >> N;
    int n = 1;


    while( n * 2 <= N ) n *= 2;

    cout << n << endl;

    return 0;
}


Measured memory_bytes: 256
while _ = $stdin.gets
  stack = []
  terms = _.chomp.split(/ /)
  terms.each do |t|
    case t
    when /^\A-?\d+\z/
      stack << t.to_f
    when '+'
      stack << stack.pop(2).inject(:+)
    when '-'
      stack << stack.pop(2).inject(:-)
    when '/'
      stack << stack.pop(2).inject(:/)
    when '*'
      stack << stack.pop(2).inject(:*)
    end
  end
  puts('%.6f' % stack[0])
end

Measured memory_bytes: 6140
#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	string s;
	cin>>s;
	int red=0,blue=0;
	for(char x: s)
	{
		if(x=='R')
		{
			red++;
		}
		else
		{
			blue++;
		}
	}
	if(red>blue){cout<<"Yes";
	}
	else
	{
		cout<<"No";
	}
}

Measured memory_bytes: 256
import math
n, X = map(int, input().split())
x = list(map(int, input().split()))
X_x = [0] * n

for i in range(n):
    X_x[i] = abs(x[i] - X)

gcd = X_x[0]
for i in range(1, n):
    gcd = math.gcd(gcd, X_x[i])

print(gcd)

Measured memory_bytes: 20364
#include "bits/stdc++.h"
#define rep(i,n) for(ll i=0;i<(n);i++)
using namespace std;
using ll = long long;


int main() {
	int D;
	cin >> D;
	int c[26];
	rep(i, 26) {
		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);
	rep(i, D) {
		cin >> t[i];
	}
	int last[26];
	rep(i, 26) {
		last[i] = 0;
	}
	int ans = 0;
	rep(d, D) {
		ans += s[d][t[d] - 1];
		last[t[d] - 1] = d + 1;
		rep(i, 26) {
			ans -= c[i] * (d + 1 - last[i]);
		}
		cout << ans << endl;
	}


	return 0;
}

Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
int main(){
  long long a,b,k;
  cin>>a>>b>>k;
  if(k<=a){
    cout<<a-k<<" "<<b<<endl;
  }
  else if(a<=k && k<=a+b){
    cout<<0<<" "<<b-(k-a)<<endl;
  }
  else if(a+b<k){
    cout<<0<<" "<<0<<endl;
  }
  return 0;
}
  


Measured memory_bytes: 256
H,W = map(int,input().split())

ans = []

top = ['#']*(W+2)

ans.append(top)

for i in range(H):
    t = list(input())
    t = ['#'] + t + ['#']
    ans.append(t)

ans.append(top)

for aa in ans:
    print(''.join(map(str,aa)))

Measured memory_bytes: 65664
import sys
readline = sys.stdin.readline

# 累積和によって、各区間の和をO(1)で求められるようにしておく
# B,C,D,Eに分けたときに、CとDの切れ目を固定する
# Cの終わりが決まったとき、Bを先頭から探索していくとB,Cを切るベストな点が分かる
# （BとCの絶対値の差が最小になる点）
# 同様に、CとDを切るベストな点が分かる
# これらのindexを覚えておくと、CとDの切れ目を移動したときにこの点から開始することが出来て
# O(N)の定数倍で解くことが出来る

N = int(readline())
A = [0] + list(map(int,readline().split()))
for i in range(1, len(A)):
  A[i] += A[i - 1]

#print(A)

b_end = 1
c_end = 2
d_end = 3
e_end = len(A) - 1
bc_best = 10 ** 10
de_best = 10 ** 10
ans = 10 ** 10
def get_ans(b_end,c_end,d_end):
  B = A[b_end]
  C = A[c_end] - A[b_end]
  D = A[d_end] - A[c_end]
  E = A[-1] - A[d_end]
#  print("get_ans b_end",b_end,"c_end",c_end,"d_end",d_end)
#  print("get_ans B",B,"C",C,"D",D,"E",E)
  return max(B,C,D,E) - min(B,C,D,E)

for c_end in range(2, N - 1):
  # b_endを右に進める条件は、今BよりCのほうが大きいこと 且つ c_endに追いつかないこと
  # Bのほうが大きくなったら、その直前と比べてどっちのほうが絶対値が小さいかを確認する
  B = A[b_end]
  C = A[c_end] - A[b_end]
  best_b_end = b_end
  if B != C:
    while b_end + 1 < c_end and B < C:
      b_end += 1
      tempB = B
      tempC = C
      B = A[b_end]
      C = A[c_end] - A[b_end]
      if B == C:
        best_b_end = b_end
        break
      if B > C:
        # ここでBのほうが大きくなったら有利なほうを採用する
        if abs(tempB - tempC) > abs(B - C):
          best_b_end = b_end
        break
      else:
        best_b_end = b_end
  b_end = best_b_end
  
  D = A[d_end] - A[c_end]
  E = A[e_end] - A[d_end]
  best_d_end = d_end
  if D != E:
    while d_end + 1 < e_end and D < E:
      d_end += 1
      tempD = D
      tempE = E
      D = A[d_end] - A[c_end]
      E = A[e_end] - A[d_end]
      if D == E:
        best_d_end = d_end
        break
      if D > E:
        if abs(tempD - tempE) > abs(D - E):
          best_d_end = d_end
        break
      else:
        best_d_end = d_end
  d_end = best_d_end
    
  val = get_ans(b_end,c_end,d_end)
  if val < ans:
    ans = val
    
print(ans)

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

int main() {
    int H,N;
    cin>>H>>N;
    int A;
   int sum=0;
   for(int i=0;i<N;i++){
   	cin>>A;
   	sum+=A;
   }
    if(sum>=H)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;

#define MAX 256

int main(void){

	int a[10], b;

	for (int i = 0; i < 10; i++){
		scanf("%d", &a[i]);
	}

	for (int i = 0; i < 10; i++){
		for (int j = 0; j < 9; j++){
			if (a[j] < a[j + 1]){
				b = a[j];
				a[j] = a[j + 1];
				a[j + 1] = b;
			}
		}
	}

	printf("%d\n%d\n%d\n", a[0], a[1], a[2]);

	return 0;
}

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

using namespace std;

string s[51];
int t[51];

int main(){
    ios::sync_with_stdio(0);
    int n;
    cin >> n;
    for(int i = 0; i<n; i++){
        cin >> s[i] >> t[i];
    }
    string sval;
    cin >> sval;
    int tsum = 0;
    int si = 0;
    for(int i = 0; i<n; i++){
        if(s[i]==sval){
            si = i+1;
            break;
        }
    }
    for(int i = si; i<n; i++)
        tsum+=t[i];
    cout << tsum << endl;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
int main()
{
  int i,n,k,a[100001],ans;
  scanf("%d %d",&n,&k);
  for(i=1;i<=n;i++){
    scanf("%d",&a[i]);
  }
  ans = (n-1)/(k-1);
  ans = ((n-1)%(k-1)==0?ans:ans+1);
  printf("%d",ans);
  return 0;
}


Measured memory_bytes: 512
#include <stdio.h>
#include <string.h>
int main(void){
        int i;
        int len,count;
        char n[1001];
        while(1){
                scanf("%s",n);
                if(n[0]=='0')
                        break;

                len = strlen(n);
                count = 0;

                for(i=0;i<len;i++)
                        count += (n[i]-'0');

                printf("%d\n",count);
        }
        return 0;
}


Measured memory_bytes: 2084
#include <bits/stdc++.h>
#define REP(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for(int (i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for(int (i) = (m); (i) < (n); (i)++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
    if(a.second != b.second){
        return a.second < b.second;
    }else{
        return a.first < b.first;
    }
}

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


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

int MOD = 2019;

void testCase(){
    string s;
    cin>>s;
    int mult = 1, n = s.length(), val = 0;
    long long res = 0;
    vector<long long> count(2019, 0);
    count[0]++;
    
    for(int i = n-1; i >= 0; i--){
        val = (val + (s[i]-'0')*mult)%MOD;
        mult = (mult*10)%MOD;
        res += count[val];
        count[val]++;
    }
    cout<<res<<endl;
}

int main(){
    int t;
    t = 1;
    // cin>>t;
    while(t--){
        testCase();
    }
}



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

using namespace std;

const int MAXN = 5e5 + 5;

int n , a , b , A , B;

int main()
{
        cin>>a>>b;
        if(a<=0 && 0<=b)
            cout<<"Zero" ,  0 ;
        else
        {
            if(b<0)
                cout<<((b - a + 1)%2 ?"Negative":"Positive");
            else cout<<"Positive";
        }
        return 0;
}


Measured memory_bytes: 256
//**KEEP IT SHORT AND SIMPLE**
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define REP(i,a,b) for (int i = a; i < b; i++)
#define NL '\n'
#define F first
#define S second
#define dot(a, b) (conj(a) * (b)).real()
#define cross(a, b) (conj(a)* (b)).imag()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef bool bl;
typedef vector < vector<long long> > vvl;
typedef vector <long long> vll;
typedef pair <long long, long long> pll;
typedef map <long long, long long> mll;
typedef map <long long, bool> mlb;
typedef priority_queue<long long, vector<long long>, greater<long long> > minPq;
typedef tuple<ll, ll, ll> line;
typedef complex<double> point;
const double pi = 2 * acos(0.0);
const int OO = 0x3f3f3f3f;
vvl Adj(100010);
bl vis[100010];
void dfs(ll u)
{
    vis[u] = 1;
    for(auto it : Adj[u])
        if(!vis[it])
            dfs(it);
}
int main()
{
    IO
    ll n, m, a, b, c, cnt = 0;
    cin >> n >> m;
    REP(i, 0, m)
    {
        cin >> a >> b >> c;
        Adj[a].push_back(b);
        Adj[b].push_back(a);
    }
    REP(i, 1, n + 1)
        if(!vis[i])
            cnt++, dfs(i);
    cout << cnt;
    return 0;
}


Measured memory_bytes: 8960
hw = input().split()
h = int(hw[0])
w = int(hw[1])

field = []
for i in range(h):
  field.append(input())

ans_field = []
for i in range(h):
  ans_field.append('')
  for j in range(w):
    if field[i][j]=='.':
      s = ''
      #[-1]はリストの最後という意味で出力されちゃう
      peris = [[i-1, j-1], [i-1, j], [i-1, j+1], [i, j-1], [i, j+1], [i+1, j-1], [i+1, j], [i+1, j+1]]
      for peri in peris:
        if peri[0]!=-1 and peri[1]!=-1:  
          try:
            s += field[peri[0]][peri[1]]
          except:
            pass
      sharp = 0
      for letter in s:
        if letter=='#':
          sharp += 1
      ans_field[i] += str(sharp)
    else:
      ans_field[i] += '#'

for i in range(h):
  print(ans_field[i])

Measured memory_bytes: 3064
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    int n;

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

    void run() {
        FastReader sc = new FastReader();
        n = sc.nextInt();
        solve();
    }

    void solve() {
        int pPrevDigit = -1;
        int prevDigit = -1;
        while(n > 0) {
            int digit = n % 10;
            if (pPrevDigit == digit) {
                System.out.println("Yes");
                return;
            } else if (prevDigit == digit || prevDigit == -1) {
                pPrevDigit = prevDigit;
                prevDigit = digit;
            } else {
                pPrevDigit = -1;
                prevDigit = digit;
            }
            n /= 10;
        }
        System.out.println("No");
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


Measured memory_bytes: 22356
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        //値を取得
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();   //都市の数
        int M = sc.nextInt();   //道路の本数
        int[] count = new int[N];   //各都市の道路の数を数える配列

        for (int i = 0; i < M * 2; ++i){
            count[sc.nextInt() - 1] += 1;
        }
        for (int i = 0; i < N; ++i){
            //出力
            System.out.println(count[i]);   //各都市から出ている道路の本数を表示
        }
        
    }
}

Measured memory_bytes: 35908
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			list.add(sc.nextInt());
		}
		int m = sc.nextInt();
		List<Integer> list2 = new ArrayList<>();
		for (int i = 0; i < m; i++) {
			list2.add(sc.nextInt());
		}
		int size = Math.min(list.size(), list2.size());
		int res = -1;
		for (int i = 0; i < size; i++) {
			if (list2.get(i) > list.get(i)) {
				res = 1;
				break;
			} else if (list2.get(i) < list.get(i)) {
				res = 0;
				break;
			}
		}
		if (res < 0) {
			if (m > n) {
				res = 1;
			} else {
				res = 0;
			}
		}
		System.out.println(res);
	}
}



Measured memory_bytes: 33252
#include <bits/stdc++.h>
using namespace std;
 
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int maxn = 4009;
const int mod = 998244353;
int c[maxn+1][maxn+1];

void pre(){
    c[0][0] = 1;
    repA(n,1,maxn){
        c[n][0] = c[n][n] = 1;
        repA(k,1,n-1) c[n][k] = (c[n-1][k-1]+c[n-1][k])%mod;
    }
}
 
void solve(){
 
 
}

int f(int n, int k){
    if(n<0||k<0) return 0;
    return c[n][k];
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    vector<ll > p(2009,1);
    repA(i,1,2008) p[i]=p[i-1]*2%mod; 

    int k,n;cin>>k>>n;

    repA(i,2,2*k){
        ll ans=0, h, m;

        if(i>k) h=(2*k+1-i)/2, m=i-k-1+h;
        else h=(i-1)/2, m=k-i+1+h;

        repA(j,0,h) ans += f(n+m-h-1,m-h+j-1)*p[j]%mod*f(h,h-j)%mod, ans%=mod;

        n--;
        if(i%2==0) repA(j,0,h) ans += f(n+m-h-1,m-h+j-1)*p[j]%mod*f(h,h-j)%mod, ans%=mod;
        n++;

        cout<<ans<<endl;
    }

	return 0;
}
 



Measured memory_bytes: 61696
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <locale>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

const long long int INF = 1e9;
typedef long long int ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define Rep(i, n) for(int i = 1; i <= (int)(n); i++)
#define llrep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define llRep(i, n) for(ll i = 1; i <= (ll)(n); i++)

using namespace std;

struct Point{
	int x,y;
};

bool vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return true;
    }
  else { // 発見できなかったとき
    return false;
    }
}

ll factorial(ll N){
    ll ans = 1;
    llRep(i,N){
        ans *= i;
    }
    return ans;
}

ll extended_factorial(ll N){
    ll ans = 1;
    llRep(i,N){
        ans *= i;
        ans %= (ll)(1e9+7);
    }
    return ans;
}

vector<ll> Eratosthenes(ll N){
    bool arr[N+1];
    //initialization
    arr[0] = false;
    arr[1] = false;
	for(ll i = 2; i < N+1; i++){
		arr[i] = true;
	}
    //eratosthenes
	for(ll i = 2; i <= sqrt(N); i++){
		if(arr[i]){
			for(ll j = 0; i * (j + 2) <= N; j++){
				arr[i *(j + 2)] = false;//not prime number
			}
		}
	}
    vector<ll> prime;
    llRep(i,N){
        if (arr[i]==true){
            prime.push_back(i);
        }
    }
    return prime;
}

char alphabet[] = {
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
};

// 二分探索
// llrep(i,N){
//     b = B[i];
//     auto cntA = lower_bound(A.begin(), A.end(), b);//イテレータなのでそのままllに変換とかはできない
//     auto cntC = upper_bound(C.begin(), C.end(), b);
//     ll cntA_ll = distance(A.begin(), cntA);
//     ll cntC_ll = C.size() - distance(C.begin(), cntC);
//     cnt += (cntA_ll * cntC_ll);
// }

int b[22];

int main(){
    int N,M,C,a;
    cin >> N >> M >> C;
    rep(i,M) cin >> b[i];
    int cnt=0;
    rep(i,N){
        int sum=0;
        rep(j,M){
            cin >> a;
            sum += a * b[j];
        }
        sum += C;
        if (sum > 0) cnt ++;
    }
    cout << cnt << endl;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<math.h>
#include<algorithm>

using namespace std;

pair <int, int> p[100010];

int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> p[i].first >> p[i].second;
    sort(p, p + n);
    int sum = p[n - 1].first;
    sum += p[n - 1].second;
    cout << sum << endl;
}


Measured memory_bytes: 1152
#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#include <random>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("-1"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
#define bn(x) ((1<<x)-1)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const ll LINF = 1e18;
const double eps = 1e-10;

const int M = 17, MX = 1<<M;
vi to[MX];
int t[MX];

int dfs(int v, int p=-1) {
  int a = 0, b = 0;
  for (int u : to[v]) {
    if (u == p) continue;
    int r = dfs(u,v);
    b |= a&r;
    a |= r;
  }
  a |= t[b]-1; ++a;
  return a;
}

int main() {
  int n;
  scanf("%d",&n);
  rep(i,n-1) {
    int a,b;
    scanf("%d%d",&a,&b);
    --a; --b;
    to[a].pb(b);
    to[b].pb(a);
  }
  rep(i,M+1) for (int j = 1<<i>>1; j < (1<<i); ++j) t[j] = 1<<i;
  int x = dfs(0);
  int ans = 0;
  while (x > 1) ++ans, x >>= 1;
  cout<<ans<<endl;
  return 0;
}































Measured memory_bytes: 11520
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, count = 0;
  cin >> N;
  
  for (int i = 0; i < N; i++){
    string S;
    cin >> S;
    
    if (S != "Y") continue;
    count++;
  }
  
  if (count == 0) cout << "Three" << endl;
  else cout << "Four" << endl;
}

Measured memory_bytes: 256
/*  -*- coding: utf-8 -*-
 *
 * c.cc: C - 100 to 105
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_X = 100000;
const int N = 6;
const int cs[N] = { 100, 101, 102, 103, 104, 105 };

/* typedef */

/* global variables */

bool dp[2][MAX_X + 1];

/* subroutines */

/* main */

int main() {
  int x;
  scanf("%d", &x);

  dp[0][0] = true;
  int cur = 0, nxt = 1;

  for (int i = 0; ! dp[cur][x] && i < N; i++) {
    int ci = cs[i];
    memcpy(dp[nxt], dp[cur], sizeof(dp[cur]));
    for (int j = 0; j + ci <= x; j++)
      if (dp[cur][j])
	for (int k = j + ci; k <= x; k += ci) dp[nxt][k] = true;
    swap(cur, nxt);
  }

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


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

class UnionFind {
public:
    vector<int> uni;
    UnionFind(int s);
    int root(int a);
    bool connect(int a, int b);
    bool isConnect(int a, int b);
    int size(int a);
};

inline UnionFind::UnionFind(int s) : uni(s, -1) {}

inline int UnionFind::root(int a) {
    if(uni[a] < 0) return a;
    else return uni[a] = root(uni[a]);
}

inline bool UnionFind::connect(int a, int b) {
    a = root(a);
    b = root(b);
    if(a == b) return false;
    if(uni[a] > uni[b]) swap(a, b);
    uni[a] = uni[a] + uni[b];
    uni[b] = a;
    return true;
}

inline bool UnionFind::isConnect(int a, int b) {
    return root(a) == root(b);
}

inline int UnionFind::size(int a) {
    return -uni[root(a)];
}

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    vector<vector<int>> G(N);
    UnionFind uf(N);
    set<pair<int, int>> ng;
    for(int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
        uf.connect(a, b);
        ng.insert(make_pair(a, b));
        ng.insert(make_pair(b, a));
    }
    for(int i = 0; i < K; ++i) {
        int c, d;
        cin >> c >> d;
        c--, d--;
        // 友達候補かつ友達ではない
        if(uf.isConnect(c, d) && ng.count({c, d}) == 0) {
            G[c].emplace_back(d);
            G[d].emplace_back(c);
            ng.insert(make_pair(c, d));
            ng.insert(make_pair(d, c));
        }
    }
    for(int i = 0; i < N; ++i) {
        cout << uf.size(i) - G[i].size() - 1 << " ";
    }
    cout << '\n';
    return 0;
}


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




int32_t main(){
	int n,w;
	cin>>n>>w;
	vector<int> weights(n);
	vector<int> values(n);
	vector<pair<int,int>> ipair(n);
	for(int i=0;i<n;i++){
		cin>>weights[i]>>values[i];
		ipair[i]=make_pair(weights[i],values[i]);
	}
	sort(ipair.begin(),ipair.end());
	vector<vector<long long>> dp(n+1,vector<long long>(w+1,0));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=w;j++){
			int p=ipair[i-1].first;
			int q=ipair[i-1].second;
			if(p<=j){
				dp[i][j]=max(dp[i-1][j],q+dp[i-1][j-p]);
			}
			else{
				dp[i][j]=dp[i-1][j];
			}
		}
	}
	cout<<dp[n][w];
	return 0;
}


Measured memory_bytes: 80256
N,K = list(map(int,input().split()))
H = list(map(int,input().split()))
#print(H)
H.sort()
#print(H)
f = True
for i in range(K):
    try:
        H.pop(-1)
    except:
        break
        f = False
if f:
    print(sum(H))
else:
    print(0)

Measured memory_bytes: 26024
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<int>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define mem(a) memset((a),0,sizeof(a))
#define fi first
#define se second
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
const int N=200005,M=N<<2,inf=1e9;
int Q,p[N],mx,c[N],zs[2];
char ans[N];
struct seg{
	int tr[M];
	void ins(int l,int r,int pos,int de,int nod){
		if(l==r){
			tr[nod]=de; return;
		}
		int mid=(l+r)>>1;
		if(pos<=mid)ins(l,mid,pos,de,nod<<1); else ins(mid+1,r,pos,de,nod<<1|1);
		tr[nod]=max(tr[nod<<1],tr[nod<<1|1]);
	}
	int ask(int l,int r,int pos,int nod){
		if(pos<=l)return tr[nod];
		if(pos>r)return -inf;
		int mid=(l+r)>>1;
		return max(ask(l,mid,pos,nod<<1),ask(mid+1,r,pos,nod<<1|1));
	}
}T[2];
int main(){
	int n=read();
	For(i,1,n){
		p[i]=read(); mx=max(mx,p[i]); 
		c[i]=(p[i]==mx)+1; Q+=p[i]==mx;
	}
	For(i,0,M-1)T[1].tr[i]=-1e9;
	Rep(i,n,1){
		For(j,0,1){
			zs[j]=T[j^(c[i]&1)].ask(1,n,p[i],1)+c[i];
		}
	//	cout<<c[i]<<" "<<zs[1]<<endl;
		For(j,0,1)T[j].ins(1,n,p[i],zs[j],1);
	}
	//string ans=min(solve1(),solve2());
	int cx=0,cy=0,hx=1,hy=1;  //cout<<Q<<" "<<T[1].ask(1,n,1,1)<<endl;
	if(T[(cy+Q-cx)&1].ask(1,n,1,1)<cy+Q-cx){
		puts("-1"); return 0;
	}
	For(i,1,n){
		Q-=c[i]-1;
		For(j,0,1)T[j].ins(1,n,p[i],j?-inf:0,1);
		int CX=cx,CY=cy,HX=hx,HY=hy;
		HX=max(HX,p[i]);
		CX+=HX==p[i];
		//if(i==14)cout<<CX<<" "<<CY<<" "<<HX<<" "<<Q-CX+CY<<" "<<CX+Q-CY<<" "<<T[(CX+Q-CY)&1].ask(1,n,HY,1)<<endl;
		if(CY+Q-CX>=0&&T[(CY+Q-CX)&1].ask(1,n,HX,1)>=CY+Q-CX){
			cx=CX; hx=HX;
			ans[i]='0';
		}else if(CX+Q-CY>=0&&T[(CX+Q-CY)&1].ask(1,n,HY,1)>=CX+Q-CY){
			cx=CX; hx=HX;
			ans[i]='0';
		}else{
			hy=max(hy,p[i]);
			cy+=hy==p[i];
			ans[i]='1';
		}
	}
	cout<<(ans+1)<<endl;
}

Measured memory_bytes: 8448
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const int mod = 1000000000+7;
int main() {
  string s;
  cin >> s;
  int n = sz(s);
  int dp[n][13];
  rep(i,n)rep(j,13)dp[i][j] = 0;
  reverse(rng(s));

  if(s[0] == '?'){
    rep(i,10)dp[0][i]=1;
  }else{
    int id = s[0]-'0';
    dp[0][id] = 1;
  }
  int num = 1;
  srep(i,1,n){
    num*=10;
    num%=13;

    if(s[i] == '?'){
      rep(j,10){
        rep(k,13){
          int id = (k+j*num)%13;
          dp[i][id] +=dp[i-1][k];
          dp[i][id]%=mod;
        }
      }
    }else{
      rep(k,13){
        int id = (k+(s[i]-'0')*num)%13;
        dp[i][id]+=dp[i-1][k];
        dp[i][id]%=mod;
      }
    }
  }
  cout<<dp[n-1][5]<<endl;
  return 0;
}

Measured memory_bytes: 5504
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
using llng = long long;
template<typename T> using v = vector<T>;
template<typename T> using vv = v<v<T> >;

struct Edge {
    int to, cap, rev;
    Edge(int _t, int _c, int _r) : to(_t), cap(_c), rev(_r) {}
};

struct Graph {
    vv<Edge> edge;
    Graph(int N) { edge = vv<Edge>(N); }
    void add(int from, int to, int cap) { add_edge(from, to, cap, false); }
    void add_ud(int v1, int v2, int cap) { add_edge(v1, v2, cap, true); }
private:
    void add_edge(int v1, int v2, int cap, bool isUd) {
        int v1_rev = edge[v2].size();
        int v2_rev = edge[v1].size();
        edge[v1].push_back(Edge(v2, cap, v1_rev));
        edge[v2].push_back(Edge(v1, (isUd ? cap : 0), v2_rev));
    }
};

int main() {
    int N, M; cin >> N >> M;
    Graph g(N+1);
    for (int i=0; i<M; i++) { int a, b; cin >> a >> b; g.add_ud(a, b, 1); }
    v<bool> visited(N+1);
    for (Edge e : g.edge[1]) {
        for (Edge d : g.edge[e.to]) if (!visited[d.to]) {
            if (d.to == N) { cout << "POSSIBLE" << endl; return 0; }
            visited[d.to] = true;
        }
    }
    cout << "IMPOSSIBLE" << endl;
    return 0;
}


Measured memory_bytes: 13544
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
 
public class Main {
	private static FastScanner sc = new FastScanner();
	
	public static void main(String[] args) {
		int N = sc.nextInt();
		int K = sc.nextInt();
		
		int[] A = new int[N];
		int max = 0;
		for(int i=0; i<N; i++) {
			A[i] = sc.nextInt();
			if(A[i] > max) {
				max = A[i];
			}
		}
		
		if(K > max) {
			System.out.println("IMPOSSIBLE");
			return;
		}
		
		for(int j=2; j*j<=max; j++) {
			boolean flag = true;
			for(int k=0; k<N; k++) {
				if(A[k]%j != 0) {
					flag = false;
					break;
				}
			}
			
			if(flag) {
				if(K%j == 0) {
					System.out.println("POSSIBLE");
				} else {
					System.out.println("IMPOSSIBLE");
				}
				return;
			}
		}
		
		System.out.println("POSSIBLE");
	}
	
	static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte() {
            if(ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch(IOException e) {
                    e.printStackTrace();
                }
                if(buflen <= 0) {
                    return false;
                }
            }
            return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
        public boolean hasNext() { skipUnprintable(); return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}

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

int n, m, a[301][301], pc[301], nc[301], c[301];

int can(int x){
	fill(pc + 1, pc + n + 1, 1);
	fill(nc + 1, nc + m + 1, 0);
	int pt = 0, nt = 0;
	while(pt < n && nt < m){
		fill(c + 1, c + m + 1, 0);
		for(int i = 1; i <= n; i++){
			while(pc[i] <= m && nc[a[i][pc[i]]]) pc[i]++;
			if(pc[i] > m) continue;
			c[a[i][pc[i]]]++;
		}
		int t = int(max_element(c + 1, c + m + 1) - c);
		nc[t] = 1;
		nt++;
		if(c[t] <= x){
			for(int i = 1; i <= n; i++){
				if(pc[i] <= m && a[i][pc[i]] == t){
					pc[i] = m + 1;
					pt++;
				}
			}
		}
	}
	return pt == n;
}

int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			scanf("%d", a[i] + j);
		}
	}
	int s = 1, e = n;
	while(s <= e){
		int mi = (s + e) / 2;
		if(can(mi)) e = mi - 1;
		else s = mi + 1;
	}
	printf("%d\n", s);
}

Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <iomanip>
#include <vector>
#include <cstring>
#include <queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uld = unsigned long long;
using P = pair<int,int>;
ll MAX(ll a,ll b,ll c){return max(a,max(b,c));}
ll MIN(ll a,ll b,ll c){return min(a,min(b,c));}
const ll mod = 1e9+7;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
template<class T> inline bool chmax(T &a,T& b){if(a < b){a = b; return true;} else return false;}
template<class T> inline bool chmin(T &a,T& b){if(a > b){a = b; return true;} else return false;}
int main(){
    int a,b;
    cin >> a >> b;
    rep(i,max(a,b)){
        cout << min(a,b);
    }
    cout << endl;
}

Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(){
	int N;
	int ans;
	int temp;
	bool OK=false;
	scanf("%d",&N);
	for(ans=1;ans<=50000;ans++){
		temp = (int)(ans*1.08);
//		printf("%d %d\n",ans,temp);
		if(temp==N){
			printf("%d",ans);
			OK = true;
			break;
		}
	}
	if(OK==false){
		printf(":(");
	}
	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define INF 1000000000
#define NUM 100
#define NIL -1
#define WHITE 0
#define BLACK 1
using namespace std;

int G[NUM][NUM];
int V[NUM];
int color[NUM];
int node_num;
int dist_sum = 0;

void init(){
    for(int i = 0; i < node_num; i++){
        for(int j = 0; j < node_num; j++){
            int a;
            scanf("%d", &a);
            if(a == -1){
                G[i][j] = INF;
            }else{
                G[i][j] = a;
            }
        }
    }

    for(int i = 0; i < node_num; i++){
        V[i] = INF;
        color[i] = WHITE;
    }
}

void updateAround(int n){
    color[n] = BLACK;
    for(int i = 0; i < node_num; i++){
        V[i] = min(V[i], G[n][i]);
    }

    int min_index = NIL;
    int min_value = INF;
    for(int i = 0; i < node_num; i++){
        if(color[i] == BLACK){
            continue;
        }
        if(V[i] < min_value){
            min_value = V[i];
            min_index = i;
        }
    }
    if(min_index != NIL){
        dist_sum += min_value;
        updateAround(min_index);
    }
}

int main(void){
    scanf("%d", &node_num);

    init();
    updateAround(0);

    printf("%d\n", dist_sum);
}



Measured memory_bytes: 3188