akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define ll long long                                    // long long省略
#define pb push_back                                    // push_back省略
#define mp make_pair                                    // make_pair省略
#define fi first                                        // first省略
#define se second                                       // second省略
#define itn int                                         // int誤字保険
#define count cout                                      // cout誤字保険
#define vecotr vector                                   // vector誤字保険
#define ednl endl                                       // endl誤字保険
#define opt() cin.tie(0); ios::sync_with_stdio(false)   // 入出力速度改善

#define rep(i,l,r) for(ll i=(l);i<(r);i++)               // 範囲[l, r)で刻み1のfor文(順方向)
#define repp(i,l,r,k) for(ll i=(l);i<(r);i+=(k))         // 範囲[l, r)で刻みkのfor文(順方向)
#define rrep(i,l,r) for(ll i=(r-1);i>=(l);i--)           // 範囲[l, r)で刻み1のfor文(逆方向)
#define rrepp(i,l,r,k) for(ll i=(r-1);i>=(l);i-=(k))     // 範囲[l, r)で刻みkのfor文(逆方向)
#define all(x) (x).begin(), (x).end()                   // vectorのポインタ位置指定用
#define max(p,q)((p)>(q)?(p):(q))                       // max拡張
#define min(p,q)((p)<(q)?(p):(q))                       // min拡張
#define bit(n,m)(((n)>>(m))&1)                          // 変数nのm番目のbitを取り出す

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int dy[]={0, 1, 0, -1};                                 // 4方向近傍
int dx[]={1, 0, -1, 0};                                 // 4方向近傍

#define INF ((1LL<<62)-(1LL<<31))
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p) pom(a,p-2,p)

////////////////////////////////////////////////////////////////////////////////////////
int main(){
    int n; cin >> n;
    ll res = INF;
    rep(a,1,n){
        int b = n - a;
        string sa = to_string(a);
        string sb = to_string(b);
        int sum = 0;
        rep(i,0,sa.size())
            sum += sa[i] - '0';
        rep(i,0,sb.size())
            sum += sb[i] - '0';
      	//cout << sum << ednl;
        res = min(res, sum);
    }
    cout << res << ednl;
}


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

using namespace std;

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

    long long n,i,mn = 1e16;
    cin>>n;
    for(i=1;i*i<=n;i++){
        if(n%i==0 && i+n/i<mn)mn=i+n/i;
    }
    cout<<mn-2<<'\n';
	return 0;
}


Measured memory_bytes: 256
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;let mut next =||{iter .next () .unwrap () } ;input_inner !{next ,$($r ) *} } ;($($r :tt ) *) =>{let stdin =std ::io ::stdin () ;let mut bytes =std ::io ::Read ::bytes (std ::io ::BufReader ::new (stdin .lock () ) ) ;let mut next =move ||->String {bytes .by_ref () .map (|r |r .unwrap () as char ) .skip_while (|c |c .is_whitespace () ) .take_while (|c |!c .is_whitespace () ) .collect () } ;input_inner !{next ,$($r ) *} } ;}
macro_rules !input_inner {($next :expr ) =>{} ;($next :expr ,) =>{} ;($next :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($next ,$t ) ;input_inner !{$next $($r ) *} } ;}
macro_rules !read_value {($next :expr ,($($t :tt ) ,*) ) =>{($(read_value !($next ,$t ) ) ,*) } ;($next :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($next ,$t ) ) .collect ::<Vec <_ >>() } ;($next :expr ,chars ) =>{read_value !($next ,String ) .chars () .collect ::<Vec <char >>() } ;($next :expr ,usize1 ) =>{read_value !($next ,usize ) -1 } ;($next :expr ,$t :ty ) =>{$next () .parse ::<$t >() .expect ("Parse error" ) } ;}

fn main() {
   input! {
      n: usize,
      k: usize,
      a: [usize; n],
      f: [usize; n]
   }
   let mut a = a;
   let mut f = f;
   a.sort();
   f.sort_by(|a, b| b.cmp(a));
   {
      let mut s = 0;
      for &a in a.iter() {
         s += a;
      }
      if s <= k {
         println!("0");
         return;
      }
   }
   let mut low = 0;
   let mut high = a[n - 1] * f[0];
   while high - low > 1 {
      let mid = (high + low)/2;
      let mut c = 0;
      for i in 0..n {
         if a[i] > mid / f[i] {
            c += a[i] - mid/f[i];
         }
      }
      if c <= k {
         high = mid;
      } else {
         low = mid;
      }
   }
   println!("{}", high);
}


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

int n, k, sg, sh, fp;
string s;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k >> s;
    for(int i = 1; i < n; ++i) {
    	if(s[i] != s[i - 1]) {
    		++sh;
		} else {
			++fp;
		}
	}
	if(sh / 2 >= k) {
		fp += 2 * k;
	} else {
		fp = n - 1;
	}
	cout << fp << endl;
	return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int a[1000005],mx[4],mxid[4],se[5],seid[5],num[2][100005];
