akhauriyash/Code-Regression huggingface dataset:
# -*- coding: utf-8 -*-
import sys
buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo


class Doubling():
    def __init__(self, N, a0):
        self.N = N
        self.nt = [[None] * N for i in range(N.bit_length())]
        for i, a in enumerate(a0):
            self.nt[0][i] = a

        for i in range(1, len(self.nt)):
            for j in range(N):
                if self.nt[i-1][j] is None:
                    self.nt[i][j] = None
                else:
                    self.nt[i][j] = self.nt[i-1][self.nt[i-1][j]]

    def apply(self, i, n):
        j = i
        for k in range(n.bit_length()):
            m = 1 << k
            if m & n:
                j = self.nt[k][j]
            if j is None:
                break
        return j


def slv(N, X, L, Q, AB):
    r = [0]
    for x in X:
        t = -1
        for j in range(r[-1], len(X)):
            if X[j] - x <= L:
                t = j
            else:
                break
        r.append(t)
    r.pop(0)

    d = Doubling(N, r)

    ans = []
    for a, b in AB:
        if a > b:
            a, b = b, a
        a -= 1
        b -= 1
        bisect = Bisect(lambda x: d.apply(a, x))
        i = bisect.bisect_left(b, 0, len(X)+1)
        ans.append(i)

    return ans


def main():
    N = read_int()
    X = read_int_n()
    L = read_int()
    Q = read_int()
    AB = [read_int_n() for _ in range(Q)]
    print(*slv(N, X, L, Q, AB), sep='\n')


if __name__ == '__main__':
    main()


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

#define int long long

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

/*
#define cin ifs
#define cout ofs
ifstream ifs("in.txt");
ofstream ofs("out.txt");
//*/

struct point {
    int n, pr;
};

int R, C, N;
vector<point> a, b, c, d;
vector<int> all;
stack<int> stk;

signed main() {
    cin >> R >> C >> N;
    for (int i = 0; i < N; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if ((x1 == 0 || y1 == 0 || x1 == R || y1 == C) &&
            (x2 == 0 || y2 == 0 || x2 == R || y2 == C)) {
            if (x1 == 0 && y1 > 0) {
                a.push_back({i, y1});
            } else if (y1 == C && x1 > 0) {
                b.push_back({i, x1});
            } else if (x1 == R && y1 < C) {
                c.push_back({i, -y1});
            } else if (y1 == 0 && x1 < R) {
                d.push_back({i, -x1});
            }
            if (x2 == 0 && y2 > 0) {
                a.push_back({i, y2});
            } else if (y2 == C && x2 > 0) {
                b.push_back({i, x2});
            } else if (x2 == R && y2 < C) {
                c.push_back({i, -y2});
            } else if (y2 == 0 && x2 < R) {
                d.push_back({i, -x2});
            }
        }
    }
    sort(a.begin(), a.end(), [](point l, point r) { return l.pr < r.pr; });
    sort(b.begin(), b.end(), [](point l, point r) { return l.pr < r.pr; });
    sort(c.begin(), c.end(), [](point l, point r) { return l.pr < r.pr; });
    sort(d.begin(), d.end(), [](point l, point r) { return l.pr < r.pr; });
    for (int i = 0; i < a.size(); i++) {
        all.push_back(a[i].n);
    }
    for (int i = 0; i < b.size(); i++) {
        all.push_back(b[i].n);
    }
    for (int i = 0; i < c.size(); i++) {
        all.push_back(c[i].n);
    }
    for (int i = 0; i < d.size(); i++) {
        all.push_back(d[i].n);
    }
    all.push_back(-1);
    stk.push(-1);
    for (int i = 0; i < all.size(); i++) {
        if (all[i] == stk.top()) {
            stk.pop();
        } else {
            stk.push(all[i]);
        }
    }
    if (stk.empty()) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
    return 0;
}

Measured memory_bytes: 7212
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

bool fichk(vector<bool> &f, string s){
  if((s.at(0) == 'x') ^ f.at(0)){
    return f.at(1) == f.at(f.size()-2);
  }else{
    return f.at(1) != f.at(f.size()-2);
  }
}

bool fc(vector<bool> &f, string s){
  int n = s.size();
  rep2(i, 1, n){
    bool c = (s.at(i) == 'x');
    if((f.at(i) ^ c) == true){
      f.at(i+1) = f.at(i-1);
    }else{
      f.at(i+1) = !f.at(i-1);
    }
  }
  // rep(i, f.size()) cerr << f.at(i);
  // cerr << endl;
  if(f.at(0) == f.at(n) && fichk(f, s)) return true;
  return false;
}

void ap(vector<bool> &f){
  rep(i, f.size()-1){
    char o;
    if(f.at(i) == true) o = 'S';
    else o = 'W';
    cout << o;
  }
  cout << endl;
}

