akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1000000007LL;
long long GCD(long long a, long long b){return b == 0 ? a : GCD(b, a % b);}

int n;
long long arr[101][5][2];
long long val[101][11];

int freq[101];

long long helper(int day){
    if(day == 5){

        bool found = false;
        long long total = 0;
        for(int i = 0; i < n; ++i){
            if(freq[i] != 0){
                found = true;
            }
            total += val[i][freq[i]];
        }
        if(found == false)
            return -1e9;
        return total;
    }

    long long best = helper(day + 1);

    // P1

    for(int i = 0; i < n; ++i)
        if(arr[i][day][0] == 1)
            freq[i]++;

    best = max(best, helper(day + 1));

    // P1 + P2

    for(int i = 0; i < n; ++i)
        if(arr[i][day][1] == 1)
            freq[i]++;

    best = max(best, helper(day + 1));

    // P2

    for(int i = 0; i < n; ++i)
        if(arr[i][day][0] == 1)
            freq[i]--;

    best = max(best, helper(day + 1));

    for(int i = 0; i < n; ++i)
        if(arr[i][day][1] == 1)
            freq[i]--;

    return best;

}

int main(){

    cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    cin >> n;

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 5; ++j){
            cin >> arr[i][j][0] >> arr[i][j][1];
        }
    }

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 11; ++j){
            cin >> val[i][j];
        }
    }

    cout << helper(0);

    return 0;
}


Measured memory_bytes: 256
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n,d,a = m()

ppp = [0 for i in range(n)]

aaa = []
lll = []
rrr = []
co = 0
for i in range(n):
    p = l()
    aaa.append([p[0],-(-p[1]//a)])
aaa.sort()
for i in range(n):
    lll.append([aaa[i][0],aaa[i][0]+2*d])
c = 0
ll = 0
po = []
for i in range(n):
    on = aaa[i]
    for j in range(ll,n):
        if lll[j][1] < aaa[i][0] and i > j:
            c -= po[j]
        else:
            ll = j
            break
    if on[1] <= c:
        po.append(0)
        continue
    else:
        co += on[1]-c
        po.append(on[1]-c)
        c += on[1]-c
        
        """
        for j in range(i+1,n):
            if aaa[j][0] <= lll[i][1]:
                aaa[j][1] -= on[1]
            else:
                break
        """
        

print(co)










Measured memory_bytes: 103500
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, cur;
struct node { int l, r; } a[maxn];
bool operator <(const node a, const node b) {
    return a.r == b.r ? a.l < b.l : a.r < b.r;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d %d", &a[i].l, &a[i].r);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) {
        cur += a[i].l;
        if (cur > a[i].r) {
            puts("No");
            return 0;
        }
    }
    puts("Yes");
    return 0;
}


Measured memory_bytes: 1792
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for i in range(len(A)):
  ans += B[A[i] - 1]
  
  if i > 0 and A[i] - A[i- 1] == 1:
    ans += C[A[i-1] - 1]

print(ans)

Measured memory_bytes: 3064
d, g = list(map(int, input().split(' ')))

pcd = [None] * d
for i in range(d):
    pcd[i] = list(map(int, input().split(' ')))


ans = float('inf')
for i in range(2 ** d):
    num = 0
    count = 0
    m = -1
    for j in range(d):
        if (i >> j) & 1:
            num += pcd[j][0] * 100 * (j + 1) + pcd[j][1]
            count += pcd[j][0]
        else:
            m = j
    if m != -1:
        k = 0
        while(k < pcd[m][0] and num < g):
            num += (m + 1) * 100
            k += 1
        count += k
    if num >= g:
        ans = min([ans, count])

print(ans)



Measured memory_bytes: 41580
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>

using namespace std;
typedef long long ll;
typedef std::pair<ll, ll> ipair;

const int MAX_E = 1e5 + 1;

class KRUSKAL {

   public: 
      int par[MAX_E];
      int UFTrank[MAX_E];
      struct edge { int u, v, cost; };
      edge es[MAX_E * 2];
      int E, V;

      bool comp(const edge& e1, const edge& e2) {
         return e1.cost < e2.cost;
      }
      
      void init(int e, int v) {
         E = e;
         V = v;
         sort(es, es + E, [this] (edge& e1, edge& e2) { return  comp(e1, e2); } );
         for (int i = 0; i < V; i++) {
            par[i] = i;
            UFTrank[i] = 0;
         }
      }

      int find(int x) {
         if (par[x] == 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 (UFTrank[x] < UFTrank[y]) {
            par[x] = y;
         } else {
            par[y] = x;
            if (UFTrank[x] == UFTrank[y]) UFTrank[x]++;
         }
      }

      bool same(int x, int y) {
         return find(x) == find(y);
      }       

      int getmin() {
         int ans = 0;
         for (int i = 0; i < E; i++) {
            edge e = es[i];
            if (!same(e.u, e.v)) {
               unite(e.u, e.v);
               ans += e.cost;
            }
         }
         return ans;
      }
};

int main() {
   int N;
   cin >> N;
   ll x[N];
   ll y[N];
   map<ipair, int> vtx;
   for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i];
      vtx[ipair(x[i], y[i])] = i;
   }
   map<ipair, int> log;
   for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
         log[ipair(x[j] - x[i], y[j] - y[i])] = 1;
         log[ipair(x[i] - x[j], y[i] - y[j])] = 1;
      }
   }

   int ans = 50;
   for (auto& l : log) {
      if (l.second == 0) continue;
      ll p = l.first.first;
      ll q = l.first.second;
      int V = N;
      KRUSKAL mst;
      int e = 0;
      for (int i = 0; i < N; i++) {
         for (int j = i + 1; j < N; j++) {
            mst.es[e].u = vtx[ipair(x[i], y[i])];
            mst.es[e].v = vtx[ipair(x[j], y[j])];
            if (x[i] + p == x[j] && y[i] + q == y[j])
               mst.es[e].cost = 0;
            else
               mst.es[e].cost = 1;
            e++;
            mst.es[e].v = vtx[ipair(x[i], y[i])];
            mst.es[e].u = vtx[ipair(x[j], y[j])];
            if (x[j] + p == x[i] && y[j] + q == y[i])
               mst.es[e].cost = 0;
            else
               mst.es[e].cost = 1;
            e++;
         }
      }
      int E = e;
      mst.init(E, V);
      int tmp = mst.getmin();
      ans = min(ans, tmp);
   }
   if (N == 1) cout << 1 << endl;
   else cout << ans + 1 << endl;
   return 0;
}


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

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(int n_){    
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void set_val(int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
  }
  T query(int a,int b){
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};

//INSERT ABOVE HERE


template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

signed main(){
  Int n,m;
  cin>>n>>m;
  vector<Int> a(n,0);
  for(Int i=1;i<n;i++) cin>>a[i];
  for(Int i=1;i<n;i++) a[i]+=a[i-1];
  
  auto b=make_v<Int>(m,n);
  for(Int i=0;i<n;i++)
    for(Int j=0;j<m;j++)
      cin>>b[j][i];

  auto dp=make_v<Int>(n+1,n+1);
  fill_v(dp,0);
  
  for(Int i=0;i<m;i++){
    using P = pair<Int, Int>;
    auto f=[&](P a,P b){return max(a,b);};
    SegmentTree<P> seg(f,P(-1,-1));
    vector<P> v;
    for(Int j=0;j<n;j++) v.emplace_back(b[i][j],j);
    seg.build(v);
    
    queue<P> q;
    q.emplace(0,n);
    while(!q.empty()){
      Int l,r;
      tie(l,r)=q.front();q.pop();
      if(l==r) continue;
      Int k=seg.query(l,r).second;
      
      dp[l][k]+=b[i][k];
      dp[l][r]-=b[i][k];
      
      dp[k+1][k]-=b[i][k];
      dp[k+1][r]+=b[i][k];
	    
      q.emplace(l,k);
      q.emplace(k+1,r);
    }
  }

  for(Int i=0;i<=n;i++)
    for(Int j=1;j<=n;j++)
      dp[i][j]+=dp[i][j-1];

  for(Int i=1;i<=n;i++)
    for(Int j=0;j<=n;j++)
      dp[i][j]+=dp[i-1][j];
  
  Int ans=0;
  for(Int i=0;i<n;i++)
    for(Int j=i;j<n;j++)
      chmax(ans,dp[i][j]-(a[j]-a[i]));

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


Measured memory_bytes: 204172
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n-1);i>=0;i--)
#define FOR(i,n,m) for(int i=n;i<=(int)(m);i++)
#define RFOR(i,n,m) for(int i=(int)(n);i>=m;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(std::unique(all(x)), x.end());
typedef long long ll;
const int INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;
const double PI=acos(-1);
using namespace std;
vector<int> dx={1,0,-1,0};
vector<int> dy={0,1,0,-1};
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
int main() {
    int n;
    string s;
    cin>>n>>s;
    rep(i,sz(s)) {
        int x=s[i]-'A';
        x=(x+n)%26;
        s[i]=x+'A';
    }
    cout<<s<<endl;
}