int main(){
	int n;scanf("%d",&n);
	for (int i=1;i<=n;i++){
		int x;scanf("%d",&x);
		num[i&1][x]++;
	}
	for (int i=1;i<=100000;i++){
		for (int j=0;j<=1;j++){
			if (num[j][i]>mx[j]){
				se[j]=mx[j];seid[j]=mxid[j];
				mx[j]=num[j][i];mxid[j]=i;
			}
			else if (num[j][i]>se[j]){
				se[j]=num[j][i];seid[j]=i;
			}
		}
	}
	if (mxid[0]!=mxid[1]) printf("%d",n-mx[0]-mx[1]);
	else printf("%d",n-max( mx[0]+se[1] , mx[1]+se[0] ));
}

Measured memory_bytes: 1024
#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
 string str1, str2;
 int i;
 cin >> str1;
 for(i = 0;i < str1.size();i++){
  str2[i] = str1[str1.size() - i - 1];
 }
 for(i = 0;i < str1.size();i++){
  cout << str2[i];
 }
 cout << endl;
 return 0; 
}

Measured memory_bytes: 1172
#include <bits/stdc++.h>
using namespace std;
int main(){
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	int n,sum=0;
	cin>>n;
	int a[n+1],b[n+1],c[n+1];
	for(int i=1;i<=n;i++){
		cin>>a[i];
	} 
	for(int i=1;i<=n;i++){
		cin>>b[i];
		sum+=b[i];
	} 
	for(int i=1;i<n;i++){
		cin>>c[i];
	}
	for(int i=1;i<n;i++){
		if(a[i]==a[i+1]-1)
		sum+=c[a[i]];
	}
	cout<<sum;
}


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

#define rep(n, i) for(int i = 0; i < n; i++)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

void solve(void){
    int n;
    unsigned long y;
    int bool_f = 0;
    long ans[3] = {-1, -1, -1};
    scanf("%d %ld\n", &n, &y);
    if (10000 * n >= y) {
        for (int i = n; i >= 0; i--) {
            if (i * 10000 > y) continue;
            if (bool_f) break;
            for (int j = n-i; j >= 0; j--) {
                if (i * 10000 + j * 5000 > y) continue;
                if (i * 10000 + j * 5000 + (n-i-j) * 1000 == y) {
                    ans[0] = i; ans[1] = j; ans[2] = n-i-j;
                    bool_f = 1;
                    break;
                }
            }
        }
    }

  printf("%ld %ld %ld\n", ans[0], ans[1], ans[2]);
}

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


Measured memory_bytes: 128
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <unordered_map>

#define rep(i, a, b) for (ll i = a; i < b; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef unordered_map<string, ll> u_map;

// input
void input() {}
template <class Head, class... Tail>
void input(Head &&a, Tail &&... b)
{
    cin >> a;
    input(forward<Tail>(b)...);
}

template <class V>
void input(vector<V> &v)
{
    for_each(v.begin(), v.end(), [](auto &i) { input(i); });
}

// output
template <class V>
void output(vector<V> &v)
{
    for_each(v.begin(), v.end(), [](auto &x) { cout << x << " "; });
    cout << endl;
}

// math functions
ll mpow(ll a, ll n, ll p)
{
    ll x = 1;
    while (n > 0)
    {
        if (n & 1)
        {
            x = a * x % p;
        }
        a = a * a % p;
        n >>= 1;
    }

    return x;
}

ll mfact(ll n, ll p)
{
    ll res = 1;
    for (ll i = 2; i <= n; i++)
    {
        res = res * i % p;
    }
    return res;
}

ll mcomb(ll n, ll r, ll p)
{
    if (n == 0 && r == 0)
    {
        return 1;
    }
    if (n < r || n < 0 || r < 0)
    {
        return 0;
    }

    ll a = mfact(n, p);
    ll b = mfact(n - r, p);
    ll c = mfact(r, p);
    return (((a * mpow(b, p - 2, p)) % p) * mpow(c, p - 2, p)) % p;
}

ll gcd(ll a, ll b)
{
    if (a < b)
    {
        a ^= b;
        b ^= a;
        a ^= b;
    }
    return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}

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