int main(){
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<bool> f(n+1);
  f.at(0) = true; f.at(1) = true;
  if(fc(f, s)){
    ap(f);
    return 0;
  }
  f.at(0) = true; f.at(1) = false;
  if(fc(f, s)){
    ap(f);
    return 0;
  }
  f.at(0) = false; f.at(1) = true;
  if(fc(f, s)){
    ap(f);
    return 0;
  }
  f.at(0) = false; f.at(1) = false;
  if(fc(f, s)){
    ap(f);
    return 0;
  }
  cout << -1 << endl;
  return 0;
}

Measured memory_bytes: 3728
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// 173_D
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();

		Long A[] = new Long[N];
		Long Ans = (long) 0;

		for(int i=0; i<N; i++) {
			A[i] = sc.nextLong();
		}
	    // Scannerクラスのインスタンスをクローズ
		sc.close();

		Arrays.sort(A, Collections.reverseOrder());

		for(int i=0; i<N-1; i++) {
			Ans += A[(i+1)/2];
		}

		System.out.println(Ans);

	}
}


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

typedef long long ll;
typedef int64_t i6;

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 1; i <= n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define Sort(x) sort(ALL(x))
#define Sort_rev(x) Sort(x);reverse(ALL(x))

template<typename V,typename T>
T find_num(V v, T num) {
    if ( find(ALL(v), num) == v.end() ) {
        return false;
    } else {
        return true;
    }
}

// π M_PI
// deg = rad*180/M_PI

int main() {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    if ( s.length() <= n) {
        cout << s << endl;
    } else {
        rep(i, n) {
            cout << s[i];
        }
        cout << "..." << endl;
    }

    return 0;
}


Measured memory_bytes: 3624
import java.util.*;
import java.lang.*;


class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double a = sc.nextDouble();
		double b = sc.nextDouble();
		double c = sc.nextDouble();
		
		System.out.println(a * b / 2 * Math.sin(Math.toRadians(c)));
		System.out.println(a + b + Math.sqrt(Math.pow(a,2) + Math.pow(b, 2) - 2 * a * b * Math.cos(Math.toRadians(c))));
		System.out.println(b * Math.sin(Math.toRadians(c)));
		
	}
}


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

int main(){
    string s,t;
    cin >> s >> t;
    string ss = s+s;
    
    for (int i = 0; i < t.size(); i++){
        string st = "";
        for (int j = 0; j < t.size(); j++){
            st += ss[i+j];
        }
        if (st == t){
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
	int i, n;
	int a[64];

	scanf("%d", &n);
	for (i = 0; i <= n; i++)
	{
		if (i == 0 || i == 1)
			a[i] = 1;
		else
			a[i] = a[i - 1] + a[i - 2];
	}
	printf("%d\n", a[n]);
	return 0;
}


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

int main() {
    int N, cnt = 0; cin >> N;
    for (int i = 1; i < sqrt(N)+1; i++) {
        for (int k = 1; k < N; k++) {
            if (k * i < N) {
                cnt++;
                if (k != i && sqrt(N)+1 <= k) cnt++;
            } else break;
        }
    }
    cout << cnt << endl;
}

Measured memory_bytes: 3676
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> tree(n, vector<pair<int, int>>());
  rep(i, n-1){
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    tree.at(a).push_back({b, c});
    tree.at(b).push_back({a, c});
  }

  queue<int> q;
  vector<ll> d(n, -1);
  int qe, k;
  cin >> qe >> k;
  k--;
  d.at(k) = 0;
  q.push(k);
  while(!q.empty()){
    auto now = q.front(); q.pop();
    for(auto p : tree.at(now)){
      if(d.at(p.first) != -1) continue;
      d.at(p.first) = d.at(now) + p.second;
      q.push(p.first);
    }
  }

  rep(_q, qe){
    int x, y;
    cin >> x >> y;
    x--; y--;
    ll ans = d.at(x) + d.at(y);
    cout << ans << endl;
  }
  return 0;
}

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

using ll = long long int;
const int mod = 1e9+7;
const int inf = 1e9+5;
//int best = 0;
map<pair<int, int>, int>mp;
int solve(vector<vector<int>>&arr, int i, int prv){
	if(i == (int)arr.size()){
		return 0;
	}
	if(mp.count({i, prv})){
		return mp[{i, prv}];
	}
	int score, best = 0;
	for(int j = 0; j < 3; j++){
		if(prv != j){
			//prv = j; #warning bugs
			//score = arr[i][j] + solve(arr, i+1, prv); 
			score = arr[i][j] + solve(arr, i+1, j);
			best = max(score, best);
		}
	}
	return mp[{i, prv}] = best;
}

int main(){
	int n;
	cin>>n;
	vector<vector<int>>arr(n, vector<int>(3));
	for(int i = 0; i < n; i++){
		for(int j = 0; j < 3; j++){
			cin>>arr[i][j];
		}
	}
	int ans = solve(arr, 0, -1);
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 36992
#!/usr/bin/env python
# coding: utf-8

# In[9]:


N = int(input())
p = list(map(int, input().split()))


# In[10]:


ans = 0
cnt = 0
while cnt < N:
    if p[cnt] == cnt+1:
        ans += 1
        cnt += 2
    else:
        cnt += 1
print(ans)


# In[ ]:






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

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) 0
#endif
#define pb push_back
#define mod 1000000007
#define endl '\n'
typedef int integer;
#define int long long
using namespace std;

int powmod(int a, int b) {
    int res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1)res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}

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