Measured memory_bytes: 384
#include<stdio.h>

int atgc(char c)
{
  if(c=='A'||c=='T'||c=='G'||c=='C'){
    return 1;
  }else{
    return 0;
  }
}

int main(void)
{
  char s[10];
  scanf("%s",&s);
  int head=0,len=0,i;
  for(i=0;s[i]!='\0';i++){
    if(!atgc(s[i])){
      if(i-head>len){
        len=i-head;
      }
      head=i+1;
    }
  }
  if(i-head>len){
    len=i-head;
  }
  
  printf("%d\n",len);
  return 0;
}

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

int main() {
    int n;
    while (cin >> n) {
        if (!n) break;
        int m = 0;

        vector<string> th;
        for (int i=0; i<n; ++i) {
            string s;
            cin >> s;
            m = max(m, (int)s.size());
            
            bool ri = true;
            for (int j=(int)s.size()-1; j>=0; --j) {
                if (s[j] == '.') {
                    s[j] = (ri ? '+' : ' ');
                    ri = false;
                }
            }
            th.push_back(s);
        }

        for (int l=0; l<m; ++l) {
            bool ok = false;
            for (int j=(int)th.size()-1; j>=0; --j) {
                if (l >= (int)th[j].size() || isalpha(th[j][l])) {
                    ok = false;
                } else if (th[j][l] == '+') {
                    ok = true;
                } else if (th[j][l] == ' ' && ok) {
                    th[j][l] = '|';
                }
            }
        }

        for (string s: th) {
            cout << s << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 3404
import sys
input = sys.stdin.readline
N,K = map(int, input().split())
ans = 0
for i in range(1,N+1):
	coin = 0
	point = i
	if K <= i:
		ans += 1/N
	else:
		while point < K:
			point *= 2
			coin += 1
		ans += (1/N) * (0.5)**coin

print(ans)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
#define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

typedef unsigned long long ull;
typedef long long ll;
typedef double db;

#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define pi pair<int,int>
#define vi vector<int>
#define pL pair<ll,ll>
#define vL vector<ll>
#define vp vector<pi>
#define vpL vector<pL>

const int MX=10000+5;
const ll inf=1e9;
const ll mod=1e9+7;

ll dp[10000+5][1000+5];
ll a[MX], b[MX];

ll n;
ll call(ll h, ll p)
{
    if(h<=0) return 0;
    if(p>n)  return inf;
    if(~dp[h][p]) return dp[h][p];
    ll ret=call(h, p+1);
    ret=min(ret, b[p]+call(h-a[p], p));
    return dp[h][p]=ret;
}
int main()
{
    FasterIO;

    ll tc, h;

    cin>>h>>n;
    for(int i=1; i<=n; i++) cin>>a[i]>>b[i];
    mem(dp, -1);
    cout<<call(h, 1)<<endl;
    return 0 ;
}


Measured memory_bytes: 79360
def main():  
  import sys
  input = sys.stdin.readline
  n,p = [int(i) for i in input().split()]
  s = input()
  ans = 0
  
  import math
  def nCr(n, r):
    if n < r:
      return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

  if p == 2 or p == 5:
      for i in range(n):
          if int(s[i]) % p == 0:
              ans += i + 1
      print(ans)
      exit()

  else:
    chk = [0]*p
    chk[0] = 1
    m = 0
    for i in range(n)[::-1]:
      m = (int(s[i])*pow(10,n-1-i,p)+m)%p
      chk[m] += 1
    for i in chk:
      ans += nCr(i,2)
    print(ans)
if __name__ == '__main__':
    main()


Measured memory_bytes: 4140
X = int(input())
if X < 600:
    print(8)
elif X < 800:
    print(7)
elif X < 1000:
    print(6)
elif X < 1200:
    print(5)
elif X < 1400:
    print(4)
elif X < 1600:
    print(3)
elif X < 1800:
    print(2)
else:
    print(1)

Measured memory_bytes: 9112
#[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" ) } ; }

fn main() {
    input! {
        n: u64
    }

    let mut ret = 1;
    for i in 2.. {
        if i * i > n {
            break;
        }
        ret = i;
    }
    println!("{}", ret * ret);
}

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

using namespace std;

int main(){

    int N;
    cin >> N;

    vector<int> A(N,0);
    vector<int> B(N,0);

    long long Sum = 0;

    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
    }
    
    for(int i = 0; i < N; i++){
        B[A[i] - 1]++;
    }

    for(int i = 0; i < N; i++){
        Sum += 0.5 * B[i] * (B[i] - 1);
    }

    for(int k = 0; k < N; k++){
        cout << Sum - B[A[k] - 1] + 1 << "\n";
    }
}

Measured memory_bytes: 4096
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
BC = A[:]
for i in range(1, N):
	BC = [BC[j - 1] if BC[j - 1] < BC[j] else BC[j] for j in range(N)] 
	tempcost = x * i + sum(BC)
	if(ans > tempcost):
		ans = tempcost
	else:
		break
print(ans)


Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,k,n) for(int i=(k);i<(n);i++)
 
struct UnionFind{
    vector<int> parent,size;
    
    UnionFind(int n): parent(n,-1),size(n,1){}
    void init(int n){ 
        parent.assign(n,-1);
        size.assign(n,1);
        return;
    }
 
    int root(int x){
        if (parent[x]<0) return x;
        else return parent[x] = root(parent[x]);
    }
 
    bool same(int x, int y){
        return root(x)==root(y);
    }
 
    void unite(int x,int y){
        int gx = root(x);
        int gy = root(y);
        if (gx==gy) return;
        else{
            if(size[gx]<size[gy]){
                size[gy]+=size[gx];
                parent[gx]=gy;
            } else{
                size[gx]+=size[gy];
                parent[gy]=gx;
            }
        }
    }
    int get_size(int x){
        return size[root(x)];
    }    
};
 
struct edge{
    int u,v;
    ll cost;
};
 
bool comp(const edge& e1, const edge& e2){
    return e1.cost < e2.cost;
}
 
edge es[1000000];
int V,E;
 
ll kruskal(){
    sort(es,es+E,comp);
    UnionFind UF = UnionFind(V);
    ll res = 0;
    FOR(i,0,E){
        edge e = es[i];
        if (!UF.same(e.u,e.v)){
            UF.unite(e.u,e.v);
            res += e.cost;
        }
    }
    return res;
}
 
int main(){
    int N; cin >> N;
    vector<pair<pair<int,int>,int> > v(N),w(N);
    int x,y;
    FOR(i,0,N){
        cin >> x >> y;
        v[i] = make_pair(make_pair(x,y),i);
        w[i] = make_pair(make_pair(y,x),i);
    }
    V = N;
    E = 2 * (N-1);
    sort(v.begin(),v.end());
    sort(w.begin(),w.end());
    FOR(i,0,N-1){
        es[i].u = v[i].second;
        es[i].v = v[i+1].second;
        es[i].cost = (ll) min(abs(v[i+1].first.first-v[i].first.first),abs(v[i+1].first.second-v[i].first.second));
        es[i+N-1].u = w[i].second;
        es[i+N-1].v = w[i+1].second;
        es[i+N-1].cost = (ll) min(abs(w[i+1].first.first-w[i].first.first),abs(w[i+1].first.second-w[i].first.second));
 
    }
    ll ans = kruskal();
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 7424
#include <bits/stdc++.h>
using namespace std;
  
int main(){
  int N= 0;
  string S = "";
  cin >> N >> S;
  int cnt = 0;
  
  
  if(N % 2 == 0){
    int fin = N/2;
    for(int i = 0; i < fin ; i++){
      if(S.at(i) == S.at(N/2 + i)) cnt++;
    }
    
    if(cnt == N/2){
      cout << "Yes" <<endl;
    }
    else{
      cout << "No" << endl;
    }
  }
  else{
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*; 
public class Main {
	static Scanner sc = new Scanner(System.in);
	static int[][] field;
	static int m, n;
	static int[]dx = {-1, 0, 1, 0},dy = {0, 1, 0, -1};
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO ツ篠ゥツ督ョツ青カツ青ャツつウツづェツつスツδソツッツドツ・ツスツタツブ
		
		while(read()){
			slove();
		}
	}
	static boolean read(){
		m = sc.nextInt();
		if(m == 0)
			return false;
		n = sc.nextInt();
		
		field = new int[n+2][m+2];
		
		for(int i = 0; i < field.length; i++)
			fill(field[i], 0);
		
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				field[i][j] = sc.nextInt();
		
		return true;
	}
	static void slove(){
		int ans = 0;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				if(field[i][j] == 1)
					ans = max(ans, rec(j, i, 1));
		System.out.println(ans);
	}
	static int rec(int x, int y, int step){
		int ret = 0;
		for(int i = 0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			if(field[ny][nx] == 1){
				field[y][x] = 0;
				ret = max(ret, rec(nx, ny, step+1));
				field[y][x] = 1;
			}else{
				ret = max(ret, step);
			}
		}
		return ret;
	}

}

Measured memory_bytes: 13000
main = mapM_ (print . solve . map read . words) . lines =<< getContents
solve [n,x] = if x*2>n then solve [n,n-x] else f x (n-x)
f a b
  |a==0 = 0
  |otherwise = 3*a*d + f r a
  where (d,r) = b`divMod`a

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

static vector<int> adj[2005];
static long long sz[2005];
static long long maxDis[2005];
static long long minDis[2005];
string s;

long long ans = 1e16;

void dp(int u, int p){
	maxDis[u] = 0;
	sz[u] = 0;
	long long totalMax = 0;
	long long maxmax = 0;
	long long maxindex = 0;
	for(int v : adj[u]){
		if(v == p) continue;
		dp(v, u);
		sz[u] += sz[v];
		maxDis[u] += maxDis[v];
		if(maxDis[v] + sz[v] > maxmax){
			maxmax = maxDis[v] + sz[v];
			maxindex = v;
		}
		totalMax += maxDis[v] + sz[v];
	}
	maxDis[u] += sz[u];
	
	long long rem = totalMax - maxmax;
	if(maxmax <= rem) minDis[u] = totalMax % 2;
	else{
		if(rem >= minDis[maxindex] + sz[maxindex]) minDis[u] = totalMax % 2;
		else{
			minDis[u] = minDis[maxindex] + sz[maxindex] - rem;
		}
	}
	
	//cout << u + 1 << " " << minDis[u] << " " << maxDis[u] << "\n";
	
	sz[u] += (s[u] == '1');
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n; cin >> n;
	cin >> s;
	for(int i = 1;i < n;i++){
		int a, b; cin >> a >> b; a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	} 
	
	//dp(5,-1);
	//return 0;
	
	for(int i = 0;i < n;i++){
		dp(i, -1);
		if(minDis[i] == 0){
			if(maxDis[i] % 2 == 1){
				//cout << i << " odd\n";
			}
			else{
				//cout << i << " " << maxDis[i] / 2 << "\n";
				ans = min(ans, maxDis[i] / 2);
			}
			
		}
		else{
			//cout << i << " cannot\n";
		}
		
	}
	
	if(ans >= 1e16) ans = -1;
	cout << ans;
}



Measured memory_bytes: 512
#include<stdio.h>
int main(){
	int W,H,x,y,r;
	scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
	if(x-r<0||x+r>W||y-r<0||y+r>H){
		printf("No\n");
	}else{
		printf("Yes\n");
	}
	return 0;
}

Measured memory_bytes: 584
import java.util.*;

class Main
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    int a1 = sc.nextInt();
    int a2 = sc.nextInt();
    int a3 = sc.nextInt();

    int A = a1 + a2 + a3;

    if(A >= 22){
      System.out.println("bust");
    }else{
      System.out.println("win");
    }
  }
}

Measured memory_bytes: 21588
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = tuple(map(int, [input() for _ in range(n)]))
    max_a, max_a2 = sorted(a, reverse=True)[:2]
    print('\n'.join(map(str, [max_a if i != max_a else max_a2 for i in a])))

if __name__ == '__main__':
    main()


Measured memory_bytes: 25124
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
l = []
total = sum(A)

for i in range(len(B)):
    if A[i] >= B[i]:
        t = A[i] - B[i]
        A[i] = t
    else:
        t = B[i] - A[i]
        A[i] = 0
        
        if A[i+1] >= t:
            A[i+1] = A[i+1] - t
        else:
            A[i+1] = 0
print(total - sum(A))

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

// A:0
// B:1
// C:2
// D:3
int rank(double eyeSight){
	if( 1.1 <= eyeSight ){
		return 0;
	}else if(0.6 <= eyeSight){
		return 1;
	}else if(0.2 <= eyeSight){
		return 2;
	}else{
		return 3;
	}
}

main(){
	int lCount[4]={0,0,0,0};
	int rCount[4]={0,0,0,0};

	double l, r;
	while(cin>>l>>r){
		lCount[rank(l)]++;
		rCount[rank(r)]++;
	}
	
	for(int i=0; i<4; ++i){
		printf("%d %d\n",lCount[i],rCount[i]);
	}
}


Measured memory_bytes: 3124
#include<bits/stdc++.h>
using namespace std;
string b;
int a,c;
double d;
stringstream e;
int main(){
    cin>>a>>b;
    e<<b;e>>c;
    d=sqrt(a*pow(10,b.length())+c);
    if(d-(int)d==0)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 384
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
tot=0

for i in range(n):
    if t[i]=='r':
        if i>=k and t[i-k]=='r':
            t[i]='m'
        else:
            tot+=p
    elif t[i]=='s':
        if i>=k and t[i-k]=='s':
            t[i]='m'
        else:
            tot+=r

    else:
        if i>=k and t[i-k]=='p':
            t[i]='m'
        else:
            tot+=s

print(tot)

Measured memory_bytes: 4084
#include<iostream>
#include<string>
using namespace std;
int main() {
	string a, b, c;
	cin >> a >> b >> c;
	if (a[a.size() - 1] == b[0] && b[b.size() - 1] == c[0]) {
		cout << "YES" << endl;
	}
	else cout << "NO" << endl;
}

Measured memory_bytes: 256
import math
xy = []
for i in range(3):
    xy.append([int(item) for item in input().split()])
xy.append(xy[0])

dist = []
for i in range(3):
    distance = math.sqrt((xy[i][0] - xy[i+1][0])**2.0 + (xy[i][1] - xy[i+1][1])**2.0)
    dist.append([distance, i, (i+1) % 3, (i+2) % 3])
dist.sort(reverse=True)

def get_vector(a, b):
    v = [a[0] - b[0], a[1] - b[1]]
    return v

def norm(a):
    n = math.sqrt(a[0] * a[0] + a[1] * a[1])
    return n

a = get_vector(xy[dist[0][-1]], xy[dist[0][-2]])
b = get_vector(xy[dist[0][-3]], xy[dist[0][-2]])
c = get_vector(xy[dist[0][-1]], xy[dist[0][-3]])
d = get_vector(xy[dist[0][-2]], xy[dist[0][-3]])
bc = dist[0][0]

ctheta = (a[0] * b[0] + a[1] * b[1]) / (norm(a) * norm(b))
cphi = (c[0] * d[0] + c[1] * d[1]) / (norm(c) * norm(d))
htheta = (math.pi - math.acos(ctheta)) / 2.0
hphi = (math.pi - math.acos(cphi)) / 2.0
r = bc / (math.tan(htheta) + math.tan(hphi) + 2)
print(r)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using pii = pair<int, int>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define reps(i,n) for(int i = 1; i <= (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define rreps(i,n) for(int i = (int)(n); i > 0; i--)
#define FOR(i,a,b) for(int i = (int)(a); i <= (int)(b); i++)
#define RFOR(i,a,b) for(int i = (int)(a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
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; }
const int INF = INT_MAX;
const int MOD = 1000000007;

int main() {
    int N;
    cin >> N;
    vi A(N), B(N), C(N);
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> B[i];
    rep(i,N) C[i] = i+1;
    int a_n, b_n;
    int cnt = 1;
    do{
        if (A == C) a_n = cnt;
        if (B == C) b_n = cnt;
        cnt++;
    } while (next_permutation(all(C)));
    cout << abs(a_n - b_n) << endl;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct bian
{
	int nxt,to;
}bi[200020];
int n,head[100010],num,ans=0x3f3f3f3f,d[100010],dp[100010][2],cnt[100010];
char c[100010];
inline void add(int from,int to){bi[++num]=bian{head[from],to},head[from]=num;}
void dfs1(int v,int fa)
{
	cnt[v]=c[v]=='W';
	int s1,s0,sum=0,mx=0;
	for(int i=head[v],u;i;i=bi[i].nxt)
	{
		u=bi[i].to;
		if(u==fa)continue;
		dfs1(u,v);
		if(!cnt[u])continue;
		cnt[v]+=cnt[u];++d[v];
		if((d[u]+(c[u]=='W'))&1)s1=dp[u][1]+1,s0=min(dp[u][0]+2,dp[u][1]);
		else s1=dp[u][1]+3,s0=min(dp[u][0],dp[u][1]+2);
		sum+=s1,mx=max(mx,s1-s0);
	}
	if(!cnt[v]&&d[v])dp[v][1]=0,dp[v][0]=2;
	else
	{
		dp[v][1]=sum+((d[v]+(c[v]=='W'))&1);
		if(d[v])dp[v][0]=sum-mx+((d[v]-1+(c[v]=='W'))&1);
		else dp[v][0]=0x3f3f3f3f;
	}
//	printf("%d %d %d\n",v,dp[v][0],dp[v][1]);
}
void dfs2(int v,int fa,int ud,int dp0,int dp1,int ucnt)
{
//	printf("%d %d %d %d %d %d\n",v,fa,ud,dp0,dp1,ucnt);
	int s0,s1,sum=0,mx1=0,mx2=0,ncnt=c[v]=='W',nd=0,ndp0,ndp1,mx;
	if(ucnt)
	{
		ncnt+=ucnt;++nd;
		if((ud+(c[fa]=='W'))&1)s1=dp1+1,s0=min(dp0+2,dp1);
		else s1=dp1+3,s0=min(dp0,dp1+2);
		if(s1-s0>mx1)mx2=mx1,mx1=s1-s0;
		else if(s1-s0>mx2)mx2=s1-s0;
		sum+=s1;
//		printf("%d ",s1);
	}
	for(int i=head[v],u;i;i=bi[i].nxt)
	{
		u=bi[i].to;
		if(u==fa)continue;
		if(!cnt[u])continue;
		ncnt+=cnt[u];++nd;
		if((d[u]+(c[u]=='W'))&1)s1=dp[u][1]+1,s0=min(dp[u][0]+2,dp[u][1]);
		else s1=dp[u][1]+3,s0=min(dp[u][0],dp[u][1]+2);
		if(s1-s0>mx1)mx2=mx1,mx1=s1-s0;
		else if(s1-s0>mx2)mx2=s1-s0;
		sum+=s1;
//		printf("%d ",s1);
	}
//	printf("\n");
	if(!ncnt&&nd)ndp1=0,ndp0=2;
	else
	{
		ndp1=sum+((nd+(c[v]=='W'))&1);
		if(nd)ndp0=sum-mx1+((nd-1+(c[v]=='W'))&1);
		else ndp0=0x3f3f3f3f;
	}
	ans=min(ans,min(ndp1,ndp0));
//	printf("%d %d %d %d %d %d %d\n",sum,mx1,mx2,ncnt,nd,ndp0,ndp1);
//	printf("%d %d %d\n",v,ndp0,ndp1);
	for(int i=head[v],u;i;i=bi[i].nxt)
	{
		u=bi[i].to;
		if(u==fa)continue;
		if(cnt[u])
		{
			ncnt-=cnt[u];--nd;
			if((d[u]+(c[u]=='W'))&1)s1=dp[u][1]+1,s0=min(dp[u][0]+2,dp[u][1]);
			else s1=dp[u][1]+3,s0=min(dp[u][0],dp[u][1]+2);
			if(s1-s0==mx1)mx=mx2;
			else mx=mx1;
			sum-=s1;
		}
		else mx=mx1;
		if(!ncnt&&nd)ndp1=0,ndp0=2;
		else
		{
			ndp1=sum+((nd+(c[v]=='W'))&1);
			if(nd)ndp0=sum-mx+((nd-1+(c[v]=='W'))&1);
			else ndp0=0x3f3f3f3f;
		}		
		dfs2(u,v,nd,ndp0,ndp1,ncnt);
		if(cnt[u])
		{
			ncnt+=cnt[u];++nd;
			if((d[u]+(c[u]=='W'))&1)s1=dp[u][1]+1,s0=min(dp[u][0]+2,dp[u][1]);
			else s1=dp[u][1]+3,s0=min(dp[u][0],dp[u][1]+2);
			sum+=s1;
		}
	}
}
int main()
{
	scanf("%d",&n);
	int x,y;
	for(int i=1;i<n;++i)
	{
		scanf("%d %d",&x,&y);
		add(x,y),add(y,x);
	}
	scanf("%s",c+1);
	dfs1(1,0);
	dfs2(1,0,0,0,0,0);
	printf("%d",ans);
	return 0;
}


Measured memory_bytes: 12672
#include <bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)
using ll = std::int64_t;
using ull = std::uint64_t;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
#define mv_rec make_v(init, tail...)
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, size_t s, Tail... tail) { return vec<decltype(mv_rec)>(s, mv_rec); }
#undef mv_rec

using namespace std;

int main() {
    string s;
    cin >> s;
    string t = "hi";
    string sum = t;
    while (sum.size() <= s.size()) {
        if (sum == s) {
            cout << "Yes" << endl;
            return 0;
        }
        sum += t;
    }
    cout << "No" << endl;
    return 0;
}


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

#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int MAX_N = 100000;
ll n;
ll a[MAX_N+1];
ll sum[MAX_N+1];

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];

  for (int i = 1; i <= n; ++i) sum[i] = sum[i-1] + a[i];

  // s[1] < 0
  ll ans1 = 0;
  ll op1 = 0;
  for (int i = 1; i <= n; ++i) {
    if (i%2 == 1) {
      if (sum[i]+op1 >= 0) {
        ans1 += sum[i]+op1+1;
        op1 -= sum[i]+op1+1;
      }
    } else {
      if (sum[i]+op1 <= 0) {
        ans1 += abs(sum[i]+op1)+1;
        op1 += abs(sum[i]+op1)+1;
      }
    }
  }
  // s[1] > 0
  ll ans2 = 0;
  ll op2 = 0;
  for (int i = 1; i <= n; ++i) {
    if (i%2 == 0) {
      if (sum[i]+op2 >= 0) {
        ans2 += sum[i]+op2+1;
        op2 -= sum[i]+op2+1;
      }
    } else {
      if (sum[i]+op2 <= 0) {
        ans2 += abs(sum[i]+op2)+1;
        op2 += abs(sum[i]+op2)+1;
      }
    }
  }

  cout << min(ans1, ans2) << endl;

  return 0;
}


Measured memory_bytes: 1792
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <cstdio>
#include <time.h>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <math.h>
//#include <bits/stdc++.h>
#define ll long long
#define int64 long long
#define mem(x,y) memset(x,y,sizeof(x))
#define ull unsigned long long
#define pb push_back
#define INF 0x3f3f3f3f
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define pi 3.141592653589793238462
#define stdio std::ios::sync_with_stdio(false)

using namespace std;

inline bool nextDouble(double &num) {
    char in;
    double Dec = 0.1;
    bool IsN = false, IsD = false;
    in = getchar();
    if (in == EOF) return false;
    while (in != '-' && in != '.' && (in < '0' || in > '9'))
        in = getchar();
    if (in == '-') {
        IsN = true;
        num = 0;
    } else if (in == '.') {
        IsD = true;
        num = 0;
    } else num = in - '0';
    if (!IsD) {
        while (in = getchar(), in >= '0' && in <= '9') {
            num *= 10;
            num += in - '0';
        }
    }
    if (in != '.') {
        if (IsN) num = -num;
        return true;
    } else {
        while (in = getchar(), in >= '0' && in <= '9') {
            num += Dec * (in - '0');
            Dec *= 0.1;
        }
    }
    if (IsN) num = -num;
    return true;
}
inline bool nextInt(int &num) {
    char in;
    bool IsN = false;
    in = getchar();
    if (in == EOF) return false;
    while (in != '-' && (in < '0' || in > '9')) in = getchar();
    if (in == '-') {
        IsN = true;
        num = 0;
    } else num = in - '0';
    while (in = getchar(), in >= '0' && in <= '9') {
        num *= 10, num += in - '0';
    }
    if (IsN) num = -num;
    return true;
}
inline bool nextLong(ll &num) {
    char in;
    bool IsN = false;
    in = getchar();
    if (in == EOF) return false;
    while (in != '-' && (in < '0' || in > '9')) in = getchar();
    if (in == '-') {
        IsN = true;
        num = 0;
    } else num = in - '0';
    while (in = getchar(), in >= '0' && in <= '9') {
        num *= 10, num += in - '0';
    }
    if (IsN) num = -num;
    return true;
}
inline void Out(int a) {
    if (a < 0) {
        putchar('-');
        a = -a;
    }
    if (a >= 10) {
        Out(a / 10);
    }
    putchar(a % 10 + '0');
}
const int maxn = 1e6 + 5;
const int maxm = 1e3 + 5;
const int mod = 1e9 + 7;
const int seed = 10007;
const double eps = 1e-6;
int a,b,c;

int main() {
    nextInt(a);
    nextInt(b);
    nextInt(c);
    int d1=abs(a-b);
    int d2=abs(a-c);
    if(d1<d2) puts("A");
    else puts("B");
    return 0;
}


Measured memory_bytes: 256
import math
import collections

def prime_factorize(n):
    s = []
    while n % 2 == 0:
        s.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            s.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        s.append(n)
    return s

a, b = map(int, input().split())

g = math.gcd(a, b)
c = collections.Counter(prime_factorize(g))

print(len(c.values()) + 1)

Measured memory_bytes: 9420
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
const ll INF = ll(1e18) + 1;
int main(void) {
    int n; ll k;
    cin >> n >> k;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    sort(a.begin(), a.end());
    ll l = -INF, r = INF;
    while(l+1 < r) {
        ll x = (l+r)/2;
        bool ok = [&]{
            ll tot = 0;
            rep(i, n) {
                if(a[i] < 0) {
                    int l = -1, r = n;
                    while(l+1 < r) {
                        int c = (l+r)/2;
                        if(a[c]*a[i] < x) r = c;
                        else l = c;
                    }
                    tot += n-r;
                }else{
                    int l = -1, r = n;
                    while(l+1 < r) {
                        int c = (l+r)/2;
                        if(a[c]*a[i] < x) l = c;
                        else r = c;
                    }
                    tot += r;
                }
                if(a[i]*a[i] < x) tot--;
            }
            tot /= 2;
            return tot < k;
        }();
        if(ok) l = x; else r = x;
    }
    cout << l;
}



Measured memory_bytes: 1920
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>

#define MAX(a,b) ((a)<(b) ? (a) : (b))
#define MIN(a,b) ((a)>(b) ? (a) : (b))


using namespace std;



int main(){
	int n,c;
	c = 0;
	cin>>n;
	vector<int> a(n);
	
	for (int i = 0; i < n; i++) {
		cin>>a[i];
		if (a[i]%2==1) {
			c++;
		}
	}
	if (c%2==0) {
		cout<<"YES"<<endl;
	} else {
		cout<<"NO"<<endl;
	}

}

Measured memory_bytes: 640
import Control.Monad
import Data.List
g = (\[a,b] [c,d] -> (abs (a-c))+(abs(b-d))) 
f a c=if null a then [] else [head(elemIndices ((minimum.map (g (head a))) c) (map (g (head a)) c))]++f(tail a)c
main=do
 [n,m]<-map read.words<$>getLine
 a<-map (map read).map words<$>replicateM n getLine
 c<-map (map read).map words<$>replicateM m getLine
 putStrLn$init$unlines$map (show.(+1))$f a c

Measured memory_bytes: 1020
#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b,q;
  cin>>a>>b>>q;
  vector<int64_t> s(a);
  for(int i=0;i<a;i++)
    cin>>s.at(i);
  vector<int64_t> t(b);
  for(int i=0;i<b;i++)
    cin>>t.at(i);
  for(int _=0;_<q;_++){
    int64_t x;
    cin>>x;
    int64_t sl=-1,sr=-1,tl=-1,tr=-1;
    auto bs=lower_bound(s.begin(),s.end(),x);
    if(bs!=s.begin())
      sl=x-*(bs-1);
    if(bs!=s.end())
      sr=*bs-x;
    auto bt=lower_bound(t.begin(),t.end(),x);
    if(bt!=t.begin())
      tl=x-*(bt-1);
    if(bt!=t.end())
      tr=*bt-x;
    int64_t u=max({sl,sr,tl,tr});
    sl=(sl==-1?u*3:sl);
    sr=(sr==-1?u*3:sr);
    tl=(tl==-1?u*3:tl);
    tr=(tr==-1?u*3:tr);
    cout<<min({max(sl,tl),sr+tl+min(sr,tl),sl+tr+min(sl,tr),max(sr,tr)})<<endl;
  }
}

