akhauriyash/Code-Regression huggingface dataset:
n,a,b,c = map(int,input().split())
l = []
for i in range(n):
    l.append(int(input()))
ans = 10000
for i in range(4**n):
    abc = [(i%(4**(j+1)))//(4**j) for j in range(n)]
    if abc.count(1) >= 1 and abc.count(2) >= 1 and abc.count(3) >= 1:    
        d_a = a
        d_b = b
        d_c = c
        
        for j in range(n):
            if abc[j] == 1:
                d_a -= l[j]
            elif abc[j] == 2:
                d_b -= l[j]
            elif abc[j] == 3:
                d_c -= l[j]         
        ans = min(ans,abs(d_a)+abs(d_b)+abs(d_c)+10*(abc.count(1)+abc.count(2)+abc.count(3)-3))
        
print(ans)

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int h,n;
    cin >> h >> n;
    int a[n];
    rep(i,0,n) {
        cin >> a[i];
        h -= a[i];
    }
    
    if(h<=0) cout << "Yes" << "\n";
    else cout << "No" << "\n"; 



}

Measured memory_bytes: 640
/*input
6 5
8 6 9 1 2 1
1 3
4 2
4 3
4 6
4 6
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds; 

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}


void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}



int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL); 
    ll t=1,i,j;
    ll n,m;
    cin>>n>>m;
    ll a[n];
    ll b[n];
    init(b,n,1);
  	ll ans=0;
    rep(i,0,n)cin>>a[i];
    rep(i,0,m){
    	ll x,y;
    	cin>>x>>y,--x,--y;
    	if(a[x]>=a[y])b[y]=0;
    	if(a[y]>=a[x])b[x]=0;
    }
    rep(i,0,n)ans+=b[i];
    cout<<ans<<endl;
    return 0;
}




Measured memory_bytes: 5192
# coding: utf-8

import math

N, K = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
A.sort()

p = 10**9 + 7
facts = [1, 1]
for i in range(2, N+1):
	facts.append((facts[i-1] * i) % p)

invs = [1 for i in range(N+1)]
invs[N] = pow(facts[N], p-2, p)
for i in range(N, 1, -1):
	invs[i-1] = invs[i] * i % p

sum_max_s = 0
for i in range(K, N+1):
	sum_max_s += A[i-1] * (facts[i-1] * invs[K-1] * invs[i-K] % p) % p

sum_min_s = 0
for i in range(1, N-K+2):
	sum_min_s += A[i-1] * (facts[N-i] * invs[K-1] * invs[N-i-K+1] % p) % p

print(str(int(sum_max_s - sum_min_s) % p))

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


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

	vector<int> v(3 * N);
	for(int i=0;i<3 * N;i++) {
		cin >> v[i];
	}
	sort(v.begin(), v.end());

	ll ans = 0;
	for(int i=N;i<3 * N;i+=2) {
		ans += v[i];
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 1536
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;cin>>s;
  long long k;cin>>k;
  for(int i=0;i<s.size();i++){
    long long a=s.at(i)-'a';
    if(26-a<=k&&a!=0){
      s.at(i)='a';
      k-=26-a;
    }
  }
  if(k>0){
    k%=26;
    long long b=s.at(s.size()-1)-'a';
    if(k>=26-b){
      s.at(s.size()-1)='a'+k-(26-b);
    }
    else s.at(s.size()-1)+=k;
  }
  cout<<s<<endl;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
using namespace std;
typedef long long ll;

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

  int G[N];
  rep(i, N-1) G[i] = abs(A[i+1] - A[i]);
  G[N-1] = abs(K - A[N-1] + A[0]);
  sort(G, G + N);
  cout << K - G[N-1] << endl;
}


Measured memory_bytes: 5172
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int Mn(int a,int b){return a<b?a:b;}
const int N=2e5+5,mod=1e9+7;
int upt(int x){while(x>=mod)x-=mod;while(x<0)x+=mod;return x;}

int n,m,lm,dp[N],sm[N],ans,g[N];
char s[N];
void solve()
{
  g[0]=sm[0]=1;
  for(int i=1;i<=n;i++)
    {
      if(i>=2)g[i]=sm[i-2];
      sm[i]=upt(sm[i-1]+g[i]);
    }
  int ans=1;//ans=1 for all one color
  for(int i=2;i<=n;i++)
    ans=(ans+(ll)i*g[n-i])%mod;
  printf("%d\n",ans);
}
int main()
{
  scanf("%d%d",&n,&m);
  scanf("%s",s+1); bool fg=0;
  for(int i=1;i<=m;i++)
    if(s[i]!=s[1]){fg=1;break;}
  if(!fg){solve();return 0;}
  if(n&1){puts("0");return 0;}
  lm=n+1; int lst;
  for(int i=m;i;i--)if(s[i]==s[1]){lst=i;break;}
  for(int i=1;i<=m;i++)
    if(s[i]==s[1])//== not !=
      {
	int j=i;
	while(j+1<=m&&s[j+1]==s[j])j++;
	swap(i,j); j=i-j+1;
	if(i-j==0) lm=Mn(lm,j+((j&1)==0));
	else if((j&1)&&i!=m)lm=Mn(lm,j);//i!=m
	//else if((j&1)&&i!=lst)lm=Mn(lm,j);
      }
  n>>=1; lm=(lm+1)>>1; dp[0]=sm[0]=1;
  for(int i=1;i<=n;i++)
    {
      dp[i]=sm[i-1]; if(i>lm)dp[i]=upt(dp[i]-sm[i-lm-1]);
      sm[i]=upt(sm[i-1]+dp[i]);
    }
  int ans=0;
  for(int i=1;i<=lm;i++)//lm not n
    ans=(ans+(ll)i*2*dp[n-i])%mod;
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 1920
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long

ll b,x,n,MIN,i,ii,a[100000000];
bool MARK[10000000];

ll ASearch(ll k){
    ll lef=1, rig=n, i=(lef+rig)/2;
    while(lef!=i && rig!=i){
        if(a[i]>=k) rig=i;
        else lef=i;
        i=(lef+rig)/2;
    }
    for(i=lef; i<=n; i++){
        if(a[i]>=k) return i;
    }
}

int main(){
    cin>>x>>n;
    MIN=x;
    if(n==0) { cout<<x; return 0; }
    for(i=1; i<=n; i++){
        cin>>a[i];
        MARK[a[i]]=true;
    }
    sort(a+1,a+n+1);
    b=ASearch(x);
    i=a[b];
    ii=a[b];
    while(true){
        i--;
        ii++;
        if(MARK[i]==false) { cout<<i; return 0; }
        if(MARK[ii]==false) { cout<<ii; return 0; }
    }
}

Measured memory_bytes: 3536
X,N = map(int,input().split())
if N ==0:
    print(X)
else:
    p = list(map(int,input().split()))
    x_min = 101
    num_list = list(range(102))
    for i in range(N):
        num_list.remove(p[i])
    for j in reversed(num_list):
        x = abs(j-X)
        if x_min >= x:
            x_min = x
            close=j
    print(close)


Measured memory_bytes: 9188
#include<bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define mod 1000000007
#define FASTIO ios::sync_with_stdio(0); cin.tie(0);
#define REP(i,a,b) for(int i=a;i<b;i++)
#define maxx 1000001
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

bool palindrome(int n){
   string s=to_string(n),ss,t;
   int sz=s.size();
   for(int i=0;i<sz/2;i++){
     if(s[i]!=s[sz-i-1]) return 0;
   }
   return 1;
}

int main() {
    FASTIO
    int a,b,cnt=0;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(palindrome(i)) cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}


Measured memory_bytes: 3680
def B_Buffet():
    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(N):
        ans += B[A[i] - 1]
        if (0 < i) and (A[i] == A[i-1] +1):
            ans += C[A[i-1]-1]
    print(ans)
    
if __name__ == "__main__":
    B_Buffet()

Measured memory_bytes: 3064
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProconTemplate
{
    class Doing
    {
        public static void Main(string[] args)
        {
            // Drop
            Console.ReadLine();

            var inputArray = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).OrderBy(x => x).ToArray();

            var ret = (inputArray[0] + inputArray[1]) / 2.0;

            if(inputArray.Count() == 2)
            {
                Console.WriteLine(ret);
                return;
            }

            for(int index=2;index < inputArray.Count(); index++)
            {
                ret = (ret + inputArray[index]) / 2.0;
            }

            Console.WriteLine(ret);
        }
    }
}


Measured memory_bytes: 13396
#include <iostream>
using namespace std;
int card[255][14]={{0}};
 
int main(void){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        char c;
        int num;
        cin>>c>>num;
        card[(int)c][num]=1;
    }
     
    for(int i=1;i<=13;i++){
        if(card[(int)'S'][i]==0){
            cout<<"S "<<i<<endl;
        }
    }
    for(int i=1;i<=13;i++){
        if(card[(int)'H'][i]==0){
            cout<<"H "<<i<<endl;
        }
    }
    for(int i=1;i<=13;i++){
        if(card[(int)'C'][i]==0){
            cout<<"C "<<i<<endl;
        }
    }
    for(int i=1;i<=13;i++){
        if(card[(int)'D'][i]==0){
            cout<<"D "<<i<<endl;
        }
    }
}


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

typedef long long ll;
typedef vector<long long> VLL;
typedef vector<pair<int,int>> VP;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define MOD (1e9+7)
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define REP(i,n) for(int i=1;i<=(int)(n);i++)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define ALL(a)  a.begin(),a.end()
#define RALL(a) a.begin(),a.end(),greater<int>()
#define ROT(a) a.begin(),a.begin()+1,a.end()
#define RROT(a) a.begin(),a.end()-1,a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)

map< int64_t, int > prime_factor(int64_t n) {
  map< int64_t, int > ret;
  for(int64_t i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}


ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
/*--------------------------------------------*/