const integer NN = 200005;
int ar[NN];
int n, m, k;


integer main() {
    ios_base::sync_with_stdio(false);

    int t = 1;
//    cin >> t;
    while (t--) {
        cin >> n;
        int ans = 0;
        for (int i = 1; i <= n;i++){
            if (i % 3 != 0 && i % 5 != 0){
                ans +=i;
            }
        }
        cout << ans << endl;
    }

}



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

int max(int a, int b) {
  if (a > b) return a;
  else return b;
}

int main() {
  string s, t;
  cin >> s >> t;
  int len_s = s.size();
  int len_t = t.size();
  int dp[len_s+1][len_t+1] = {};
  for (int i=1; i<=len_s; i++) {
    for (int j=1; j<=len_t; j++) {
      if (s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
      else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
    }
  }
  string ans = "";
  int x = len_s, y = len_t;
  while (x > 0 && y > 0) {
    if (dp[x][y] == dp[x-1][y]) {
      x--;
    } else if (dp[x][y] == dp[x][y-1]) {
      y--;
    } else {
      ans = s[x-1] + ans;
      x--; y--;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 35456
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n],b[n];
  for(int i=1;i<=n;i++)
  {
    cin>>a[i];
    b[a[i]]=i;
  }
  for(int i=1;i<=n;i++)
  {
    cout<<b[i]<<" ";
  }
}

Measured memory_bytes: 1536
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
#define int long
#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
const int INF = 1e9;
using namespace std;

signed main() {
    string S; cin >> S; int cnt=0;
    vector<pair<char,int>> p;
    rep(i,S.size()){
       if(S[i]=='x') continue;
       else p.push_back(make_pair(S[i],i));
    }
    bool can = true;
    rep(i,p.size()/2){
        if(p[i].first!=p[p.size()-1-i].first) can = false;
    }
    if(can){
        rep(i,(p.size()%2==0 ? p.size()/2 : p.size()/2+1)){
            if(i==0){
                cnt+=abs(p[0].second-((int)S.size()-1-p[p.size()-1].second));
            }
            else {
                cnt+=abs((p[i].second-p[i-1].second)-
                (p[p.size()-i].second-p[p.size()-1-i].second));
            }
        }
    }
    if(can) cout << cnt << endl;
    else cout << -1 << endl;
}


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

int main(){
    string w;
    cin >> w;
    map<char, int> m;
    for(auto&& c : w)
    {
        m[c]++;
    }
    bool isAllEven = true;
    for(auto&& p : m)
    {
        if (p.second % 2 == 1) {
            isAllEven = false;
            break;
        }
    }
    cout << (isAllEven ? "Yes" : "No") << "\n";
}

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

int main(){

 long N;
 long A[100001];
 int ans = 0;
 scanf("%ld", &N);
 for(int i=0; i<N; i++){
   scanf("%ld", &A[i]);
 }

 for(int j=0; j<N; j++){
   if(A[j]%2 != 0){
     printf("first\n");
     exit(0);
   }
 }

 printf("second\n");
  return 0;
}


Measured memory_bytes: 896
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main() {
    int H,W;
    cin >> H >> W;
    char a[H][W];
    for (int i = 0;i<H;i++){
        for (int j = 0;j<W;j++)
        cin >> a[i][j];
    }
    vector<int> h,w;
    for (int i = 0;i < H;i++){
        for(int j = 0;j<W;j++){
            if(a[i][j] == '#'){
                h.push_back(i);
                break;
            }
        }
    }
    for (int i = 0;i < W;i++){
        for(int j = 0;j<H;j++){
            if(a[j][i] == '#'){
                w.push_back(i);
                break;
            }
        }
    }
    for (int i = 0;i<h.size();i++){
        for(int j = 0;j<w.size();j++){
            cout << a[h[i]][w[j]] ;
        }
        cout << endl;
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()




using namespace std;

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

  if(S.size() <= K){
    cout<<S<<endl;
  } else {
    for(int i=0;i<K;i++){
      cout<<S[i];
    }
    cout<<"..."<<endl;
  }

  return 0;
}

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

#define REP(i,n) for(ll i=0;i<=ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
#define ALL(x) x.begin(),x.end() 
#define SIZE(x) ll(x.size()) 

int main(void)
{
    ll num, sum;
    cin >> num >> sum;

    REP(i, num) {
        REP(j,num) {
            ll k = num - i - j;
            if ((k >= 0) && (i+j+k == num) && (10000*i + 5000*j + 1000*k == sum)) {
                cout << i << " " << j << " " << k << endl;
                return 0;
            }
        }
    }

    cout << "-1 -1 -1" << endl;

    return 0;
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1000000000
using namespace std;
typedef long long ll;
int R,G,B,N;
ll ans=0;
int red,blue,green;
int main(int argc,char const* argv[])
{
  cin >> R >> G >> B >> N;
  for(int i=0;i<=N;i++)
    {
      red=i;
      for(int j=0;j<=N;j++)
	{
	  green=j;
	  blue=N-(R*red)-(G*green);
	  if(blue>=0 && blue%B==0)
	    {
	      //cout << red << ' ' << green << ' ' << blue/B << endl;
	      ans++;
	    }
	}
    }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
n = int(input())
anaglram_map = dict()


for i in range(n):
    line = input()
    line = sorted(line)
    line = str(line)
    if line not in anaglram_map:
        anaglram_map[line] = 1
    else:
        anaglram_map[line] += 1

couter = 0
for value in anaglram_map.values():
    if value >= 2:
        couter += value * (value - 1) // 2
print(couter)


Measured memory_bytes: 26236
-- AtCoder My Practice
-- author: Leonardone @ NEETSDKASU
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Set              as S
import qualified Data.Text             as T
import qualified Data.Array            as A
import qualified Text.Printf           as P
import qualified Data.Map              as M
import qualified Data.IntMap           as I
import qualified Data.ByteString.Char8 as C
import qualified Data.List             as L
import Data.Maybe (fromJust)
import Data.Ix    (inRange)

mapread = map $ fst . fromJust . C.readInt



main = print . solve . map read . words =<< getContents

solve (k:_:xs) = ans where
    x = head . reverse $ L.sort xs
    y = k - x
    ans = if y >= x - 1 then 0 else x - y - 1

    

Measured memory_bytes: 892
#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 ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n; cin >> n;
  vector<int> cnt(61000,0);
  FOR(x,1,101) FOR(y,1,101) FOR(z,1,101){
    int num = x*x+y*y+z*z+x*y+y*z+z*x;
    cnt[num]++;
  }
  REP(i,n) cout << cnt[i+1] << '\n';
  return 0;
}


Measured memory_bytes: 3600
#include <bits/stdc++.h>
#define int long long
#define lint long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
template <typename T> inline T LB(vector<T>& v, T a) { return *lower_bound(ALL(v),a); }
template <typename T> inline int LBP(vector<T>& v, T a) { return lower_bound(ALL(v),a) - v.begin(); }
template <typename T> inline T UB(vector<T>& v, T a) { return *upper_bound(ALL(v),a); }
template <typename T> inline int UBP(vector<T>& v, T a) { return upper_bound(ALL(v),a) - v.begin(); }
template <typename T1, typename T2> ostream& operator<< (ostream& os, const pair<T1,T2>& p) { os << p.first << " " << p.second; return os; }
template <typename T1, typename T2> istream& operator>> (istream& is, pair<T1,T2>& p) { is >> p.first >> p.second; return is; }
template <typename T> ostream& operator<< (ostream& os, const vector<T>& v) { REP(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
template <typename T> istream& operator>> (istream& is, vector<T>& v) { for(T& in : v) is >> in; return is; }
template <typename T = int> 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); }
const lint MOD = 1000000007;
const lint INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
// #define _DEBUG

int N;
int p[2][3];
int dp[25000010];

void _main() {
    cin >> N;
    REP(i, 2) REP(j, 3) cin >> p[i][j];
    for (int i = 0; i < 3; i++) {
        for (int j = p[0][i]; j <= N; j++) {
            dp[j] = max(dp[j], dp[j - p[0][i]] + p[1][i] - p[0][i]);
        }
    }
    N += dp[N];
    ZERO(dp);
    for (int i = 0; i < 3; i++) {
        for (int j = p[1][i]; j <= N; j++) {
            dp[j] = max(dp[j], dp[j - p[1][i]] + p[0][i] - p[1][i]);
        }
    }
    cout << N + dp[N] << endl;
}

signed main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    _main();
    return 0;
}

Measured memory_bytes: 195584
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());

            Console.WriteLine(Math.Pow(N,2));
        }
        
    }

}