Measured memory_bytes: 4756
#include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define iter(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define riter(i, c) for (auto i = (c).rbegin(); i != (c).rend(); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W, N;
    cin >> H >> W >> N;
    cout << (N + max(H, W) - 1) / max(H, W) << endl;
    return 0;
}

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

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    long long ans=0,sum=0;
    for(int i=0;i<N;++i){
            cin >> A.at(i);
            A.at(i) -= i+1;
        }
    
    sort(A.begin(), A.end());
    int median = A.at(N/2);
    for(int i=0;i<N;++i) ans += abs(A.at(i)- median);
    cout << ans << endl;
}


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

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;

const long long INF = 1LL << 60;

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

    vector<int> A(N + 1, 0), B(N + 1, 0);
    repd(i, 1, N + 1) cin >> A[i];

    int num = 0, end = 0;

    for (int i = N; i >= 1; i--) {
        int cnt = 0;
        for (int j = i * 2; j <= N; j += i) {
            cnt += B[j];
        }
        if (cnt % 2 == A[i]) {
            B[i] = 0;
        }
        else {
            B[i] = 1;
        }
        if (B[i] == 1) {
            num++;
            if (end == 0) end = i;
        }
        /*
        repd(i, 1, N + 1) {
            cout << B[i] << " ";
        }
        cout << "cnt " << cnt << endl;
        */
    }

    cout << num << endl;

    if (num != 0) {
        repd(i, 1, N + 1) {
            if (B[i] && end != i) cout << i << " ";
        }
        cout << end << endl;
    }
    return 0;
}