int main(){
  ll a, b;
  cin >> a >> b;
   
  map< int64_t, int > ret = prime_factor(gcd(a,b));
  
  cout << ret.size() + 1<< endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll Max = 100006;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n, m, a[Max];
	set <ll> bad[Max];
	cin >> n >> m;
	for (int i=1; i<=n; i++) {
		cin >> a[i];
	}
	ll l, r;
	for (int i=1; i<=m; i++) {
		cin >> l >> r;
		if (a[r] >= a[l]) bad[l].insert(1);
		if (a[l] >= a[r]) bad[r].insert(1);
	}
	ll ans = 0;
	for (int i=1; i<=n; i++) if (!bad[i].size()) {
		ans++;
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 11768
#include <bits/stdc++.h>
using namespace std;
 
int n, k, a[105], dp[100011];
set<int> st;
int main() {
	cin >> n >> k;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	for(int i = 1; i <= k; i++) {
		st = {};
		for(int j = 0; j < n; j++) {
			if(a[j] > i)
				continue;
			st.insert(dp[i - a[j]]);
		}
		int mex = 0;
		while(st.find(mex) != st.end())
			mex++;
		dp[i] = mex;
	}
	printf("%s", dp[k] != 0 ? "First" : "Second");
	return 0;
}

Measured memory_bytes: 640
import queue
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
x = [[] for _ in range(n+1)]
ans = [0]*(n+1)
for _ in range(m):
  ai,bi = map(int,input().split())
  x[ai].append(bi)
  x[bi].append(ai)
q = queue.Queue()
q.put(1)
while not q.empty():
  i = q.get()
  for nx in x[i]:
    if ans[nx] == 0:
      ans[nx] = i
      q.put(nx)
print("Yes")
for i in range(2,n+1):
  print(ans[i])

Measured memory_bytes: 94100
#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <time.h>

#include <string>

const int MAX_K = 100050;
int K;
long long A[MAX_K];

int main(int argc, char **argv) {
    std::cin >> K;

    for (int i = 1; i <= K; i++) {
        std::cin >> A[i];
    }

    if (A[K] != 2) {
        std::cout << -1 << std::endl;
        return 0;
    }

    long long min = 2;
    long long max = 3;

    for (int i = K; 1 <= i; i--) {
        long long lb = 0;
        long long ub = min / A[i] + 1;

        while (lb + 1 < ub) {
            long long mid = (lb + ub) / 2;
            if (A[i] * mid >= min) {
                ub = mid;
            } else {
                lb = mid;
            }
        }

        if (max < ub * A[i]) {
            std::cout << -1 << std::endl;
            return 0;
        }

        long long id_for_min = ub;

        lb = id_for_min;
        ub = max / A[i] + 1;

        while (lb + 1 < ub) {
            long long mid = (lb + ub) / 2;
            if (A[i] * mid <= max) {
                lb = mid;
            } else {
                ub = mid;
            }
        }

        long long id_for_max = lb;

        min = A[i] * id_for_min;
        max = A[i] * id_for_max + A[i] - 1;
    }

    std::cout << min << " " << max << std::endl;

    return 0;
}

Measured memory_bytes: 1024
import java.math.BigInteger
fun main(args: Array<String>) {
    tailrec fun fact(n: BigInteger, total: BigInteger = BigInteger.ONE): BigInteger {
        if (n <= BigInteger.ONE) {
            return total
        }
        return fact(n.subtract(BigInteger.ONE), total * n)
    }

    val values = readLine()?.split(' ') ?: return
    if (values.size != 2) {
        return
    }
    val n = values[0].toLong()
    val m = values[1].toLong()

    // n, mの組み合わせの数を足す
    // n! / r!(n-r)!
    val nCount = if (n - 2 >= 0) {
        fact(BigInteger.valueOf(n)) / (fact(BigInteger.valueOf(2)) * fact(BigInteger.valueOf(n - 2)))
    } else {
        BigInteger.ZERO
    }
    val mCount = if (m - 2 >= 0) {
        fact(BigInteger.valueOf(m)) / (fact(BigInteger.valueOf(2)) * fact(BigInteger.valueOf(m - 2)))
    } else {
        BigInteger.ZERO
    }
    println((nCount + mCount).toInt())
}


Measured memory_bytes: 38296
object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val k, n = sc.nextInt()
  val as = Vector.fill(n)(sc.nextInt())
  val min = ((as.last - k) +: as).zip(as).foldLeft(Int.MaxValue) {
    case (acc, (a, a2)) => (a + k - a2).min(acc)
  }
  println(min)
}


Measured memory_bytes: 78380
#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 BUGAVOID(x) x
#define rep(...) BUGAVOID(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define sz(c) (int)(c.size())
#define all(c) c.begin(),c.end()
#define ms(a,v) memset(a,v,sizeof(a))
#define mp make_pair
#define write(x) cout<<(x)<<endl
using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll;
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; }
constexpr ll pow2(const int n) { return 1ll << n; }
const int INF = 1 << 28; const ll LINF = 1ll << 60;
const int MAX = 1e5 + 5; const int MOD = 1e9 + 7; const double EPS = 1e-6;
const int dy[4] = { 0,1,0,-1 }; const int dx[4] = { 1,0,-1,0 };
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); }; }aaaa;
// 1. 問題よくよめえええええええええええええええええええええええええ
// 2. intオーバーフロー << * + あたり
// 3. 落ち着け