Measured memory_bytes: 13140
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			int w = scanner.nextInt(), h = scanner.nextInt();
			if (w == 0 && h == 0)
				break;
			for (int i = 0; i < w; ++i) {
				for (int j = 0; j < h; ++j) {
					System.out.print((i + j) % 2 == 0 ? '#' : '.');
				}
				System.out.print('\n');
			}
			System.out.println();
		}
	}
}



Measured memory_bytes: 35364
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <math.h>
#include <cmath>
#include<cctype>
#include<string>
#include<set>
#include<iomanip>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include<bitset>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility> 
using namespace std;
using ll = long long;
template<typename T>using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1LL << 60;
#define all(x) (x).begin(),(x).end()
#define puts(x) cout << x << endl
#define rep(i,m,n) for(ll i = m;i < n;++i)
#define pb push_back
#define fore(i,a) for(auto &i:a)
#define rrep(i,m,n) for(ll i = m;i >= n;--i)


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

	int N;
	cin >> N;
	int cur = 0;
	vector<int>L(N), R(N);
	for (int i = 0; i < N; i++)cin >> L[i] >> R[i];
	sort(all(L));
	reverse(all(L));
	sort(all(R));

	ll ans1 = 0;
	ll ans2 = 0;
	//leftから
	if (L[0] >= 0) {
		int cur1 = 0;
		for (int i = 0; i < N; i++) {
			if (L[i] > cur1) {
				ans1 += L[i] - cur1;
				cur1 = L[i];
			}
			else break;
			if (L[i] - R[i] > 0) {
				ans1 += L[i] - R[i];
				cur1 = R[i];
			}
			else break;
		}
		ans1 += abs(cur1);
	}

	//rightから
	if (R[0] <= 0) {
		int cur2 = 0;
		for (int i = 0; i < N; i++) {
			if (cur2 > R[i]) {
				ans2 += cur2 - R[i];
				cur2 = R[i];
			}
			else break;
			if (L[i] - R[i] > 0) {
				ans2 += L[i] - R[i];
				cur2 = L[i];
			}
			else break;
		}
		ans2 += abs(cur2);
	}

	puts(max(ans1, ans2));


	return 0;
}