Measured memory_bytes: 2432
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(u) begin(u),end(u)
#define PB push_back
#define LE(n,m) ((n) < (m) + EPS)
#define GE(n,m) ((n) + EPS > (m))
#define EQ(n,m) (abs((n)-(m)) < EPS)

typedef long long int ll;
const int INF = (1<<30) - 1;
const double EPS = 1e-9;
const int MOD = 1000000007;

//g++ -std=c++0x -msse4.2 -O3
//#include <bits/stdc++.h>
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout.precision(16);
    //cout.setf(ios::fixed);

    int n, r;
    while(cin >> n >> r, n|r){
        vector<int> pile(n);
        for(int i=0; i<n; i++)
            pile[i] = n-i;
        
        for(int _=0; _<r; _++){
            int p, c;
            cin >> p >> c;
            vector<int> t(n);
            for(int i=0; i<c; i++)
                t[i] = pile[p-1+i];
            for(int i=0; i<p-1; i++)
                t[i+c] = pile[i];
            for(int i=p-1+c; i<n; i++)
                t[i] = pile[i];
            pile = t;
        }
        
        cout << pile[0] << endl;
    }
    
    return 0;
}

Measured memory_bytes: 3132
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define ws ____ws
int u[108000];
int r(int x){
	if(x == u[x])return x;
	return u[x] = r(u[x]);
}