int main()
{
    ll N;
    input(N);

    vector<string> s(N);
    input(s);

    sort(s.begin(), s.end());

    string str = s[0];
    ll cnt = 1;
    vector<pair<string, ll>> res;
    rep(i, 1, N)
    {
        if (str == s[i])
        {
            cnt++;
        }
        else
        {
            res.push_back(pair<string, ll>(str, cnt));
            str = s[i];
            cnt = 1;
        }
    }
    if (s.back() == str)
    {
        res.push_back(pair<string, ll>(str, cnt));
    }

    sort(res.begin(), res.end(), compare);

    ll m = res[0].second;
    cout << res[0].first << endl;
    rep(i, 1, res.size())
    {
        if (res[i].second == m)
        {
            cout << res[i].first << endl;
        }
    }

    return 0;
}

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

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

	vector<int> h(N, 0);
	for(int i=0; i<N;i++) {
		cin >> h.at(i);
	}
	vector<int>::iterator p= h.begin();

	
	vector<int> dp(N, 0);
	dp.at(0) = 0;
	dp.at(1) = abs(*(p+1) - *p);
	
	for(int i=0; i < N-2; i++) {
		dp.at(i+1) = min( dp.at(i+1), dp.at(i) + abs(*(p+1) - *p));
		cerr << i << " :" ;
		cerr << dp.at(i) << " " << dp.at(i+1) << " ";

		if (i < N-2)  { dp.at(i+2) = min( dp.at(i+1) + abs(*(p+2) - *(p+1)), dp.at(i)+abs(*(p+2) - *(p))  );
			cerr << dp.at(i+2) << endl;
		}
		p++;
	}
	cout << dp.at(N-1) <<endl;
}


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

using namespace std;