Measured memory_bytes: 1024
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder2 {
	class Program {
		static void Main(string[] args) {

			//出力の都度更新をやめる
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);


			Ans();

			//更新する
			Console.Out.Flush();
			
		}
	public static void Ans() {

			var inputArray = retreiveLongArray();
			var inputCount = 3;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			long N = inputArray[0];
			long A = inputArray[1];
			long B = inputArray[2];

			var map= retreiveLongArray();
			var hirou = new long[N];

			hirou[0] = 0;

			for (int i = 1; i < N; i++) {
				var toho = map[i] - map[i - 1];
				var rouryoku = Math.Min(toho * A, B);
				hirou[i] = rouryoku;

			}
			//答えの出力
			var ans = hirou.Sum();
			Console.WriteLine(ans);

		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}


		/// <summary>
		/// 2組みずつの入力データ(数値)からTuppleListを作成する
		/// </summary>
		/// <param name="N"></param>
		/// <returns></returns>
		private static List<Tuple<long, long>> createLongTuppleList(long N) {
			var inpList = new List<Tuple<long, long>>();

			for (int i = 0; i < N; i++) {
				var inp = retreiveLongArray();
				var item1 = inp[0];
				var item2 = inp[1];
				inpList.Add(new Tuple<long, long>(item1, item2));
			}
			return inpList;
		}
	}
}


Measured memory_bytes: 38492
N, M = map(int, input().split())
H = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(M)]

result = [1] * N

for i in range(M):
    A = AB[i][0] - 1
    B = AB[i][1] - 1

    if H[A] > H[B]:
        result[B] = 0
    elif H[A] < H[B]:
        result[A] = 0
    elif H[A] == H[B]:
        result[A] = 0
        result[B] = 0

print(sum(result))

Measured memory_bytes: 31940
#include <stdio.h>

int main(){
  int N,count = 0,i,j;
  int P[N];
  scanf("%d",&N);
  for(i=0;i<N;i++){
    scanf("%d",&P[i]);
  }
  for(j=0;j<N;j++){
    if(P[j]!=j+1){
      count = count +1;
    }
  }
  if(count > 2){
	printf("NO");
  }else{
    printf("YES");
  }
}
    

Measured memory_bytes: 128
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define REP2(i,n,s) for(int i=(int)s;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
    int n,a,b;
    cin>>n>>a>>b;

    if((b-a-1)%2 == 1) cout << "Alice" << endl;
    else cout << "Borys" << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
#include<math.h>
#include<bitset>
#include<queue>
#include<set>
#include<iomanip>
#include<assert.h>
#include<string>
#include<cstdlib>
#include<cctype>
#include<unordered_map>
#include<sstream>
#include<numeric>
#include<stack>
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 9e18;
constexpr int inf = 1e9;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
};

// intやllをstringに変換
template<typename T> inline string toString(const T &a){
  ostringstream oss;
  oss << a;
  return oss.str();
};
// ----------------------------------------------------------------------------