void unite(int a, int b){
	a = r(a);
	b = r(b);
	if(a != b)
	u[a] = b;
}               

vector<int> ws[108000], hs[108000];

int main(){
	int n, w, h;
	cin >> n >> w >> h;
	int cnt = n, res, x, y;
	bool edge = false;
	for(int i = 0;i < n;i++){
		cin >> x >> y ;
		if(x == w || y == h || x == 1 || y == 1)edge=true;
		ws[x].push_back(i);
		hs[y].push_back(i);
	}
	
	for(int i = 0;i < n;i++)u[i] = i;
	for(int i = 0;i <= 100000;i++){
		for(int j = 0;j < ws[i].size();j++){
			if(r(ws[i][j]) != r(ws[i][0]))cnt--;
			unite(ws[i][j], ws[i][0]);
		}
		for(int j = 0;j < hs[i].size();j++){    
			if(r(hs[i][j]) != r(hs[i][0]))cnt--;
			unite(hs[i][j], hs[i][0]);
		}
	}
	
	res = n - cnt;
	if(cnt > 1)res += cnt * 2 - 1 - edge;
	cout << res << endl;
	return 0;	
}

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

#define ll long long

ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

inline void out (int x)
{
	if (x>9) out(x/10);
	putchar (x%10+'0');
}