int N, M;
vector<int> start;
vector<vector<pair<int, int>>> graph; // <to, cost>
vector<int> dist;
vector<bool> visited;
int ans;

int main() {
    cin >> N >> M;
    start.assign(N, 0);
    graph.assign(N, vector<pair<int, int>>());
    rep(i, M) {
        int l, r, d;
        cin >> l >> r >> d;
        l--; r--;
        graph[l].emplace_back(r, d);

        if (d > 0) {
            start[r] = 2; // Rに来たものは始点x=0になれない
        }

        if (start[l] != 2) {
            start[l] = 1;
        }
    }

    ans = 1; // Yes
    dist.assign(N, -1);
    visited.assign(N, false);
    rep(i, N) {
        if (start[i] != 1) continue;
        if (dist[i] == -1) {
            dist[i] = 0;
        }
        vector<pair<int, int>> stack; // <v, cost>
        stack.emplace_back(i, 0);

        while (not stack.empty()) {
            const auto top = stack.back();
            stack.pop_back();
            dist[top.first] = top.second;

            for (const auto& e : graph[top.first]) {
                if (dist[e.first] == -1) {
                    stack.emplace_back(e.first, top.second + e.second);

                    if (start[e.first] == 2) {
                        start[e.first] = 0;
                    }
                }
                else if (top.second + e.second != dist[e.first]) {
                    ans = 0;
                    break;
                }
            }

            if (ans == 0) {
                break;
            }
        }

        if (ans == 0) {
            break;
        }
    }

    bool rExists = false;
    rep(i, N) {
        if (start[i] == 2) rExists = true;
    }
    if (rExists) ans = 0;

    write(ans ? "Yes" : "No");
}

Measured memory_bytes: 7668
<?php
fscanf(STDIN,"%d",$n);
$x = explode(" ",trim(fgets(STDIN)));
$sum = 0;
for ($i = 0; $i < $n; $i++) {
    $sum = $sum+$x[$i];
}
$p = $sum/$n;
$p=round($p,0);
$sum2 = 0;
for ($i = 0; $i < $n; $i++) {
    $sum2 = $sum2+($x[$i]-$p)**2;
}
echo $sum2."\n";
?>


Measured memory_bytes: 3828
#include <iostream>

using namespace std;

uint64_t N;

uint64_t scale(uint64_t n) {
  uint64_t ret = 1;

  while (n /= 10)
    ++ret;

  return ret;
}

int main() {

  cin >> N;

  uint64_t larger = N;
  for (size_t i = 1; i * i <= N; ++i) {
    if (N % i == 0)
      larger = N / i;
  }

  cout << scale(larger) << endl;

  return 0;
}


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

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1'000'000'000'000'007ll;
const llong mod = 1e9+7;