int main(){
  int N,Q;
  cin >> N >> Q;
  vector<vector<int>> G(N);
  for(int i=0; i<N-1; i++){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> cost(N,0);
  for(int i=0; i<Q; i++){
    int p,x;
    cin >> p >> x;
    p--;
    cost[p] += x;
  }
  vector<int> dist(N,-1);
  queue<int> que;
  que.push(0);
  dist[0] = cost[0];
  while(que.size()){
    int v = que.front();
    que.pop();
    for(auto nv: G[v]){
      if(dist[nv] != -1) continue;
      dist[nv] = dist[v] + cost[nv];
      que.push(nv);
    }
  }
  for(int i=0; i<N; i++){
    if(i){
      cout << " ";
    }
    cout << dist[i];
  }
  cout << endl;
  return 0;
}


Measured memory_bytes: 16372
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vd=vector<double>;
using vvd=vector<vd>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using vs=vector<string>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define LINF    	((ll)1ll<<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.141592653589793

template<class S,class T>ostream& operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	
	int n;
	cin>>n;
	cout<<"Christmas";
	rep(i,25-n)cout<<" Eve";
	cout<<endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std ;
class Cube {
  public:
  int f[6] ;
  Cube(){}
  void roll_z(){ roll(1,2,4,3); }
  void roll_y(){ roll(0,2,5,3); }
  void roll_x(){ roll(0,1,5,4); }
  void roll(int i , int j , int k , int l ){
    int t = f[i] ; f[i] = f[j] ;f[j] = f[k] ;f[k]= f[l];f[l]= t;
  }
  void move(char ch){
    if(ch == 'E') for (int i = 0 ; i < 3 ; i++)roll_y() ;
    if(ch == 'W') roll_y() ;
    if(ch == 'N') roll_x() ;
    if(ch == 'S') for (int i = 0 ; i < 3 ; i++)roll_x() ;
  }
} ;


int main () {
  Cube c ;
  string com ;
  for(int i = 0 ; i < 6 ; i++ ) cin >> c.f[i] ;
  cin >> com ;
  for(int i = 0 ; i < com.size() ; i++ ) c.move(com[i]) ;
  cout << c.f[0] << endl ;
}


Measured memory_bytes: 3204
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	REP(i, v.size()) { if (i)os << " "; os << v[i]; }return os;
}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
	REP(i, v.size()) { if (i)os << endl; os << v[i]; }return os;
}

constexpr long long INF64 = 1e18;

using Weight = long long;

using Array = vector<Weight>;
using Matrix = vector<Array>;

struct flow {
	int n;
	Matrix g;
	vector<int> itr, level;

	flow(int n) :n(n) { g.assign(n, Array(n, 0)); }

	void add_edge(int from, int to, Weight cap) {
		g[from][to] += cap;
	}

	void bfs(int s) {
		level.assign(n, -1);
		queue<int>q;
		level[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int u = q.front(); q.pop();
			for (int v = 0; v<n; ++v) {
				if (g[u][v]>0 && level[v]<0) {
					level[v] = level[u] + 1;
					q.push(v);
				}
			}
		}
	}

	Weight dfs(int u, int t, Weight f) {
		if (u == t)return f;
		for (int &i = itr[u]; i<n; ++i) {
			if (g[u][i]>0 && level[u]<level[i]) {
				Weight d = dfs(i, t, min(f, g[u][i]));
				if (d>0) {
					g[u][i] -= d;
					g[i][u] += d;
					return d;
				}
			}
		}
		return 0;
	}

	Weight dinic(int s, int t) {
		Weight ret = 0, f;
		while (bfs(s), level[t] >= 0) {
			itr.assign(n, 0);
			while ((f = dfs(s, t, INF64))>0)ret += f;
		}
		return ret;
	}
};

int main() {
	int n, m;
	cin >> n >> m;
	flow g(n);
	for (int i = 0; i<m; ++i) {
		int u, v;
		Weight c;
		cin >> u >> v >> c;
		g.add_edge(u, v, c);
	}
	cout << g.dinic(0, n - 1) << endl;
}
//be checked by AOJ(GRL-6-A)

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

using namespace std;

int main ()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a;
        cin >> a;
        n -= a;
    }
    if (n < 0)
    {
        cout << -1;
    }
    else
    {
        cout << n;
    }
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int A, B,K;
    cin >> A >> B>>K;
    if (A+K-1 >= B-K+1){
        for (int i = A; i <= B;i++){
            cout << i<<endl;
        }
    }else{
        for (int i = A; i < A + K;i++){
            cout << i << endl;
        }
        for (int i = B - K + 1; i <= B;i++) { 
            cout << i << endl;
        }
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

struct UnionFind {
	vector<int> par, rank;
	vector<int> cnt;
	
	UnionFind(int n) {
		par.resize(n);
		rank.resize(n);
		cnt.resize(n);
		REP(i, n) {
		 par[i] = i;
		 rank[i] = 0;
		 cnt[i] = 1;
		}
	}
	
	int operator[] (int x) {
		return find(x);
	}
	
	int find(int x) {
		if (x == par[x]) return x;
		return par[x] = find(par[x]);
	}
	
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		
		if (rank[x] < rank[y]) swap(x, y);
		
		par[y] = x;
		cnt[x] += cnt[y];
		if (rank[x] == rank[y]) rank[x]++;
	}
	
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	
	int size(int x) {
		return cnt[find(x)];
	}
	
};

int N, M, K;
vector<int> g[112345], g2[112345];

int main() {
	cin >> N >> M >> K;
	UnionFind uf(N);
	REP(i, M) {
		int A, B;
		scanf("%d %d", &A, &B);
		A--; B--;
		g[A].push_back(B);
		g[B].push_back(A);
		
		uf.unite(A, B);
	}
	REP(i, K) {
		int A, B;
		scanf("%d %d", &A, &B);
		A--; B--;
		g2[A].push_back(B);
		g2[B].push_back(A);
	}
	
	REP(i, N) {
		int ans = uf.size(i) - 1;
		ans -= g[i].size();
		for (int u : g2[i]) if (uf[u] == uf[i]) ans--;
		printf("%d\n", ans);
	}
	return 0;
}