struct edge{
	int to,pre;
	ll w;
}e[200020];
int last[100010];
int sz;

inline void ins (int u,int v,ll w)
{
	e[++sz].to=v;
	e[sz].pre=last[u];
	e[sz].w=w;
	last[u]=sz;
}

int n;
int q[1000010];
bool vis[100010];
ll dis[100010];
int k;

void work ()
{
	for (int i=1;i<=n;i++) dis[i]=9223372036854775807ll;
	int head,tail;
	head=tail=1;
	q[1]=k; vis[k]=1;
	dis[k]=0;
	while (head<=tail)
	{
		int now=q[head++]; vis[now]=0;
		for (int i=last[now];i;i=e[i].pre)
		{
			int to=e[i].to;
			if (dis[to]>dis[now]+e[i].w)
			{
				dis[to]=dis[now]+e[i].w;
				if (!vis[to])
				{
					vis[to]=1; q[++tail]=to;
				}
			}
		}
	}
}


void doing ()
{
	n=read();
	int u,v; ll w;
	for (int i=1;i<n;i++)
	{
		u=read(); v=read(); w=read();
		ins(u,v,w);
		ins(v,u,w);
	}
	int q;
	q=read(); k=read();
	work();
	while (q--)
	{
		u=read(); v=read();
		cout<<dis[u]+dis[v]<<endl;
	}
}