int main() {
	llong N;
	vector<llong> L;

	cin >> N;
	L.resize(N);
	for (int i = 0; i < N; ++i)
		cin >> L[i];
	sort(ALL(L));

	llong ans = 0;
	for (int i = 0; i < N; ++i) {
		for (int j = i+1; j < N; ++j) {
			llong a = L[i];
			llong b = L[j];

			llong c_low = upper_bound(ALL(L), b-a) - L.begin();
			llong c_high = lower_bound(ALL(L), a+b) - L.begin();

			ans += c_high - c_low;
			if (c_low <= i && i <= c_high) --ans;
			if (c_low <= j && j <= c_high) --ans;
		}
	}

	cout << ans/3 << endl;

	return 0;
}

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

#define REP(i, n) for (int i = 0; i < (n); ++i)
int main(void){
    int n, ans = 0;

    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    vector<int> a(n), b(n), c(n);
    REP(i, n) {
        cin >> a[i];
        a[i]--;
    }
    REP(i, n) {
        cin >> b[i];
    }
    REP(i, n - 1) {
        cin >> c[i];
    }
    REP(i, n) {
        ans += b[a[i]];
        if (0 < i && a[i] == a[i - 1] + 1) {
            ans += c[a[i - 1]];
        }
    }

    cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 256
r, c = list(map(int, input().split()))
t = [[0]*(c+1) for i in range(r+1)]
for i in range(r):
  a = list(map(int, input().split()))
  for j in range(c):
    t[i][j] = a[j]
    t[r][j] += a[j]
  t[i][c] = sum(a)
t[r][c] = sum(t[r])
for i in range(r+1):
  print(" ".join(map(str, t[i])))


Measured memory_bytes: 5700
{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}
 
import           Control.Applicative
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString.Char8       as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import           Data.List
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive.MutVar
import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           Unsafe.Coerce
 
main :: IO ()
main = do
    [a, b, q] <- map read.words <$> getLine :: IO [Int]
    sstsxs <- U.unfoldrN (a + b + q) (B.readInt.B.dropWhile isSpace) <$> B.getContents
    let (ss, tsxs) = U.splitAt a sstsxs
    let (ts, xs) = U.splitAt b tsxs
    putStr.unlines.map show $ solve ss ts xs
 
inf :: Int
inf = 0x3f3f3f3f3f3f3f3f
 
solve :: U.Vector Int -> U.Vector Int -> U.Vector Int -> [Int]
solve ss ts xs = map f $ U.toList xs
  where
    f x = minimum
        [ abs (sl - x) + abs (tl - sl) `min` abs (tr - sl)
        , abs (sr - x) + abs (tl - sr) `min` abs (tr - sr)
        , abs (tl - x) + abs (sl - tl) `min` abs (sr - tl)
        , abs (tr - x) + abs (sl - tr) `min` abs (sr - tr)
        ]
      where
        (sl, sr) = neighbor x ss
        (tl, tr) = neighbor x ts
 
neighbor :: Int -> U.Vector Int -> (Int, Int)
neighbor x vec
    | x < U.head vec = (inf, U.head vec)
    | i + 1 < U.length vec = (U.unsafeIndex vec i, U.unsafeIndex vec (i + 1))
    | otherwise = (U.last vec, inf)
  where
    i = upperBound 0 (U.length vec - 1) (\i -> U.unsafeIndex vec i <= x)
 
-------------------------------------------------------------------------------
 
lowerBound :: (Integral i) => i -> i -> (i -> Bool) -> i
lowerBound low high p = go low high
  where
    go !low !high
        | high <= low = high
        | p mid       = go low       mid
        | otherwise   = go (mid + 1) high
      where
        h = toInteger high
        l = toInteger low
        mid = fromIntegral $ l + div (h - l) 2
{-# INLINE lowerBound #-}
 
upperBound :: (Integral i) => i -> i -> (i -> Bool) -> i
upperBound low high p
    | p high = high
    | otherwise = lowerBound low high (not.p) - 1
{-# INLINE upperBound #-}

Measured memory_bytes: 11988
def main():

    K, A, B = map(int, input().split())
    if B <= A:
        return K+1
    else:
        if A-1 >= K:
            return K+1
        else:
            L = K - (A-1) # A-1 times
            v = A
            v += (B-A) * (L // 2)
            if L % 2 == 1:
                v += 1
            return max(v, K+1)


if __name__ == '__main__':
    print(main())

Measured memory_bytes: 3060
n, k = map(int, input().split())
a = [int(x)-1 for x in input().split()]
lis = [None] * n
now = 0
lis[0] = 0
num = 0
rode = [0]
while True:
    now = a[now]
    num += 1
    rode.append(now)
    if num == k:
        print(now + 1)
        break
    if lis[now] == None:
        lis[now] = num
    else:
        print(rode[lis[now] + (k-lis[now]) % (num - lis[now])] + 1)
        break

Measured memory_bytes: 94884
#include <stdio.h>

int main()
{
	int n, c;

	scanf("%d", &n);

	for (int i = 1; i <= n; i++) {
		if (i % 3 == 0) printf(" %d", i);
		else {
			c = i;

			while (c % 10 != 3 && c) {
				c /= 10;
				}
			
			if (c) printf(" %d", i);
		}
	}

	printf("\n");

	return 0;
}



Measured memory_bytes: 2036
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<map>
#include<vector>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
long long READ(){
    long long xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
char one(){
	char ch=getchar();
	while(ch==' '||ch=='\n')
		ch=getchar();
	return ch;
}
int main(){
	//freopen("in","r",stdin);
	int A=read(),B=read(),ans=A*B;
	ans=max(ans,A+B);
	ans=max(ans,A-B);
	printf("%d\n",ans);
	return 0;
}



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


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

	if ((a*100+b*10+c)%4==0)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

Measured memory_bytes: 256
//自称高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INF 1999999999
#define REP(i,n) for(int i=0;i<n;i++)
#define MODA 1000000007 

int main() {
    int ans = 0;
	int n, p[20];
	cin >>n;
	REP(i, n) cin >> p[i];
	for(int i = 1; i < n-1; i++){
		if((p[i] - p[i-1]) * (p[i] - p[i+1])<0) ans++;
	}
    cout << ans << endl;
}

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

 int main (){
     int a,b;
     cin >> a >> b;
     cout << a/b << " " << a%b << " " << fixed << setprecision(5) << double(a)/double(b) << endl;
 }


Measured memory_bytes: 3256
import bisect
n=int(input())
x=list(map(int,input().split()))
y=[]
for i in range(n):
    y.append(x[i])

x.sort()

for i in range(n):
    if bisect.bisect_left(x,y[i])+1 <= len(x)/2:
        print(x[int(len(x)/2)])
    else:
        print(x[int(len(x)/2)-1])

Measured memory_bytes: 101716
N, K = map(int, input().split())

mod = 10 ** 9 + 7

memo = [0] * (K + 1)
ans = 0
for i in range(K, 0, -1):
    memo[i] = pow(K // i, N, mod)
    j = 2
    while i * j <= K:
        memo[i] -= memo[i * j]
        j += 1
    ans += memo[i] * i
    ans %= mod

print(ans)


Measured memory_bytes: 69196
# -*- coding: utf-8 -*-
import sys
from decimal import Decimal, ROUND_HALF_UP


def main():
    N,K = list(map(int, sys.stdin.readline().split()))
    A_list = list(map(int, sys.stdin.readline().split()))


    def check(length :int) -> bool:
        cnt = 0

        for a in A_list:
            if a >= length:
                quotient = a / length
                divide = -(-quotient // 1)  # round up
                cnt += (divide - 1)
        
        return (True if cnt <= K else False)


    L = 0.1            # the minimum length
    R = max(A_list)  # the maximum length

    while (R - L) > 0.01:
        M = L + (R - L) / 2

        if check(M):
            R = M
        else:
            L = M
    
    
    ans = float( Decimal(str(M)).quantize(Decimal('1E-1'), rounding=ROUND_HALF_UP) )
    ans = -(-ans // 1)  # round up
    print( int(ans) )


if __name__ == "__main__":
    main()


Measured memory_bytes: 32372
n = int(input())
ans = 0
s = [list(map(str,input().split())) for i in range(n)]
key_s = str(input())
key = 0
for t,tt in s:
  if key == 1:
    ans += int(tt)
  if t == key_s:
    key = 1
print(ans)
    


Measured memory_bytes: 2940
import java.util.Scanner;

/**
 * Main
 */
public class Main {

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

        int X = sc.nextInt();
        int N = sc.nextInt();
        int[] p = new int[N];
        int[] q = new int[102];
        for(int i=0; i<N; i++){
            p[i] = sc.nextInt();
            q[p[i]] = 1; 
        }
        int minDif = 1000;
        int ans = X;
        for(int i = 0; i<=101; i++){
            if(q[i] == 0){
                if(Math.abs(X-i) < minDif){
                    ans = i;
                    minDif = Math.abs(X-i);
                }
            }
        }
        System.out.println(ans);
    }
}

Measured memory_bytes: 27180
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;
const int INF=1000000000;

int main(){
    int N;cin >> N;
    double p[N];
    for (int i = 0; i < N; i++) {
        cin >> p[i];
    }
    vector<vector<double>> dp(N+1,vector<double> (N+1,0.0));
    dp[0][0]=1.0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            dp[i+1][j+1]+=dp[i][j]*p[i];
            dp[i+1][j]+=dp[i][j]*(1-p[i]);
        }
    }
    double ans=0.0;
    for (int i = (N+1)/2; i <= N; i++) {
        ans+=dp[N][i];
    }
    cout << setprecision(10) << fixed << ans << endl;
    return 0;
}



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


#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii;
const int MD = (int)1e9 + 7; typedef vector<ll> VL;
void dbg(){ cerr << "\n"; }  template <class T, class ...T2>
void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }
template <class T, class T2> void amax(T& a,T2 b){ if(a < b) a = b;}


int main()
{
	cin.tie(0); ios_base::sync_with_stdio(false);
	int n, q, k;
	string s1, s2;
	cin >>n >>s1 >>s2;
	ll ans = 1;
	rep(i, n){
		if(s1[i] == s2[i]){
			if(i==0) ans = 3;
			else if(s1[i-1] != s2[i-1]) ans *=1;
			else ans *=2;
		}else{
			if(i==0) ans = 6;
			else if(s1[i-1] != s2[i-1])ans *=3;
			else ans *=2;
			i++;
		}
		ans %=MD;
	}
	cout << ans <<"\n";
	return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(){

    int a,b;
    scanf("%d %d",&a,&b);
    if(a+b <=16 && a <= 8 && b <= 8 && a > 0 &&b > 0){
        printf("Yay!\n");
    }
    else {
        printf(":(\n");
    }

return 0;
}


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

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define XX              first
#define YY              second
#define PB              push_back
#define MP              make_pair

#define NEWLINE         cerr << "\n";
#define HOISE           cerr << "hoise " << __LINE__ << "\n"
#define PAUSE           system("pause")
#define DEBUG(x)        cerr << #x << " = " << x << "\n"
#define FASTIO          ios_base::sync_with_stdio(false);cin.tie(NULL)

typedef long long ll;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;

const int INF = 1e9 + 23;
const ll INFL = 2e18 + 23;

// #define int long long

inline void sell(ll &money, ll &x, ll cost)
{
    money += x*cost;
    x = 0;
}
inline void buy(ll &money, ll &x, ll cost)
{
    ll canBuy = money/cost;
    money -= canBuy*cost;
    x += canBuy;
}

int32_t main()
{
    FASTIO;

    int n;
    cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++)
        cin >> a[i];

    vector<ll> b;
    for(int i = 0; i < n; i++){
        b.PB(a[i]);
        while(i<n and a[i] == b.back()) 
            i++;
        
        i--;
    }

    ll m = 1000;
    ll has = 0;
    for(int i = 0; i < (int)b.size(); i++){
        if(i+1 < (int)b.size() and  b[i] < b[i+1]){
            // DEBUG(m);
            // DEBUG(has);
            sell(m, has, b[i]);
            
            // DEBUG(m);
            // DEBUG(has);

            buy(m, has, b[i]);
        
            // DEBUG(m);
            // DEBUG(has);
        }
        else{
            sell(m, has, b[i]);
        }
    }
    cout << m;

    return 0;
}

Measured memory_bytes: 3612
import           Control.Monad
import           Data.Array.IO
import           Data.List.Split
import           Data.List
import           Data.Ord

type Index = (Int, Int)
type Ret = IOUArray Index Int

main :: IO ()
main = do
  [w, h, n] <- map read . words <$> getLine
  ipt       <- replicateM n $ map read . words <$> getLine
  ret       <- newArray ((0, 0), (w - 1, h - 1)) 1 :: IO Ret

  forM_ ipt $ \(x : y : a : _) -> case a of
    1 -> mapM_ (\i -> writeArray ret i 0)
               [ (a, b) | a <- [0 .. x - 1], b <- [0 .. h - 1] ]
    2 -> mapM_ (\i -> writeArray ret i 0)
               [ (a, b) | a <- [x .. w - 1], b <- [0 .. h - 1] ]
    3 -> mapM_ (\i -> writeArray ret i 0)
               [ (a, b) | a <- [0 .. w - 1], b <- [0 .. y - 1] ]
    4 -> mapM_ (\i -> writeArray ret i 0)
               [ (a, b) | a <- [0 .. w - 1], b <- [y .. h - 1] ]
    _ -> return ()

  print =<< sum <$> mapM (readArray ret)
                         [ (a, b) | a <- [0 .. w - 1], b <- [0 .. h - 1] ]



Measured memory_bytes: 1404
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>
#define MAXN 300010
#define y1 zjtsb_y1
using namespace std;

int w,h,n,w0;
pair<int,int> a[MAXN];
deque<int> S1,S2,S3;

bool cmp2(pair<int,int> x,pair<int,int> y){
	return x.second<y.second;
}

int gao(int l,int r,int y0,int y1){
	if(l==r) return max(max(w+a[l].second-y0,w+y1-a[l].second),max(y1-y0+a[l].first,y1-y0+w-a[l].first));
	int mid=(l+r)>>1;
	int ymid=a[mid].second;
	int res1=gao(l,mid,y0,a[mid].second);
	int res2=gao(mid+1,r,a[mid+1].second,y1);
	sort(a+l,a+r+1);
	int mid2=l-1;
	while(mid2<r && a[mid2+1].first<=w0) mid2++;
	static int s1[MAXN][3],s2[MAXN][3];
	int ls1=0,ls2=0;
	int t0=y0,t1=y1;
	for(int i=mid2;i>=l;i--){
		s1[++ls1][0]=t0;
		s1[ls1][1]=t1;
		s1[ls1][2]=a[i].first;
		if(a[i].second<=ymid && a[i].second>t0) t0=a[i].second;
		if(a[i].second>ymid && a[i].second<t1) t1=a[i].second;
	}
	s1[++ls1][0]=t0;
	s1[ls1][1]=t1;
	s1[ls1][2]=0;
	t0=y0; t1=y1;
	for(int i=mid2+1;i<=r;i++){
		s2[++ls2][0]=t0;
		s2[ls2][1]=t1;
		s2[ls2][2]=a[i].first;
		if(a[i].second<=ymid && a[i].second>t0) t0=a[i].second;
		if(a[i].second>ymid && a[i].second<t1) t1=a[i].second;
	}
	s2[++ls2][0]=t0;
	s2[ls2][1]=t1;
	s2[ls2][2]=w;
	S1.clear();
	S2.clear();
	S3.clear();
	int now1=1,now2=1;
	for(int i=1;i<=ls2;i++){
		while(!S3.empty() && s2[S3.front()][1]-s2[S3.front()][0]+s2[S3.front()][2]<s2[i][1]-s2[i][0]+s2[i][2])
			S3.pop_front();
		S3.push_front(i);
	}
	int res=0;
	for(int i=1;i<=ls1;i++){
		while(now1<=ls2 && s2[now1][0]<=s1[i][0]){
			while(!S1.empty() && s2[S1.front()][1]+s2[S1.front()][2]<s2[now1][1]+s2[now1][2])
				S1.pop_front();
			S1.push_front(now1++);
		}
		while(now2<=ls2 && s2[now2][1]>=s1[i][1]){
			while(!S2.empty() && s2[S2.front()][2]-s2[S2.front()][0]<s2[now2][2]-s2[now2][0])
				S2.pop_front();
			S2.push_front(now2++);
		}
		while(!S1.empty() && s2[S1.back()][1]>=s1[i][1]) S1.pop_back();
		while(!S2.empty() && s2[S2.back()][0]<=s1[i][0]) S2.pop_back();
		while(!S3.empty() && (s2[S3.back()][0]<=s1[i][0] || s2[S3.back()][1]>=s1[i][1])) S3.pop_back();
		res=max(res,s1[i][1]-s1[i][0]+s2[min(now1-1,now2-1)][2]-s1[i][2]);
		if(!S1.empty()) res=max(res,s2[S1.back()][1]-s1[i][0]+s2[S1.back()][2]-s1[i][2]);
		if(!S2.empty()) res=max(res,s1[i][1]-s2[S2.back()][0]+s2[S2.back()][2]-s1[i][2]);
		if(!S3.empty()) res=max(res,s2[S3.back()][1]-s2[S3.back()][0]+s2[S3.back()][2]-s1[i][2]);
	}
	res=max(res,max(res1,res2));
	return res;
}

int solve(){
	w0=w/2;
	sort(a+1,a+n+1,cmp2);
	return gao(1,n,0,h);
}

int main(){
#ifdef DEBUG
	freopen("F.in","r",stdin);
#endif
	scanf("%d%d%d",&w,&h,&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&a[i].first,&a[i].second);
	int ans1=solve()*2;
	swap(w,h);
	for(int i=1;i<=n;i++) swap(a[i].first,a[i].second);
	int ans2=solve()*2;
	printf("%d\n",max(ans1,ans2));
	return 0;
}


Measured memory_bytes: 9088
#include <bits/stdc++.h>
using namespace std;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int n,m;
	cin>>n>>m;
	int x,y,z=0,k;
	vector<int>w[200001];
	for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        w[x].push_back(y);
        w[y].push_back(x);
    }
    bool r[200001]={0};
    for(int i=1;i<=n;i++)
    {
        if(r[i])continue;
        k=1;
        vector<int>u;
        u.push_back(i);
        r[i]=1;
        int p=0;
        while(1)
        {
            if(p>=u.size())
                break;
            int ui=w[u[p]].size();
            for(int j=0;j<ui;j++)
            {
                if(r[w[u[p]][j]])
                    continue;
                r[w[u[p]][j]]=1;
                k++;
                u.push_back(w[u[p]][j]);
            }
            p++;
        }
        z=max(z,k);
    }
    cout<<z<<endl;
	return 0;
}


Measured memory_bytes: 15424
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define outy printf("Yes\n")
#define outn printf("No\n")
#define outf printf("First\n")
#define outs printf("Second\n")
#define FOR(a,b,c) for(int a=b;a<=c;a++)
#define FORD(a,b,c) for(int a=b;a>=c;a--)
#define FORL(a,b) for(int a=head[b];a;a=nxt[a])
#define in(a)  a=read()
#define in2(a,b)  a=read(),b=read()
#define in3(a,b,c)  a=read(),b=read(),c=read()
#define in4(a,b,c,d)  a=read(),b=read(),c=read(),d=read()
#define inl(a)  a=readl()
#define inl2(a,b)  a=readl(),b=readl()
#define inl3(a,b,c)  a=readl(),b=readl(),c=readl()
#define inl4(a,b,c,d)  a=readl(),b=readl(),c=readl(),d=readl()
#define out(a) printf("%d\n",a)
#define out2(a,b) printf("%d %d\n",a,b)
#define out3(a,b,c) printf("%d %d %d\n",a,b,c)
#define out4(a,b,c,d) printf("%d %d %d %d\n",a,b,c,d)
#define outl(a) printf("%lld\n",a)
#define outl2(a,b) printf("%lld %lld\n",a,b)
#define outl3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define outl4(a,b,c,d) printf("%lld %lld %lld %lld\n",a,b,c,d)
#define clr(x,y) memset(x,y,sizeof(x))
inline int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
    return s*t;
}
inline ll readl(){
    char c;ll s=0;int t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
	return s*t;
}
/*------------------------------------------------------------*/
#define mod 1000000007
#define maxn 100010
/*------------------------------------------------------------*/
int a[100][100];
char c[100][100];
int fx[8][2]={{0,-1},{0,1},{1,0},{1,-1},{1,1},{-1,-1},{-1,0},{-1,1}}; 
int main(){
	int n,m;
	in2(n,m);
	FOR(i,1,n){
		FOR(j,1,m){
			c[i][j]=getchar();
		}
		getchar();
	}
	FOR(i,1,n)
		FOR(j,1,m)
			if(c[i][j]=='.'){
				FOR(k,0,7)
					a[i][j]+=(c[i+fx[k][0]][j+fx[k][1]]=='#');
			}
			 else a[i][j]=-1;
	FOR(i,1,n){
		FOR(j,1,m)
			a[i][j]==-1?putchar(c[i][j]):printf("%d",a[i][j]);
		printf("\n");
	}
}

Measured memory_bytes: 384
n=int(input())
w=list(map(int,input().split()))
sum1,sum2 = 0,sum(w)
ans=float('inf')
for i in range(n):
    sum1 += w[i]
    sum2 -= w[i]
    ans=min(ans,abs(sum1-sum2))
print(ans)    


Measured memory_bytes: 3060
#include <iostream>
using namespace std;
int main(){
    int S;
    cin >> S;
    int s;
    s = S % 60;
    
    int m;
    
    m = (S/60)%60;
    
    if (m > 60){
        m = 0;
    }
    
    int h;
    h = S/(60*60);
    cout << h<< ":" << m << ":" << s <<"\n";
}


Measured memory_bytes: 3092
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define per(i, n) for (int i = (n)-1; i > -1; i--)
#define reps(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define sper(i, s, n) for (int i = (s); i >= (int)(n); i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;

void solve()
{
    string s;
    cin >> s;
    int q;
    cin >> q;
    bool flip = false;
    rep(i, q)
    {
        int t;
        cin >> t;
        if (t == 1)
        {
            if (!flip)
                flip = true;
            else
                flip = false;
        }
        else
        {
            int f;
            string c;
            cin >> f >> c;
            if (f == 1)
            {
                if (!flip)
                    s.insert(0, c);
                else
                    s += c;
            }
            else
            {
                if (flip)
                    s.insert(0, c);
                else
                    s += c;
            }
        }
    }
    if (flip)
        reverse(s.begin(), s.end());
    cout << s << endl;
}

int main()
{
    solve();
    return 0;
}

Measured memory_bytes: 3836
from collections import deque
S=input()
Q=int(input())

X=deque(S)
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            if f==-1:
                X.append(A[2])
        if A[1]=="2":
            if f==-1:
                X.appendleft(A[2])
            if f==1:
                X.append(A[2])

if f==-1:
    print("".join(reversed(X)))
else:
    print("".join(X))

Measured memory_bytes: 8564
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

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

const int INF = 100000000;

int main(){
	while(1){
		int n;
		cin >> n;
		if(!n) break;

		vector<P> rw(n);
		rep(i, n){
			cin >> rw[i].first >> rw[i].second;
		}
		sort(all(rw));

		int rsum = 0;
		int wsum = 0;
		rep(i, n){
			rsum += rw[i].first;
			wsum += rw[i].second;
		}
		if(rsum <= 2*rw[n-1].first){
			// 1〜n-1のwで作れる「rsum - 2*rw[n-1].first」以下の最大の整数
			int diff = 2*rw[n-1].first - rsum;
			bool dp[1001] = {};
			dp[0] = true;
			rep(j, n-1){
				for(int i = 1000; i >= 0; i--){
					if(!dp[i]) continue;
					if(i+rw[j].second > diff) continue;
					dp[i+rw[j].second] = true;
				}
			}
			int ssum = 0;
			rep(i, 1001){
				if(dp[i]) ssum = i;
			}
			cout << 2*rw[n-1].first + wsum - ssum << endl;

		}else{
			cout << rsum + wsum << endl;
		}
	}

	return 0;
}

Measured memory_bytes: 1204
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<cstdlib>
#include<list>
#include<set>
#include<numeric>

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll A[100010];

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

	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}

	ll ans = 0;
	for (ll i = 0; i < N; i++) {
		if (A[i] != 0) {
			ans += A[i] / 2;
			if (A[i] % 2 == 0)A[i] = 0;
			else if (A[i + 1] != 0 && i != N - 1) {
				A[i + 1] -= 1;
				ans += A[i] % 2;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int gcd(int x, int y) {
    if(y == 0) {
        return x;
    }
    return gcd(y, x%y);
}

int main() {
    int n;
    cin >> n;
    vector<int> A(n);
    rep(i, n) {
        cin >> A[i];
    }
    int ans = A[0];

    for(int i = 1; i < n; i++) {
        ans = gcd(ans,A[i]);
    }

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

Measured memory_bytes: 640
#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

typedef long long ll;

ll n, m, x;
string s[805];
ll sx, sy;
queue<ll> qx, qy, qi;
bool v[805][805];
ll ans = 4e18;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};

int main()
{
    scanf("%lld%lld%lld", &n, &m, &x);
    for(ll i = 0; i < n; i++)
    {
        cin>>s[i];
        for(ll j = 0; j < m; j++)
        {
            if(s[i][j] == 'S') sx = i, sy = j;
        }
    }
    v[sx][sy] = true;
    qx.push(sx), qy.push(sy), qi.push(0);
    while(!qx.empty())
    {
        ll t1 = qx.front();
        ll t2 = qy.front();
        ll t3 = qi.front();
        qx.pop(), qy.pop(), qi.pop();
        if(t3 > x) break;
        ans = min(ans, t1);
        ans = min(ans, t2);
        ans = min(ans, n - t1 - 1);
        ans = min(ans, m - t2 - 1);
        for(ll i = 0; i < 4; i++)
        {
            if(t1 + dx[i] < 0 || t1 + dx[i] >= n) continue;
            if(t2 + dy[i] < 0 || t2 + dy[i] >= m) continue;
            if(s[t1 + dx[i]][t2 + dy[i]] == '#') continue;
            if(v[t1 + dx[i]][t2 + dy[i]]) continue;
            v[t1 + dx[i]][t2 + dy[i]] = true;
            qx.push(t1 + dx[i]), qy.push(t2 + dy[i]), qi.push(t3 + 1);
        }
    }
    printf("%lld\n", (ans + 2 * x - 1) / x);
    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#include <stdio.h>
#include <string>
  using namespace std;
#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) int ab;cin>>ab;(p).push_back(ab);
#define vecpl2(p) long long ab;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define set0(k,n) for(int nn=0;nn<(n);nn++){ (k).push_back(0); }
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define vecsize(b,size) vector<int> (b)((size));
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;

 		
int main() {

int N,M;
cin>>N>>M;
vec(A);
set0(A,N+1)

vector<pair<int,int>> S;
vector<pair<int,int>> T;
fx(M){
int a,b;
	cin>>a>>b;
	S.push_back(make_pair(b,a));
        T.push_back(make_pair(b,a));
}

sorts(S)
map<int,int> B;
fx(M){
	int p=A.at(S.at(x).second)+1;
	A.at(S.at(x).second)++;
B[S.at(x).first]=p;
}
fx(M){
	string K;
	int i=0;
string a1=to_string(T.at(x).second).c_str();
	while(i++<6-a1.size()){
		K+='0';
	}
	K+=to_string(T.at(x).second).c_str();
string a2=to_string(B[T.at(x).first]).c_str();
i=0;
	while(i++<6-a2.size()){
		K+='0';
	}
	K+=to_string(B[T.at(x).first]).c_str();
	ce(K)
}


}

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

#define rep_(v, f, l) for (int v = (f); v < (l); ++v)
#define rep(v, n) rep_(v, 0, n)
#define lep(v, n) rep_(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }

int main()
{
    int a, b;

    while (~scanf("%d %d", &a, &b)){
        printf("%d\n", a + b);
    }
    return (0);
}

Measured memory_bytes: 1188
for _ in range(int(input())):
    A, B = sorted(map(int, input().split()))
    if B - A <= 1:
        print(2*A-2)
    else:
        X = int((A*B)**.5)
        X -= A*B == X*X
        print(2*X-1-(X*(X+1)>=A*B))

Measured memory_bytes: 3060
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
	int N, i, j, k, l, sta_x, sta_y, sta_z, sta_switch = 0, check = 0, x[105] = {}, y[105] = {};
    long hight, l_hight, z[105] = {};
	cin >> N;
	for (i = 0;i < N;i++) {
		cin >> x[i] >> y[i] >> z[i];
		if (z[i] > 0 && sta_switch == 0) {
			sta_x = x[i];
			sta_y = y[i];
			sta_z = z[i];
			sta_switch = 1;
		}
	}
	for (j = 0;j < 101;j++) {
		for (k = 0;k < 101;k++) {
			hight = sta_z + abs(j - sta_x) + abs(k - sta_y);
			check = 0;
			for (l = 0;l < N;l++) {
				l_hight = hight - abs(j - x[l]) - abs(k - y[l]);
				if (l_hight < 0) {
					l_hight = 0;
				}
				if (z[l] != l_hight) {
					check = 1;
				}
			}
			if (check == 0) {
				cout << j << " " << k << " " << hight;
				return 0;
			}
		}
	}
}

Measured memory_bytes: 256