Measured memory_bytes: 13560
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
signed main(){
  int N,M;
  cin>>N>>M;
  if(N>=M) 
    cout<<0<<endl;
  else{
  vi A(M);
  rep(i,0,M) cin>>A[i];
  sort(ALL(A));
    if(N==1)
      cout<<A[M-1]-A[0]<<endl;
    else{
  vector<pii> B(M-1);
  rep(i,0,M-1) B[i]=make_pair(A[i+1]-A[i],i);
  sort(ALL(B));
  vi C(N-1);
  rep(i,0,N-1) C[i]=B[M-2-i].second;
  sort(ALL(C));
  int ans=A[C[0]]-A[0];
  rep(i,1,N-1) ans+=A[C[i]]-A[C[i-1]+1];
  ans+=A[M-1]-A[C[N-2]+1];
  cout<<ans<<endl;
      }
    }
  }

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

using namespace std;

template<typename T> void drop(const T &x){cout<<x<<'\n';exit(0);}

void milkshake() {
  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 10; j++)
      for(int k = 0; k < 10; k++) {
        string t;
        t.push_back('0' + i);
        t.push_back('0' + j);
        t.push_back('0' + k);

        int l = 0, m = 0;
        while(l + m < n) {
          if(t[m] == s[l + m]) m++;
          else l++;
          if(m < 3) continue;
          ans++;
          break;
        }
      }
  cout << ans << '\n';
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt=1;
  //cin >> tt;
  while(tt--) milkshake();

  return 0;
}



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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ull unsigned long long
#define ld long double

template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
template <class T>
using VVV = V<V<V<T>>>;

#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) rep2(i, 0, n)
#define rep1(i, n) rep1(i, 1, n + 1)

// #define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
// #define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()  //

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int dp[10 * 40 + 1][10 * 40 + 1];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(15);
    //    cout << std::setfill('0') << std::setw(2);

    int N;
    std::cin >> N;
    int M_a;
    std::cin >> M_a;
    int M_b;
    std::cin >> M_b;
    std::vector<int> a(N);
    std::vector<int> b(N);
    std::vector<int> c(N);
    for (int i = 0; i < N; i++) {
        std::cin >> a[i];
        std::cin >> b[i];
        std::cin >> c[i];
    }
    rep(j, 401) rep(k, 401) { dp[j][k] = INF; }
    dp[0][0] = 0;

    rep(i, N) {
        for (int j = 400; j >= a[i]; --j)
            for (int k = 400; k >= b[i]; --k) {
                dp[j][k] = min(dp[j][k], dp[j - a[i]][k - b[i]] + c[i]);
            }
    }

    int ans = INF;
    rep2(i, 1, 401) rep2(j, 1, 401) {
        if (i * M_b == j * M_a) ans = min(ans, dp[i][j]);
    }
    if (ans == INF) ans = -1;
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 896
N, M = gets.split.map(&:to_i)
D = N.times.map { gets.split.map(&:to_i) }.sort_by(&:first)

total_item = 0
total_pay = 0

D.find do |price, max_item|
  
  if total_item + max_item < M
    total_pay += max_item * price
    total_item += max_item
    false
  else
    item = M - total_item
    total_pay += item * price
    total_item += item
    true
  end
end

puts total_pay

Measured memory_bytes: 12664
n,k = map(int,input().split())
H = list(map(int,input().split()))
inf = float('INF')
dp = [inf]*n
dp[0] = 0
for i in range(1,n):
    for j in range(1,min(i,k)+1):
        # print(j)
        dp[i]=min(dp[i],dp[i-j]+abs(H[i]-H[i-j]))
    # print(dp)

    # dp[i]=min(dp[i],dp[i-1]+abs(H[i]-H[i-1]))
# print(dp)
print(dp[-1])

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

using namespace std;

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

	int n; cin >> n;
	int w[n], sum = 0;
	for (int i = 0; i < n; ++i) {
		cin >> w[i];
		sum += w[i];
	}
	int pre = 0, ans = sum;
	for (int i = 0; i < n - 1; ++i) {
		pre += w[i];
		ans = min(ans, abs(pre - (sum - pre)));
	}
	cout << ans << endl;
}

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

int H, W;
char C[110][110];

int main() {
    cin >> H >> W;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            cin >> C[i][j];
        }
    }

    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            cout << C[i][j];
        }
        cout << endl;
        for (int j = 0; j < W; ++j) {
            cout << C[i][j];
        }
        cout << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#define WHITE 0
#define GRAY 1
#define BLACK 2

void dfs(void);
void visit(int);

int n;
int time;
int d[100];
int f[100];
int color[100];
int a[100][100];