int main ()
{
	//freopen("xx.in","r",stdin);
	//freopen("xx.out","w",stdout);
	doing ();
	return 0;
}



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

int main() {
	char s[201], p[101],t[101];
	
	scanf("%s %s", s, p);
	strcpy(t, s);
	strcat(s, t);
	if (strstr(s, p) == NULL ) printf("No\n");
	else printf("Yes\n");
	return 0;
}

Measured memory_bytes: 972
def f(a0,a1, N,s, A):
    A[0] = a0
    A[1] = a1
    WOLF = -1
    SHEEP = 1
    for i in range(2,N):
        if s[i-1] == "o" and A[i-1] == WOLF:
            A[i] = -A[i-2]
        elif s[i-1] == "o" and A[i-1] == SHEEP:
            A[i] = A[i-2]
        elif s[i-1] == "x" and A[i-1] == SHEEP:
            A[i] = -A[i-2]
        elif s[i-1] == "x" and A[i-1] == WOLF:
            A[i] = A[i-2]
    for i in range(N):
        same = A[(i-1)%N] == A[(i+1)%N]
        if A[i] == WOLF:
            if  s[i] == "o" and same or s[i] == "x" and not same:
                return False
        if A[i] == SHEEP:
            if  s[i] == "x" and same or s[i] == "o" and not same:
                return False
    return True
                
        