int main(){
    string s;
    cin >> s;
    int a = 0;
    int b = 0;
    int c = 0;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == 'a') {
            a++;
        } else if (s[i] == 'b') {
            b++;
        } else {
            c++;
        }
    }
        if(a == 1 && b == 1 && c == 1){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 1000005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 +c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
int N;
int t = 0;
void Solve() {
    read(N);
    int a;
    for(int i = 1 ; i <= N ; ++i) {
	read(a);
	t += (a & 1);
    }
    t = t & 1;
    if(t == 0) puts("YES");
    else puts("NO");
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}


Measured memory_bytes: 256
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=sorted(lmp(),reverse=True)
ans=0
for i in range(1,n):
    ans+=a[i//2]
print(ans)

Measured memory_bytes: 97988
import sys

stdin = sys.stdin

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

n = ni()
a = na()

if n % 2 == 0:
    pre = [0]
    for i in range(0, n, 2):
        pre.append(pre[-1] + a[i])
    suf = [0]
    b = a[::-1]
    for i in range(0, n, 2):
        suf.append(suf[-1] + b[i])
    ans = -99999999999999999
    for i in range(len(pre)):
        ans = max(ans, pre[i] + suf[-1-i])
    print(ans)
else:
    pre = [0]
    for i in range(0, n, 2):
        pre.append(pre[-1] + a[i])
    # pre2 = [0]
    # for i in range(1, n, 2):
    #     pre2.append(pre2[-1] + a[i])
    ans = -999999999999999999
    mx = 0
    for i in range(1, n, 2):
        ans = max(ans, mx + pre[-1] - pre[i//2+1])
        mx = max(mx + a[i], pre[i//2+1])
    ans = max(ans, mx)
    print(ans)


Measured memory_bytes: 106812
# -*- coding: utf-8 -*-

s = input()
n = len(s)
ans = 0

if n == 1:
    print(int(s))
    exit()

for i in range(2 ** (n - 1)):

    tmp = int(s[0])

    for j in range(n - 1):

        if (i >> j) & 1:
            ans += tmp
            tmp = 0

        tmp = tmp * 10 + int(s[j + 1])

    ans += tmp

print(ans)


Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main()
{
    int n;
    cin >> n;
    if (n <= 599)
        cout << 8 << endl;
    else if (n <= 799)
        cout << 7 << endl;
    else if (n <= 999)
        cout << 6 << endl;
    else if (n <= 1199)
        cout << 5 << endl;
    else if (n <= 1399)
        cout << 4 << endl;
    else if (n <= 1599)
        cout << 3 << endl;
    else if (n <= 1799)
        cout << 2 << endl;
    else
        cout << 1 << endl;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
int main()
{int n;
 cin>>n;
 int a[n];
 for(int i=0;i<n;i++)
   cin>>a[i];
 long long INF=10e9+5;
 vector<long long>dp(n,INF);
int i,j;
 dp[0]=0;
 for(i=0;i<n;i++)
 { j=i+1;
  if(j<n)
 dp[j]=min(dp[j],dp[i]+abs(a[j]-a[i]));
    j=i+2; 
  if(j<n)
 dp[j]=min(dp[j],dp[i]+abs(a[j]-a[i]));
   //if(j<n)
 // cout<<dp[j];
 }
 cout<<dp[n-1];
  
}


Measured memory_bytes: 4232
/*input
100 50 4
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll sub(ll a,ll b){
    ll x=a-b;
    while(x<0) x+=MOD;
    while(x>MOD) x-=MOD;
    return x;
}
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
const int maxn=1e5+5;
int main(){
	int a,b,c;
	cin>>a>>b>>c;
	vector<int> v;
	REP1(i,100){
		if(a%i==0 and b%i==0){
			v.pb(i);
		}
	}
	reverse(ALL(v));
	--c;
	cout<<v[c];
}

Measured memory_bytes: 384
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

H, W = map(int, input().split())

print("#" * (W + 2))
for _ in range(H):
    line = "#" + input() + "#"
    print(line)
print("#" * (W + 2))

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

int GCD(int n,int m){
  if(m==0){
    return n;
  }
  return GCD(m,n%m);
}

int main(){
  int N;
  cin >> N;
  vector<int> kos(1000005,0);
  vector<int> A(N);
  for(int i=0;i<N;i++){
    cin >> A[i];
    kos[A[i]]++;
  }
  int current=A[0];
  for(int i=1;i<N;i++){
    current=GCD(current,A[i]);
  }
  if(current>1){
    cout << "not coprime" << endl;
    return 0;
  }
  for(int i=2;i<=1000000;i++){
    int count=0;
    int now=i;
    while(now<=1000000){
      count+=kos[now];
      if(count>1){
        cout << "setwise coprime" << endl;
        return 0;
      }
      now+=i;
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}

Measured memory_bytes: 11076
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i,a,b) for(in i=(in)(a);i<(in)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
const double PI=3.14159265358979323846;
const in MOD = 1000000007;
const in INF=1e18+7;
const in inf=1e9+7;
using P=pair<in,in>;
vector<in> dx={0,1,-1,0};
vector<in> dy={1,0,0,-1};

struct union_find{
    vector<in> par;
    vector<in> depth;
    vector<in> siz;

    union_find(in n) : par(n) , depth(n,0) , siz(n,1){
        rep(i,n) par[i]=i;
    }

    in find(in x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }

    void unite(in x,in y){
        x=find(x);
        y=find(y);
        if(x==y) return;
        siz[x] +=siz[y];
        siz[y] =siz[x];
        if(depth[x]<depth[y]) par[x]=par[y];
        else{
            par[y]=par[x];
            if(depth[x]==depth[y]) depth[x] ++;
        }
    }

    bool same(in x,in y){
        return find(x)==find(y);
    }
    in size(in x){
        return siz[find(x)];
    }
};

int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
    in n,m;
    cin>>n>>m;
    vector<in> a(m),b(m);
    rep(i,m){cin>>a[i]>>b[i];a[i]--,b[i]--;}
    reverse(all(a));
    reverse(all(b));
    union_find uf(n);
    stack<in> ans;
    rep(i,m){
        if(uf.same(a[i],b[i])) ans.emplace(0);
       else ans.emplace(uf.size(a[i])*uf.size(b[i]));
        uf.unite(a[i],b[i]);
    }
    in sum=0ll;
    rep(i,m){
        sum +=ans.top();
        cout<<sum<<endl;
        ans.pop();
    }
}
//./problem.exe

Measured memory_bytes: 5916
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

#define mp make_pair
#define pb push_back


typedef long long ll;
typedef long double ld;

using namespace std;

int n;
int a[120000];
char c[120000];
const ll INF = 1e18;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", a + i);
		if (i != n - 1)
			scanf(" %c", c + i + 1);
	}
	c[0] = '+';
	ll mn = INF;
	ll sum = 0;
	ll cur = 0;
	ll now = 0;
	for (int i = 0; i < n; ++i) {
		sum += a[i];
		if (c[i] == '+') {
			cur += a[i];
		}
		else {
			if (now != 0)
				mn = min(mn, now + cur);
			cur = 0;
			now += a[i];
		}
	}
	mn = min(mn, now);
	cout << sum - 2 * mn << "\n";
	return 0;
}




Measured memory_bytes: 896
import java.io.*;
import java.util.*;

public class Main{
    final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray();
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    }
    static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    }
    static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    }
    static int findLargestDiv(int n){
        if(n%2==0) return n/2;
        else{
            for(int i=3;i<=n/2;i+=2){
                if(n%i==0) return n/i;
            }
        }
        return 1;
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        long x,k,d,temp;
        x=s.nextLong();k=s.nextLong();d=s.nextLong();
        x=Math.abs(x);
        temp=x/d;
        x-=(Math.min(temp,k)*d);
        k-=Math.min(k,temp);
        if(k%2==1){
            System.out.println(Math.min(x+d,Math.abs(x-d)));
        }else{
            System.out.println(x);
        }
    }
}

Measured memory_bytes: 25340
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

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

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

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

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

fn solve(a: i32, b: i32, c: i32, x: i32) -> i32 {
    let mut counter = 0;
    for i in 0..a + 1 {
        for j in 0..b + 1 {
            if 500 * i + 100 * j <= x && (x - 500 * i - 100 * j) / 50 <= c {
                counter += 1
            }
        }
    }
    counter
}

fn main() {
    input!{
        a: i32,
        b: i32,
        c: i32,
        x: i32
    }
    println!("{}", solve(a, b, c, x))
}


Measured memory_bytes: 4352
import java.util.*;
 
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int d = sc.nextInt();
    int e = sc.nextInt();
    
    if(a<=123 && a>=1 && b<=123 && b>=1 && c<=123 && c>=1 
       && d<=123 && d>=1 && e<=123 && e>=1){
        int a1 = 0;
      int b1 = 0;
      int c1 = 0;
      int d1 = 0;
      int e1 = 0;
      if(a % 10 != 0){
       a1 = (int)Math.round((a+5)/10.0)*10;
      }else{
        a1 = a;
      }
      if(b % 10 != 0){
       b1 = (int)Math.round((b+5)/10.0)*10;
      }else{
        b1 = b;
      }
      if(c % 10 != 0){
       c1 = (int)Math.round((c+5)/10.0)*10;
      }else{
        c1 = c;
      }
      if(d % 10 != 0){
       d1 = (int)Math.round((d+5)/10.0)*10;
      }else{
        d1 = d;
      }
      if(e % 10 != 0){
       e1 = (int)Math.round((e+5)/10.0)*10;
      }else{
        e1 = e;
      }
      
      int ans = a1 + b1 + c1 + d1 + e1;
      
      int a2 = a1 - a;
      int b2 = b1 - b;
      int c2 = c1 - c;
      int d2 = d1 - d;
      int e2 = e1 - e;
      
      int[] list = {a2,b2,c2,d2,e2};
      
      int intMax = list[0];
      for(int i = 1; i < list.length; i++){
        if(intMax < list[i] && intMax != 10 && list[i] != 10){
          intMax = list[i];
        }
      }
      int answer = ans - intMax;
      System.out.println(answer);
      
    }else{
       System.out.println("入力範囲に違反しています");
    }
  }
}

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

using namespace std; 
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define forin(i, n) for ( i = 0; i < (int)n; ++i )
typedef long long ll;

 
int main()
{
    IOS
 
    int x, n;
    cin >> x >> n;
    int i, minimo = x;
    vector<bool> numeros(102, false);
    forin(i, n)
    {
        int aux;
        cin >> aux;
        numeros[aux] = true;
    }
    int sustract = 0;
    while (1)
    {
        if ( !numeros[x - sustract] ) { cout << ( x - sustract ); break; }
        if ( !numeros[x + sustract] ) { cout << ( x + sustract ); break; }

        sustract++;
    }
    return 0;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
#define INF 1e18
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define MAX 1000
#define ll long long
#define P pair<int,int>
using namespace std;
const int mod = 1000000007;
//char c[MAX][MAX];
int step[MAX][MAX];
int H,W,N; 
int vy[4] ={1,-1,0,0}, vx[4] = {0,0,-1,1}; //up,down,left,right 

signed main(){
    ll a,b,c,k; cin >> a >> b >> c >> k;
    ll ans = 0; 
    if(k <= a) {
        ans += k;
        cout << ans << endl;
        return 0;
    }
    ans += a; k -= a + b;
    if(k > 0) {
        ans -= k;
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

template<typename Monoid,typename F>
class SegmentTree{
private:
    int sz;
    vector<Monoid> seg;
    const F op;//演算
    const Monoid e;//単位元
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<=n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    string S;
    cin >> S;
    auto op = [&](int a,int b){return a+b;};
    SegmentTree<int,decltype(op)> seg(N,op,0);
    vec<SegmentTree<int,decltype(op)>> segs;
    for(int c=0;c<26;c++) segs.push_back(seg);
    for(int i=0;i<N;i++){
        int c = S[i]-'a';
        segs[c].set(i,1);
    }
    for(int c=0;c<26;c++) segs[c].build();
    int Q;
    cin >> Q;
    while(Q--){
        int t;
        cin >> t;
        if(t==1){
            int i;
            char c;
            cin >> i >> c;
            i--;
            int p = S[i]-'a';
            int n = c-'a';
            S[i] = c;
            segs[p].update(i,0);
            segs[n].update(i,1);
        }else{
            int l,r;
            cin >> l >> r;
            l--; r--;
            int ans = 0;
            for(int c=0;c<26;c++) ans += segs[c].query(l,r+1)!=0;
            cout << ans << "\n";
        }
    }
}

Measured memory_bytes: 113692
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(),(v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;  
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
// const ll INF = 1LL << 30;
#define pp pair<ll, pair<ll, ll>> 
#define fi first
#define se second

void print(Vl vec){
    rep(i, 0, sz(vec)){
        if(i) cout << " ";
        cout << vec[i];
    }
    coel;
}

////////////////////////////////////////////////////////


int main() {
    ll n; cin >> n;
    string ans;
    ll b = 1, a = 2;
    bool f = true;
    if(n == 0){ co(0); return 0;}
    while(1){
        if(n == 0) break;
        ll r = n % a;
        if(r < 0) r *= -1;
        ans.push_back('0'+r/b);
        if(f) n -= r, f = false;
        else n += r, f = true;
        b *= 2, a *= 2;
    }
    reverse(ALL(ans));
    co(ans);

    return 0;
}



Measured memory_bytes: 3528
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int k= 1;
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("SUN",7);
        hashMap.put("MON",6);
        hashMap.put("TUE",5);
        hashMap.put("WED",4);
        hashMap.put("THU",3);
        hashMap.put("FRI",2);
        hashMap.put("SAT",1);
        String s1;
        while(k-->0){
            s1=s.next();
            System.out.println(hashMap.get(s1));
        }
    }

    private static int binary_search(
            int arr[], int l,
            int h, int x)
    {
        while (l <= h) {
            int mid = (l + h) / 2;
            if (arr[mid] <= x)
                l = mid + 1;
            else
                h = mid - 1;
        }
        return h;
    }
    static int gcd(int a, int b)
    {
        if(b==0) {
            return a;
        }
        return gcd(b,a%b);
    }
    private static boolean isPrime(int n) {
        if (n == 2)
            return true;
        else if ((n % 2 == 0 && n > 2) || n < 2)
            return false;
        else {
            for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
                if (n % i == 0)
                    return false;
            }
            return true;
        }


    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

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

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

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

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

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

}

Measured memory_bytes: 26816
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n)  FOR(i, 0, n)
#define ASC(c) sort((c).begin(), (c).end())
#define DESC(c) sort((c).begin(), (c).end(), greater<int>())

#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

typedef long long ll;
typedef unsigned long long ull;
struct edge { int u, v; ll w; };
 
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
    int n, x; cin >> n >> x;
    int sum = 0, ans = 1;
    REP(i, n) {
        int tmp; cin >> tmp;
        sum += tmp;
        if (sum > x) break;
        else ans++;
    }
    cout << ans << endl;
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

int main(){
    string s, t;
    int K;
    cin >> s >> K;
    int N = s.length();
    set<string> st;
    REP(i, N){
        for(int j=i+1;j<i+6;j++){
            t = s.substr(i, j-i);
            st.insert(t);
        }
    }
    set<string>::iterator itr = st.begin();
    REP(i, K-1){
        itr++;
    }
    cout << *itr << endl;
    return 0;
}

Measured memory_bytes: 1664
#include <bits/stdc++.h>
#define llong long long
#define f first
#define s second
#define false true

using namespace::std;

int main()
{
    ifstream fin("input.txt");
    int N;
    cin >> N;
    vector<int> arr(3 * N);
    for( int i = 0; i < 3 * N; i++) {
        cin >> arr[i];
    }
    sort(arr.rbegin(), arr.rend());
    llong answ = 0;
    for (int i = 1; i < 2 * N + 1; i += 2) {
        answ += arr[i];
    }
    cout << answ;
}


Measured memory_bytes: 1408
import java.util.*;
import java.io.*;
 
public class Main{
 
	public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        for(int i = 0; i < N; i++){
            A[i] = s.nextLong();
        }

        long result = 0;
        HashMap<Long, Long> map = new HashMap<Long, Long>();
        for(int i = 0; i < N; i++){
            map.put((long)(A[i] + i + 1), map.getOrDefault((A[i] + i + 1), 0L) + 1);
        }

        for(int i = 0; i < N; i++){
            result += map.getOrDefault((long)(i+1 - A[i]), 0L);
        }
        
        
        
        System.out.println(result);
        
        
	}
} 

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

const int INF=1e9;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;

int N;
int Par[100005],Rank[100005];
P x[100005],y[100005];
PP xy[200005]; 

void init(int n){
    for(int i=0; i<n; i++){
        Par[i]=i;
        Rank[i]=0;
    }
}
int find(int x){
    if(Par[x]==x){
        return x;
    }else{
        return Par[x]=find(Par[x]);
    }
}
void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y) return;
 
    if(Rank[x]<Rank[y]){
        Par[x]=y;
    }else{
        Par[y]=x;
        if(Rank[x]==Rank[y]) Rank[x]++;
    }
}
bool same(int x,int y){
    return find(x)==find(y);
}
  
int main(){
 
cin>>N;

for(int i=0; i<N; i++){
    scanf("%d %d",&x[i].first,&y[i].first);
    x[i].second=i; y[i].second=i;
}

sort(x,x+N); sort(y,y+N);

for(int i=1; i<N; i++){
    xy[i-1].first=x[i].first-x[i-1].first; xy[i-1].second=P(x[i].second,x[i-1].second);
}

for(int i=1; i<N; i++){
    xy[i+N-2].first=y[i].first-y[i-1].first; xy[i+N-2].second=P(y[i].second,y[i-1].second);
}

sort(xy,xy+2*N-2);

init(N); 

ll ans=0LL;
for(int i=0; i<2*N-2; i++){
    int p=xy[i].second.first;
    int q=xy[i].second.second;
    if(!same(p,q)){
        unite(p,q); ans+=xy[i].first;
    }
}

cout<<ans<<endl;

}

Measured memory_bytes: 4992
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

from itertools import accumulate

n,m,q = li()
grid = [[0 for _ in range(n+1)] for _ in range(n+1)]

for _ in range(m):
    l,r = li()
    grid[0][r] += 1
    if l+1 <= n:
        grid[l+1][r] -= 1
    
imos = [list(accumulate(grid[row])) for row in range(n+1)]
for row in range(n):
    for col in range(n+1):
        imos[row+1][col] += imos[row][col]
        
for _ in range(q):
    p,q = li()
    print(imos[p][q])

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

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int MAX { 200010 };
const ll MOD { 1000000007 }, oo { 1LL << 62 };

ll solve(int N, const vector<ll>& xs)
{
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);

    int A, B;
    cin >> A >> B;

    cout << max(A - 2*B, 0) << '\n';

    return 0;
}


Measured memory_bytes: 3636
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <utility>
#include <functional>
#include <deque>
#include <cctype>
#include <stack>

using ll = long long;
typedef std::pair<int, int>p;

ll min(ll a, ll b) {
	if (a >= b)return b;
	else return a;
}
ll max(ll a, ll b) {
	if (a >= b)return a;
	else return b;
}
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);

}
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}

const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool flag = true;
bool fl = true;
struct edge { int to, cost; };
std::vector<edge>v[100];
bool used[210];

int main() {
	std::string s;
	int ans = 0;
	std::cin >> s;
	std::stack<char>st;
		for (int i = 0;i < s.size();i++) {
			if (s[i] == 'S') {
				st.push(s[i]);
			}
			else if (s[i] == 'T') {
				if (st.size() == 0)st.push(s[i]);
				else if (st.top() == 'T')st.push(s[i]);
				else if (st.top() == 'S')st.pop();
			}
		}
		ans = st.size();
	std::cout << ans << std::endl;
}

Measured memory_bytes: 644
#include<bits/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i,a,b) for(register sign i=(sign)(a);i<=(sign)(b);++i)
#define Fordown(i,a,b) for(register sign i=(sign)(a);i>=(sign)(b);--i)
const int N=2e5+5;
template<typename T>bool cmax(T &a,T b){return (a<b)?a=b,1:0;}
template<typename T>bool cmin(T &a,T b){return (a>b)?a=b,1:0;}
template<typename T>T read()
{
	T ans=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)&&ch!='-')ch=getchar();
	if(ch=='-')f=-1,ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch-'0'),ch=getchar();
	return ans*f;
}
template<typename T>void write(T x,char y)
{
	if(x==0)
	{
		putchar('0'),putchar(y);
		return;
	}
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	static char wr[20];
	int top=0;
	for(;x;x/=10)wr[++top]=x%10+'0';
	while(top)putchar(wr[top--]);
	putchar(y);
}
void file()
{
#ifndef ONLINE_JUDGE
	freopen("B.in","r",stdin);
	freopen("B.out","w",stdout);
#endif
}
int n,x;
ll sum[N],cost[N];
unsigned long long ans;
void input()
{
	n=read<int>(),x=read<int>();
	For(i,1,n)sum[i]=sum[i-1]+read<int>();
}
void work()
{
	cost[1]=5;
	For(i,2,n)cost[i]=1ll*(i+1)*(i+1)-1ll*i*i;
	//For(i,1,n)cout<<cost[i]<<' ';
	//cout<<endl;
	unsigned long long res;
	int l,r,now;
	ans=1e18;
	For(i,1,n)
	{
	//	num=n/i;
		res=0;
		for(r=n,now=1;r>0;r=l-1,now++)
		{
			l=r-i+1;
			cmax(l,1);
			res=res+(sum[r]-sum[l-1])*cost[now];
			if(res>ans)res=ans;	
		}
		res=res+1ll*x*i;
	//	cout<<i<<' '<<res<<endl<<endl;
	
		cmin(ans,res);
	//	i=n/num;
	}
	write(ans+1ll*n*x,'\n');
}
int main()
{
//	file();
	input();
	work();
	return 0;
}


Measured memory_bytes: 3328
// AOJ 2249 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249)
#include<cstdio>
#include<cstring>
#include<limits>
#include<queue>
#include<set>
#include<vector>
#define rep(i,a) for(int i=0;i<(a);++i)
#define itr(it,a) for(auto it=(a).begin();it!=(a).end();++it)
#define clr(a,v) memset((a),(v),sizeof(a))

typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;

const int MAX_N = 10000, INF = std::numeric_limits<int>::max()>>2;

int N, M;
std::vector<PP> G[MAX_N];
bool used[MAX_N];

int main()
{
	while( scanf( "%d%d", &N, &M ), N|M )
	{
		rep( i, N )
			G[i].clear();

		clr( used, 0 );

		rep( i, M )
		{
			int u, v, d, c;
			scanf( "%d%d%d%d", &u, &v, &d, &c );
			--u; --v;
			G[u].push_back( PP( P( d, c ), v ) );
			G[v].push_back( PP( P( d, c ), u ) );
		}

		std::priority_queue<PP> pque;
		std::vector<P> d( N, P( INF, INF ) );
		pque.push( PP( P( 0, 0 ), 0 ) );
		d[0] = P( 0, 0 );

		int ans = 0;

		while( !pque.empty() )
		{
			PP p = pque.top(); pque.pop();
			int dist = -p.first.first, cost = -p.first.second, v = p.second;

			if( used[v] )
				continue;

			used[v] = true;
			ans += cost;

			rep( i, G[v].size() )
			{
				PP &e = G[v][i];

				if( d[e.second] > P( dist+e.first.first, e.first.second ) )
				{
					d[e.second] = P( dist+e.first.first, e.first.second );
					pque.push( PP( P( -d[e.second].first, -e.first.second ), e.second ) );
				}
			}
		}

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

	return 0;
}

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

int minecount(string s);

int main(){
	int height, width;
	cin >> height >> width ;
	string s[height+2];
	for(int i=1; i<=height; ++i){
		cin >> s[i];
	}
	string edge = "";
	for(int i=0; i<=width+1; ++i){
		edge += ".";
	}
	s[0]=s[height+1]=edge;
	string field[height+2][width+2];
	string nine="";
	string output[height+1];
	for(int i=1; i<=height; ++i){
		for(int j=0; j<width; ++j){
			if(s[i][j]=='#'){
				field[i][j+1] = '#';
			} else {
				if(j==0){
					nine = s[i-1].substr(j  ,2) + s[i].substr(j  ,2) + s[i+1].substr(j  ,2);
				} else if(j == width-1) {
					nine = s[i-1].substr(j-1,2) + s[i].substr(j-1,2) + s[i+1].substr(j-1,2);
				} else {
					nine = s[i-1].substr(j-1,3) + s[i].substr(j-1,3) + s[i+1].substr(j-1,3);
				}
				field[i][j+1] = to_string(minecount(nine));
			}
			output[i] += field[i][j+1];
		}
		cout << output[i] << endl;
	}
	return 0;
}

int minecount(string s){
	int num=0;
	for(int i=0; i<s.length(); ++i){
		if(s[i]=='#') ++num;
	}
	return num;
}

Measured memory_bytes: 384
n, m = gets.strip.split.map(&:to_i)
paths = {}
n.times do |i|
    paths[i + 1] = []
end
m.times do
    a, b = gets.strip.split.map(&:to_i)
    paths[a] << b
    paths[b] << a
end

count = 0
queue = []

queue << [1, [1]]

while !queue.empty? do
    current_node, visited = queue.shift
    edges = paths[current_node]
    unvisited = edges - visited
    if unvisited.empty?
        # これ以上すすめるところがないとき
        if visited.size == n
            # すべての頂点を訪れている
            count += 1
        end
    end
    unvisited.each do |node|
        visited2 = visited.clone
        visited2 << node
        queue << [node, visited2]
    end
end

puts count

Measured memory_bytes: 5628
#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <stack>
#include <limits>
#include <climits>
#include <cassert>
#include <fstream>
#include <cstring>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <ciso646>
#include <set>
#include <array>
#include <unordered_map>

using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define VS vector<string>
#define VI vector<ll>
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define pii pair<ll,ll>
#define pcc pair<char,char>
#define pic pair<ll,char>
#define pci pair<char,ll>
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
	struct input_returnner {
		ll N; input_returnner(ll N_ = 0) :N(N_) {}
		template<typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); }
		template<typename T> operator T() const { T res; cin >> res; return res; }
		template<typename T> T operator - (T right) { return T(input_returnner()) - right; }
		template<typename T> T operator + (T right) { return T(input_returnner()) + right; }
		template<typename T> T operator * (T right) { return T(input_returnner()) * right; }
		template<typename T> T operator / (T right) { return T(input_returnner()) / right; }
		template<typename T> T operator << (T right) { return T(input_returnner()) << right; }
		template<typename T> T operator >> (T right) { return T(input_returnner()) >> right; }
	};
	template<typename T> input_returnner in() { return in<T>(); }
	input_returnner in() { return input_returnner(); }
	input_returnner in(ll N) { return std::move(input_returnner(N)); }
}

const ll MOD = 1e9 + 7;

void solve();

signed main() {
	SETUP;
	solve();
#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}

#define int ll

void solve() {
	int N; cin >> N;
	vector<int> A(N);
	REP(i, N) {
		int a; cin >> a;
		A.push_back(a);
	}
	sort(ALL(A));
	int ma = A[A.size() - 1];
	int index = std::distance(A.begin(), lower_bound(A.begin(), A.end(), ma / 2));
	if (abs(A[index] - ma / 2.0) < abs(A[index - 1] - ma / 2.0)) {
		cout << ma << " " << A[index] << endl;
	}
	else {
		cout << ma << " " << A[index - 1] << endl;
	}
}


Measured memory_bytes: 1792