int main(){
  int k,v,u;
  int i,j;
  
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      a[i][j]=0;
  
  scanf("%d",&n);
  
  for(i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    for(j=0;j<k;j++){
      scanf("%d",&v);
      a[u-1][v-1]=1;
    }
  }

  dfs();

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

void dfs(void){

  int i,j;
  
  for(i=0;i<n;i++){
    color[i]=WHITE;
  }
  time=0;
  for(i=0;i<n;i++){
    if(color[i]==WHITE) visit(i);
  }
}

void visit(int r){

  int i,j;

  color[r]=GRAY;
  d[r]=++time;
  for(i=0;i<n;i++){
    if(a[r][i]==0) continue;
    if(color[i]==WHITE) visit(i);
  }
  color[r]=BLACK;
  f[r]=++time;
  
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-3;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index)
{
   vector.erase(vector.begin() + index);
}

using Graph = vector<vector<tuple<ll, ll, ll>>>;

// MOD確認

int main(){
   ll X,K,D;
   long double x,k,d;
   cin >> X >> K >> D;
   X = abs(X);
   x = X;k = K;d = D;
   if(abs(X) > k * d){
      cout << abs(X - K * D) << endl;
      return 0;
   }
   ll Y = (X+D-1)/D;
   ll XX = abs(X - Y * D);
   ll YY = K - Y;
   if(YY % 2 == 0){
      cout << XX << endl;
   }else{
      cout << abs(D-XX) << endl;
   }
}


Measured memory_bytes: 3632
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<cstring>
#include<iomanip> //cout << fixed << setprecision(15) << x << endl;

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1LL<<60;
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
/* -- template -- */

const int MAX_GRID = 2001;
int wide[MAX_GRID][MAX_GRID], height[MAX_GRID][MAX_GRID];

int main() {
    int H, W; cin >> H >> W;
    string S[H]; rep(i, H)  cin >> S[i];
    rep(i, H) {
        int cnt = 1;
        rep(j, W) {
            if(S[i][j] == '#') wide[i][j] = 0, cnt = 1;
            else wide[i][j] = cnt, ++cnt;
        }
        for(int j = W - 1; j > 0; --j) {
            if(wide[i][j - 1] == 0 || wide[i][j] == 0)continue;
            wide[i][j - 1] = wide[i][j];
        }
    }
    rep(j, W) {
        int cnt = 1;
        rep(i, H) {
            if(S[i][j] == '#') height[i][j] = 0, cnt = 1;
            else height[i][j] = cnt, ++cnt;
        }
        for(int i = H - 1; i > 0; --i) {
            if(height[i - 1][j] == 0 || height[i][j] == 0)continue;
            height[i - 1][j] = height[i][j];
        }
    }
    int ans = 0;
    rep(i, H) {
        rep(j, W) {
            ans = max(ans, height[i][j] + wide[i][j] - 1);
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 35584
#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i, N) cin >> A[i];

    ll ans = 0;
    map<int, int> mp;
    rep(i, N) {
        ans += mp[i - A[i]];
        mp[i + A[i]]++;
    }
    write(ans);
}

Measured memory_bytes: 20932
import math
from itertools import accumulate
a,b=map(int,input().split())
c=list(map(int,input().split()))

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort(reverse = True)
    return divisors
d=make_divisors(sum(c))


m=1
for i in d:
  e=[]
  for j in range(a):
    e.append(c[j]%i)
  e.sort()
  acc_m = list(accumulate(e))
  e=[i-e[j] for j in range(a)]
  acc_p = list(accumulate(e))
  for j in range(a):
    k=acc_m[j]
    l=acc_p[-1]-acc_p[j]
    if k==l and k+l<=b*2:
      print(i)
      exit()


Measured memory_bytes: 3188
function Main (input) {
  input = input.split("\n").map(Number);
  var N = input[0];
  var K = input[1];
  var ans = 1;

  for (var i = 0; i < N; i++) {
    if (ans*2 > ans + K) {
      ans += K;
    }
    else if (ans*2 < ans + K) {
      ans *= 2;
    }
    else if (ans*2 == ans + K) {
      ans *= 2;
    }
  }
  console.log(ans);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 7372
import java.math.BigInteger;
import java.util.*;

class Main {
	Scanner sc = new Scanner(System.in);

	public void run() {
		int N=sc.nextInt();
		for(int i=0;i<N;i++){
			BigInteger a=sc.nextBigInteger(),b=sc.nextBigInteger(),sum=a.add(b);
			if(sum.toString().length()<=80){
				ln(sum);	
			}else{
				ln("overflow");
			}
		}
	}

	public static String join(double[] array, String separator) {
		StringBuilder str = new StringBuilder(array[0] + "");
		for (int i = 1; i < array.length; i++) {
			str.append(separator).append(array[i] + "");
		}
		return str.toString();
	}

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

	public static void pr(Object o) {
		System.out.print(o);
	}

	public static void ln(Object o) {
		System.out.println(o);
	}

	public static void ln() {
		System.out.println();
	}
}

Measured memory_bytes: 12000