def main():
    N = int(input())
    s = input()
    A = [0]*N
    if f(1,1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))

    elif f(-1,1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))

    elif f(1,-1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))

    elif f(-1,-1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))
    else:
        print(-1)


main()

Measured memory_bytes: 4888
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i,n) for((i)=0;(i)<(n);(i)++)
#define per(i,n) for((i)=(n)-1;(i)>=0;(i)--)
#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
typedef long long ll;
typedef long double ld;
const ll MOD1 = 1000000007;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main(){
  ll n,m,k,i,j,result=0,wMax,sum=0;
  int w[110]={0},v[110]={0};
  string s;
  cin >> n >> wMax;
  rep(i,n){
    cin >> w[i] >> v[i];
    sum += (ll)v[i];
  }
  vector<ll> dp(sum+1,MOD1);//価値jのときの重さのmin
  
  dp[0]=0;
  rep(i,n){
    per(j,sum+1){
      dp[j+v[i]] = min(dp[j+v[i]],dp[j]+w[i]);
    }
  }
  per(j,sum+1){
    if(dp[j]<=wMax){
      cout << j << endl;
      break;
    }
  }
  return 0;
}

Measured memory_bytes: 3960
import java.util.*;

class Main {

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int n= s.nextInt();
        Set<Integer> set=new HashSet<>();
        for (int i = 0; i <n ; i++) {
            int a= s.nextInt();
            if(set.contains(a)){
                System.out.println("NO");
                return;
            }
            set.add(a);
        }
        System.out.println("YES");

    }

}

Measured memory_bytes: 100320