akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push
#define mp make_pair
#define mt make_tuple
#define eb emplace
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, a, n) for (ll i = a; i < (ll)(n); ++i)
#define repr(i, a, n) for (ll i = n - 1; (ll)a <= i; --i)
#define lb lower_bound
#define ub upper_bound
#define ENDL '\n'
typedef pair<int, int> Pi;
typedef pair<ll, ll> PL;
const ll mod = 1000000007;
const ll INF = 1000000099;
const ll LINF = (ll)(1e18 + 99);
const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T, typename U>
inline bool chmin(T &t, const U &u)
{
  if (t > u)
  {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T, typename U>
inline bool chmax(T &t, const U &u)
{
  if (t < u)
  {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T>
inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T mpow(T a, T n)
{
  T res = 1;
  for (; n; n >>= 1)
  {
    if (n & 1)
      res = res * a;
    a = a * a;
  }
  return res;
}

signed main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int h, w, k;
  cin >> h >> w >> k;
  V<string> v(h);
  VV<bool> used(h, V<bool>(w, false));

  queue<ll> mov;
  rep(i, 0, h)
  {
    cin >> v[i];
    rep(j, 0, w)
    {
      if (v[i][j] == 'S')
      {
        mov.push(i);
        mov.push(j);
        mov.push(k);
        used[i][j] = true;
      }
    }
  }

  ll ans=INF;
  while (mov.size())
  {
    int g, r, num;
    g = mov.front();
    mov.pop();
    r = mov.front();
    mov.pop();
    num = mov.front();
    mov.pop();

    chmin(ans,1 + (g+k-1)/k);
    chmin(ans,1 + (r+k-1)/k);
    chmin(ans,1 + (h-1-g+k-1)/k);
    chmin(ans,1 + (w-1-r+k-1)/k);


    if (num == 0)
      continue;

    rep(i, 0, 4)
    {
      int neg = g + dx[i], ner = r + dy[i];
      if (0 <= neg && neg < h && 0 <= ner && ner < w && (v[neg][ner] == '.') && !used[neg][ner])
      {
        used[neg][ner] = true;
        mov.push(neg); 
        mov.push(ner);
        mov.push(num - 1);
      }
    }
  }
  cout<<ans<<ENDL;
}
//( ・ __ ・ ) KEEP BEING ORGANIZED
//CHECK tuserflow,vector_size,what to output?

Measured memory_bytes: 1152
#include <bits/stdc++.h>
using namespace std;
int main(){
    int x,min,max;
    cin>>x;
    min=x;
    max=x;
    for(int i=1;i<5;i++){
        cin>>x;
        if(x>max) max=x;
        if(x<min) min=x;
    }
    cin>>x;
    cout<<(max-min>x?":(":"Yay!")<<"\n";
}


Measured memory_bytes: 256
#include <cstdio>
typedef long long ll;

const int N = 1001000, p = 1e9+7;
inline int qm(const int x) {return x < p ? x : x - p;}
int n, a, b, c, d;

int main() {
	scanf("%d",&n);
	if(n == 1) return puts("1"), 0;
	if(n == 2) return puts("4"), 0;
	c = n, b = (ll)n * n%p;
	int sm = 0;
	for(int i=n-2;i;--i) {
		sm = qm(sm + d);
		a = (sm + b + (ll)(n-1)*(n-1) + i+1)%p;
		d = c, c = b, b = a;
	}
	printf("%d\n", b);
	return 0;
}

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

int main()
{
    int n;
    cin >> n;
    string s[n];
    vector<string> v;
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        v.push_back(s[i]);
    }
    int m;
    cin >> m;
    string t[m];
    for (int i = 0; i < m; i++)
    {
        cin >> t[i];
        v.push_back(t[i]);
    }

    int ans = 0;
    for (int i = 0; i < v.size(); i++)
    {
        int cnt = 0;
        for (int j = 0; j < n; j++)
        {
            if (v[i] == s[j])
                cnt++;
        }
        for (int j = 0; j < m; j++)
        {
            if (v[i] == t[j])
                cnt--;
        }
        ans = max(ans, cnt);
    }
    cout << ans << endl;
}


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

signed main(){
  struct UnionFind{
    int n;
    vector<int> r,p;
    UnionFind(){}
    UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
    int find(int x){
      return (x==p[x]?x:p[x]=find(p[x]));
    }
    bool same(int x,int y){
      return find(x)==find(y);
    }
    void unite(int x,int y){
      x=find(x);y=find(y);
      if(x==y) return;
      if(r[x]<r[y]) swap(x,y);
      r[x]+=r[y];
      p[y]=x;
    }
  };
  int N,K,L;
  cin>>N>>K>>L;
  UnionFind ro(N),li(N);
  for(int i=0;i<K;i++){
    int p,q;
    cin>>p>>q;
    p--;q--;
    ro.unite(p,q);
  }
  for(int i=0;i<L;i++){
    int p,q;
    cin>>p>>q;
    p--;q--;
    li.unite(p,q);
  }
  std::vector<int> v(N);
  for(int i=0;i<N;i++)v[i]=ro.find(i)*1000000+li.find(i);
  sort(v.begin(),v.end());
  for(int i=0;i<N;i++){
    int l=0;
    int r=N-1;
    int m=(l+r)/2;
    while(v[m]!=ro.find(i)*1000000+li.find(i)){
      if(v[m]<ro.find(i)*1000000+li.find(i))l=m+1;
      else r=m-1;
      m=(l+r)/2;
    }
    int ans=1;
    for(int i=m-1;v[i]==v[m]&&i>=0;i--)ans++;
    for(int i=m+1;v[i]==v[m]&&i<N;i++)ans++;
    cout<<ans<<" ";
  }
  cout<<endl;
}


Measured memory_bytes: 8448
#include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    int aux;
    cin >> a >> b >> c;
    if( a > b)
    swap( a, b );
    if( a > c )
    swap( a , c );
    if( b > c)
    swap( b ,c );
    
    cout<<a<<" "<<b<<" "<<c<<endl;

    return 0; 
}


Measured memory_bytes: 3096
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
fn main(){
    let (n,y) = {
        let t = read_vec::<i64>();
        (t[0],t[1])
    };
    for j in 0..n+1{
        for k in 0..n+1{
            if 10000 * j + 5000 * k + 1000*(n-j-k) == y && (n-j-k) >= 0 {
                println!("{} {} {}",j,k, (n-j-k));
                return;
            }
        }
    }
    println!("-1 -1 -1");
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=s;i<n;i++)

typedef vector<double> vd;
typedef vector<vd> vvd;

int main()
{
    int r; cin >> r;
    cout << r * r << endl;
    return 0;
}

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

using namespace std;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;

  vector<int> a(Q), b(Q), c(Q), d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  int ans = 0;
  queue<vector<int>> que;
  que.push(vector<int>(1, 1));

  while (true) {
    vector<int> A = que.front();
    que.pop();
    if ((int)A.size() == N + 2) {
      break;
    }

    if ((int)A.size() == N + 1) {
      int score = 0;
      for (int i = 0; i < Q; i++) {
        if (A[b[i]] - A[a[i]] == c[i]) {
          score += d[i];
        }
      }
      ans = max(ans, score);
    }

    int tail = A.back();
    for (int add_elem = tail; add_elem <= M; add_elem++) {
      A.push_back(add_elem);
      que.push(A);
      A.pop_back();
    }
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;

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

	int n;
	cin >> n;
	long double res;
	cin >> res;
	bool g = true;
	for (int i = 1; i < n; i++) {
		double a;
		cin >> a;
		if ((long long) a == 0) {
			cout << "0\n";
			return 0;
		}
		res *= a;
		if (res > 1e18) {
			g = false;
		}
	}
	if (g)
		cout << (long long) res << '\n';
	else
		cout << "-1\n";

	return 0;
}


Measured memory_bytes: 3748
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int n,type[100],f[100][3][3],m,ans;
char s1[100],s2[100];
int main(){
    scanf("%d",&n);
    scanf("%s%s",s1,s2);
    int i=0;
    while (i<n) {
        if (s1[i]==s2[i]) {
            type[++m]=0;
            i++;
        }
        else {
            type[++m]=1;
            i+=2;
        }
    }
    if (type[1]==0) for (int i=0;i<3;i++) f[1][i][i]=1;
    else {
        for (int i=0;i<3;i++)
            for (int j=i+1;j<3;j++) 
                f[1][i][j]=f[1][j][i]=1;
    }
    for (int i=2;i<=m;i++) {
        if (type[i]==0){
            for (int j=0;j<3;j++)
            for (int k=0;k<3;k++)
            for (int l=0;l<3;l++)
            if (l!=j&&l!=k) 
            f[i][l][l]=(f[i][l][l]+f[i-1][j][k])%mod;
        }
        else {
            for (int j=0;j<3;j++)
            for (int k=0;k<3;k++)
            for (int l=0;l<3;l++)
            for (int o=0;o<3;o++)
            if (l!=j&&o!=k&&l!=o) 
            f[i][l][o]=(f[i][l][o]+f[i-1][j][k])%mod;
        }
    }
    for (int i=0;i<3;i++)
    for (int j=0;j<3;j++) ans=(ans+f[m][i][j])%mod;
    printf("%d\n",ans);
}

Measured memory_bytes: 256
import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc: Scanner = new Scanner(System.in)
    val x: Int = sc.nextLine.toInt
    val a: Int = sc.nextLine.toInt
    val b: Int = sc.nextLine.toInt
    println((x - a) % b)

  }
}


Measured memory_bytes: 25540
// g++ macro.cpp -std=c++14
#include <bits/stdc++.h> //STL include
typedef long long ll;// long long int
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
using namespace std;//namespace

#define dump(x)  cout << #x << " = " << (x) << endl;// debug cout
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)// for macro
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define CIN(x) int x;cin>>x;
#define COUT(x) cout<<(x)<<endl;

#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_;
#define VECCOUT(x) for(auto&youso_: (x) )cout<<youso_<<" ";cout<<endl;

#define ALL(obj) (obj).begin(),(obj).end()// iterator

#define P pair<int,int>
#define V vector<int>

#define pb(a) push_back(a)//push_back
#define mp make_pair// make_pair


int main(){

    CIN(a);CIN(b);CIN(c);
    set<int> s;
    s.insert(a);s.insert(b);s.insert(c);
    COUT(s.size());
    return 0;
}


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

#define MAX_INPUT_SIZE  1024

int
main(int argc, char* argv[])
{
    char input[MAX_INPUT_SIZE];

    int time = atoi(fgets(input, sizeof(input), stdin));

    int hour   = time / 60 / 60;
    int minute = (time - (hour*60*60)) / 60;
    int sec    = (time - (hour*60*60) - (minute*60));

    printf("%d:%d:%d\n", hour, minute, sec);

    return 0;
}

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_V = 200005;
int N;

vector<int> tree[MAX_V];  // ツリーを隣接リスト形式のグラフ構造で表したもの
using II = pair<int, int>;
vector<int> centers;

int dist[MAX_V];
int calcDist(int pos, int pre, int len = 0)
{
  int ret = len;
  dist[pos] = len;
  for(int e : tree[pos]) {
    if(e == pre) continue;
    ret = max(ret, calcDist(e,pos,len+1));
  }
  return ret;
}

//直径のパス
vector<int> vec;
//直径のペアを返す
II calcD()
{
  memset(dist, 0, sizeof(dist));
  int k = calcDist(0,-1);
  II ret;
  REP(n,N) {
    if(dist[n] == k) ret.first = n;
  }
  memset(dist, 0, sizeof(dist));
  k = calcDist(ret.first, -1);
  REP(n,N) {
    if(dist[n] == k) ret.second = n;
  }

  vec.clear();
  int pos = ret.second;
  while(true) {
    vec.push_back(pos);
    if(pos == ret.first) break;
    for(int n : tree[pos]) {
      if(dist[n] == dist[pos]-1) {
        pos = n;
      }
    }
  }
  // cerr << ret.first << "," << ret.second << endl;
  return ret;
}

//自分から見て勝ちか負けか
bool check(int pos, int remain)
{
  if(remain == 0) {
    return pos%3 != 2;
  } else {
    bool l = check(pos+1, remain-1);
    bool r = check(pos, remain-1);
    cerr << pos << " " << remain << " " << l << " " << r << endl;
    return !l || !r;
  }
}

signed main()
{
  cin >> N;
  REP(n,N-1) {
    int a, b; cin >> a >> b; a--; b--;
    tree[a].push_back(b); tree[b].push_back(a);
  }

  calcD();

  // for(int v : vec) {
  //   cerr << v << " "; 
  // }
  // cerr << endl;

  int turn = max((LL)0, (LL)(vec.size()+1)/2-1);
  // cerr << turn << endl;
  if(vec.size()%2 == 1) {
    if(turn%3 != 2) {
      cout << "First" << endl;
    } else {
      cout << "Second" << endl;
    }
  } else {
    if(turn%3 != 0) {
      cout << "First" << endl;
    } else {
      cout << "Second" << endl;
    }
  }

  return 0;
}

Measured memory_bytes: 27384
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

const int NMAX=200000;
ll A[NMAX],B[NMAX];

int main(){
  int N; cin>>N;
  rep(i,N) cin>>A[i];
  rep(i,N) cin>>B[i];
  ll ans=0;
  rep(i,29){
    ll T=(1<<i);
    ll a[N],b[N];
    rep(j,N){
      a[j]=A[j]%(2*T);
      b[j]=B[j]%(2*T);
    }
    sort(b,b+N);
    ll tmp=0;
    rep(j,N){
      int x,y,z;
      x=N-(lower_bound(b,b+N,T-a[j])-b);
      y=N-(lower_bound(b,b+N,2*T-a[j])-b);
      z=N-(lower_bound(b,b+N,3*T-a[j])-b);
      tmp+=x-y+z;
    }
    ans+=tmp%2*T;
  }
  cout<<ans<<endl;
  return 0;
}


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

int n, a, b;
int res = 0;

int main(){
  cin >> n >> a >> b;
  for(int i = 1; i <= n; i++){
    int sum = 0, tmp = i;
    while(tmp > 0){
      sum += tmp % 10;
      tmp /= 10;
    }
    if(sum >= a && sum <= b)res += i; 
  }
  cout << res << endl;
  return 0; 
}

Measured memory_bytes: 256
n,c,k = map(int,input().split())
t = sorted([int(input()) for _ in range(n)])
ans,ppl = 1,1
temp = t[0]
for v in t[1:]:
  if v-temp>k or ppl>=c:
    ans += 1
    ppl = 1
    temp = v
  else:
    ppl += 1
  #print(ans)
print(ans)

Measured memory_bytes: 14088
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    
    int N,K;
    cin>>N>>K;
    vector<pair<ll,ll>> dt(N);
    //種類数を固定する
    //種類が同じ中では美味しさが大きいものを優先するとしてよい
    rep(i,N){
        ll t,d;
        cin>>t>>d;
        dt[i] = {d, t};
    }
    sort(all(dt));
    reverse(all(dt));
    map<ll, ll> map;
    ll ans = 0;
    ll sum = 0;
    ll l = 0;
    rep(i,N){
        //大きいK個
        if(i < K){
            map[dt[i].second]++;
            sum += dt[i].first;
            if(i == K-1){
                ll syu = map.size();
                ans = sum + syu * syu;
                l = K-1;
            }
        }
        else{
            //まだ選んでいない種類のものを考えていく
            if(map[dt[i].second] == 0){
                //左端を戻す
                //一個のやつは除いたらダメ
                //除くと種類数を増やすことができないから
                while(0 <= l && map[dt[l].second] == 1){
                    l--;
                }
                if(l == -1)break;
                sum += dt[i].first;
                sum -= dt[l].first;
                map[dt[i].second]++;
                map[dt[l].second]--;
                l--;
                ll syu = map.size();
                chmax(ans, sum + syu * syu);
            }
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 5760
H,W = map(int,input().split(" "))
N = int(input())
a = list(map(int,input().split(" ")))

grid = [[0]*W for _ in range(H)]
grid_iter = [0,0]
for i in range(N):
	for j in range(a[i]):
		grid[grid_iter[1]][grid_iter[0]] = i+1

		if grid_iter[0] == 0 and grid_iter[1] % 2 == 1:
			grid_iter = [0,grid_iter[1]+1]

		elif grid_iter[0] == W-1 and grid_iter[1] % 2 == 0:
			grid_iter = [W-1,grid_iter[1]+1]

		else:
			if grid_iter[1] % 2 == 1:
				grid_iter[0] -= 1
			else:
				grid_iter[0] += 1

for i in range(H):
	for j in range(W):
		print(str(grid[i][j]) + " ",end = "")
	print()






Measured memory_bytes: 3956
n = int(input())
a = [int(input()) for i in range(5)]
t = 0
b = 0
for i in range(5):
    if a[i] == min(a) and a[i] != b:
        if n % a[i] == 0:
            t = t + n//a[i]
        else:
            t = t + n//a[i] + 1
        b = a[i]
    else:
        t = t + 1
print(t)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define MOD 1000000007
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;
VVI adj;
int n, m;
VI res;
int dfs(int v) {
	if(res[v] == INT_MIN) {
		res[v] = 0;
		for (int u : adj[v]) {
			res[v] = max(res[v],1+dfs(u));
		}
	}
	return res[v];
}
void solve() {
	cin >> n >> m;
	adj.assign(n, VI());
	res.resize(n, INT_MIN);
	for (int i = 0; i < m; ++i)
	{
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		adj[u].push_back(v);
	}
	int max_ = 0;
	for(int i = 0; i < n; i++) {
		max_ = max(max_,dfs(i));
	}
	cout << max_;
}

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

	return 0;
}

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

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S sum(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *max_element(all(a));}

template<class T>void puta(T&&t){cout<<t<<endl;}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>void tf(bool b,S t,T f){if(b)puta(t);else puta(f);}
void OUT(bool b){tf(b,"YES","NO");}
void Out(bool b){tf(b,"Yes","No");}
void out(bool b){tf(b,"yes","no");}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1;for(auto s:t){os<<(a?"":" ")<<s;a=0;}return os;}
template<class S>auto&operator>>(istream&is,vector<S>&t){for(S&a:t)cin>>a;return is;}

int popcnt(int x){return __builtin_popcount(x);}
int popcnt(ll x){return __builtin_popcountll(x);}
int bsr(int x){return 31 - __builtin_clz(x);}
int bsr(ll x){return 63 - __builtin_clzll(x);}
int bsf(int x){return __builtin_ctz(x);}
int bsf(ll x){return __builtin_ctzll(x);}

/*他のライブラリを入れる場所*/

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	using data=tuple<ll,ll,ll>;
	vector<data> edge;
	rep(i,m){
		ll a,b,c;
		cin>>a>>b>>c;
		a--;b--;
		edge.emplace_back(a,b,-c);
	}
	vl ans(n,LINF);
	ans[0]=0;
	vl ret=ans;
	rep(lp,n*2){
		bool ok=false;
		vl nxt=ans;
		for(auto&e:edge){
			ll a,b,c;
			tie(a,b,c)=e;
			ok|=chmin(nxt[b],ans[a]+c);
		}
		//cout<<nxt<<endl;
		if(lp+1>=n and ok and ret[n-1]!=nxt[n-1]){
			puta("inf");
			return 0;
		}
		ans=nxt;
		if(lp+2==n)ret=ans;
	}
	puta(-ret[n-1]);
	return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <cstdio>
#include <algorithm> // sort
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <utility>

using namespace std;

typedef long long int lli;
typedef pair<lli,lli> p_ii;

#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl
int bug = 1;

vector<string> grid;
vector<vector<lli>> ans;
vector<lli> cnt;

int main(){
	IOS
	lli i, j;
	lli h, w, k;
	lli sub;

	cin >> h >> w >> k;
	grid.resize(h);
	ans.resize(h, vector<lli>(w, -1));
	cnt.resize(h, 0);
	for(i = 0; i < h; i++){
		cin >> grid[i];
	}

	for(i = 0; i < h; i++){
		for(j = 0; j < w; j++){
			if(grid[i][j] == '#'){
				cnt[i]++;
			}
		}
	}

	sub = 1;
	bool found = false;
	lli fir_line;
	for(i = 0; i < h; i++){
		if(!found && cnt[i] > 0){
			found = true;
			fir_line = i;
		}
		if(!found && cnt[i] == 0){
			continue;
		}

		bool fir_found = false;
		for(j = 0; j < w; j++){
			if(cnt[i] == 0){
				ans[i][j] = ans[i-1][j];
			}
			else{
				if(grid[i][j] == '#'){
					if(fir_found){
						sub++;
					}
					else{
						fir_found = true;
					}
				}
				ans[i][j] = sub;
			}
		}
		if(cnt[i] > 0){
			sub++;
		}
	}

	for(i = 0; i < fir_line; i++){
		for(j = 0; j < w; j++){
			ans[i][j] = ans[fir_line][j];
		}
	}

	for(i = 0; i < h; i++){
		for(j = 0; j < w; j++){
			cout << ans[i][j];
			if(j != w-1){
				cout << " ";
			}
		}
		cout << endl;
	}

	
	return 0;
}


Measured memory_bytes: 1664
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define REP(i,n) for (int i = 0; i < n; ++ i)
#define FOR(i,a,b) for (int i = a; i <= b; ++ i)
#define FORD(i,b,a) for (int i = b; i >= a; -- i)
#define debug(x) cerr << #x << ": " << x << "\n"
#define task ""
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> li;
typedef vector<int> vi;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen(task".inp", "r", stdin);
	//freopen(task".out", "w", stdout);
	ll m, n;
	cin >> n >> m;
	if (n > m) swap(n, m);
	if (n == 1) {
		if (m == 1) cout << 1;
		else {
			cout << m - 2;
		}
	} else cout << (n - 2) * (m - 2);
}


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

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S sum(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
int sgn(const double&r){return (r>EPS)-(r<-EPS);} // a>0  : sgn(a)>0
int sgn(const double&a,const double&b){return sgn(a-b);} // b<=c : sgn(b,c)<=0

template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>void tf(bool b,S t,T f){if(b)puta(t);else puta(f);}
void YN(bool b){tf(b,"YES","NO");}
void Yn(bool b){tf(b,"Yes","No");}
void yn(bool b){tf(b,"yes","no");}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1;for(auto s:t){os<<(a?"":" ")<<s;a=0;}return os;}
template<class S>auto&operator>>(istream&is,vector<S>&t){for(S&a:t)cin>>a;return is;}

/*他のライブラリを入れる場所*/


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll a,n;
    cin>>a>>n;
    vl b(n);cin>>b;
    puta(max(a-sum(b),-1));
    return 0;
}

Measured memory_bytes: 3680
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

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

using namespace std;
using namespace __gnu_pbds;

template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

#define all(x)                      (x).begin(),(x).end()
#define Sort(x)                     sort(all((x)))
#define X                           first
#define Y                           second
#define Mp                          make_pair
#define sep                         ' '
#define endl                        '\n'
#define debug(x)                    cerr << #x << " = " << x << endl
#define SZ(x)                       ll(x.size())
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define set_random                  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll poww(ll a, ll b, ll md) {
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

ll n, k, ans, sorted = 1, C, P[MAXN];
set<ll> st;

int main() {
    fast_io;

    cin >> n >> k;
    for (ll i = 0; i < n; i++) cin >> P[i];
    for (ll i = 0; i < k; i++) st.insert(P[i]);
    for (ll i = k - 2; i >= 0; i--) {
        if (P[i] > P[i + 1]) break;
        sorted++;
    }
    if (sorted == k) C = 1;
    else ans++;

    for (ll i = k; i < n; i++) {
        if (P[i] > P[i - 1]) sorted++;
        else sorted = 1;
        sorted = min(k, sorted);
        st.insert(P[i]);
        if (((*st.begin()) != P[i - k] || (*prev(st.end())) != P[i]) && sorted != k) ans++;
        if (((*st.begin()) != P[i - k] || (*prev(st.end())) != P[i]) && sorted == k) C = 1;
        st.erase(P[i - k]);
    }
    cout << ans + C << endl;

    return 0;
}
/*

*/


Measured memory_bytes: 11264
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int INF = 1000100100;

int main()
{
    int n; cin >> n;
    int a[n][n];
    rep(i,n){
        rep(j,n){
            cin >> a[i][j];
        }
    }

    bool exist = true;
    rep(k,n){
        rep(i,n){
            rep(j,n){
                if(a[i][j] > a[i][k]+a[k][j]){
                    exist = false;
                    break;
                }
            }
        }
    }

    if(exist){
        rep(k,n){
            rep(i,n){
                rep(j,n){
                    if(i == j || j == k || k == i) continue;
                    if(a[i][j] == a[i][k]+a[k][j]){
                        a[i][j] = INF;
                    }
                }
            }
        }
        ll ans = 0;
        rep(i,n){
            for(int j=i+1; j<n; j++){
                if(a[i][j] == INF) continue;
                ans += a[i][j];
            }
        }
        cout << ans << endl;
    }else{
        cout << -1 << endl;
    }

    
}

Measured memory_bytes: 3936
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    }

    static class C {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.ni();
            String s = in.ns();
            int[] wCnt = new int[n + 1];
            for (int i = 0; i < n; i++) {
                wCnt[i + 1] = wCnt[i];
                if (s.charAt(i) == '.')
                    wCnt[i + 1]++;
            }
            int best = n;
            for (int i = 0; i <= n; i++) {
                int blackLeft = i - wCnt[i];//excl
                int whiteRight = wCnt[n] - wCnt[i];
                best = Math.min(best, blackLeft + whiteRight);
            }
            out.println(best);
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String ns() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String rl = in.readLine();
                    if (rl == null) {
                        return null;
                    }
                    st = new StringTokenizer(rl);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(ns());
        }

    }
}



Measured memory_bytes: 26708
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <climits>
#include <stack>
#include <string>
#include <random>
#include<set>
#include <iomanip>
#include <climits>
#include <memory>

class Sum_bit {
	std::vector<int> _array;
public:
	explicit Sum_bit(const int size) :_array(size, 0) {};
	void increment(int position, const int value) {
		while (position < _array.size()) {
			_array[position] += value;
			position += ~position & (position + 1);
		}
	}
	int operator[](int index) const {
		auto result = 0;
		while (index >= 0) {
			result += _array[index];
			index -= ~index & (index + 1);
		}
		return result;
	}
};
int find_first(int from, int to, const Sum_bit &bit, const int target) {
	while (from < to) {
		auto mid = (from + to) / 2;
		if (bit[mid] < target) {
			from = mid + 1;
		}
		else {
			to = mid;
		}
	}
	return from;
}
struct Star {
	int x, y, bright;
};
int main() {
	int n, d; std::cin >> n >> d;
	std::vector<Star> stars(n);
	for (auto &star : stars) std::cin >> star.x >> star.y >> star.bright;
	std::sort(stars.begin(), stars.end(), [](const Star &left, const Star &right) {return left.bright < right.bright; });
	Sum_bit xBit(2000001), yBit(2000001);
	std::queue<Star> queue;
	long long int max = 0;
	for (const auto star : stars) {
		while (!queue.empty() && queue.front().bright + d < star.bright) {
			xBit.increment(queue.front().x, -1);
			yBit.increment(queue.front().y, -1);
			queue.pop();
		}
		queue.push(star);
		xBit.increment(star.x, 1);
		yBit.increment(star.y, 1);
		long long int xMin = find_first(0, 2000000, xBit, 1);
		auto xMax = find_first(0, 2000000, xBit, queue.size());
		auto yMin = find_first(0, 2000000, yBit, 1);
		auto yMax = find_first(0, 2000000, yBit, queue.size());
		if (max < (xMax - xMin) * (yMax - yMin)) {
			max = (xMax - xMin) * (yMax - yMin);
		}
	}
	std::cout << max << std::endl;
}


Measured memory_bytes: 23116
#include <bits/stdc++.h>
using namespace std;
int N; 
long long M; 
vector<int> E[100010];
long long root0[100010], inv[100010], ans[100010];
void dfs1(int now, int parent){
	long long tmp = 1LL; 
	for(int i : E[now]){
		if(i == parent) continue; 
		dfs1(i, now); 
		tmp *= root0[i] + 1; 
		tmp %= M; 
	}
	if(now == 0){
		ans[0] = tmp; inv[0] = 1; 
	}
	root0[now] = tmp;
	return;
}
void dfs2(int now, int parent){
	vector<long long> left, right; 
	long long tmp = 1; 
	left.push_back(1); 
	for(int i = 0; i < (int) E[now].size(); i++){
		if(E[now].at(i) == parent){
			left.push_back(tmp); continue; 
		}
		tmp *= root0[E[now].at(i)] + 1; tmp %= M; 
		left.push_back(tmp); 
	}
	tmp = 1; right.push_back(1); 
	for(int i = E[now].size() - 1; i >= 0; i--){
		if(E[now].at(i) == parent){
			right.push_back(tmp); continue; 
		}
		tmp *= root0[E[now].at(i)] + 1; tmp %= M; 
		right.push_back(tmp); 
	}
	reverse(right.begin(), right.end()); 
	for(int i = 0; i < (int) E[now].size(); i++){
		if(E[now].at(i) == parent) continue; 
		inv[E[now].at(i)] = left[i] % M * right[i+1] % M; 
		if(now != -1){
			inv[E[now].at(i)] *= inv[now]; inv[E[now].at(i)] %= M;  
		}
		inv[E[now].at(i)] ++; inv[E[now].at(i)] %= M; 
		ans[E[now].at(i)] = root0[E[now].at(i)] * inv[E[now].at(i)] % M; 
		dfs2(E[now].at(i), now); 
	}
	return; 
}
int main(){
	cin >> N >> M; 
	for(int i = 0; i < N-1; i++){
		int x, y; cin >> x >> y; 
		x--; y--; 
		E[x].push_back(y); E[y].push_back(x); 
	}
	dfs1(0, -1); 
	dfs2(0, -1); 
	for(int i = 0; i < N; i++){
		cout << ans[i] << "\n"; 
	}
}

Measured memory_bytes: 29404
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {
	string s1, s2, s3;
	cin >> s1 >> s2 >> s3;
	string res = "AXC";
	res[1] = s2[0];
	cout << res << endl;
}


Measured memory_bytes: 3468
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on

void main()
{
    long N, M;
    scan(N, M);
    auto A = new long[](N);
    auto B = new long[](N);
    foreach (i; 0 .. N)
        scan(A[i], B[i]);
    zip(A, B).sort();

    long ans;
    foreach (i; 0 .. N)
    {
        if (M <= 0)
        {
            writeln(ans);
            return;
        }
        ans += min(B[i], M) * A[i];
        M -= B[i];
    }
    writeln(ans);
}


Measured memory_bytes: 13432
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <random>
#include <cstdio>
#include <cstring>
#include <cmath>

// for i in f..<t {... : as Swift
#define forin(i, f, t) for(ll i = f; i < t; i++)

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main() {
    vector<P> lsc, rsc;
    ll n;
    cin >> n;
    forin(i, 0, n) {
        ll l, r;
        cin >> l >> r;
        lsc.push_back(P{i, l});
        rsc.push_back(P{i, r});
    }
    sort(lsc.begin(), lsc.end(), [](P lhs, P rhs) {
        return lhs.second < rhs.second;
    });
    sort(rsc.begin(), rsc.end(), [](P lhs, P rhs) {
        return lhs.second > rhs.second;
    });
    
    vector<bool> used(n, false);
    
    ll lenU = 0;
    ll count = 0;
    ll pos = 0;
    auto ls = lsc, rs = rsc;
    bool vec = true;
    while (count++ < n) {
        auto& cvec = vec ? ls : rs;
        auto &cp = cvec.back();
        while (used[cp.first]) {
            cvec.pop_back();
            cp = cvec.back();
        }
        if ((vec && pos < cp.second) || (!vec && cp.second < pos)) {
            lenU += abs(cp.second - pos);
            pos = cp.second;
        }
        used[cp.first] = true;
        vec = !vec;
    }
    lenU += abs(pos);
    
    ll lenV = 0;
    ls = lsc; rs = rsc;
    count = 0;
    pos = 0;
    vec = false;
    used.assign(n, false);
    while (count++ < n) {
        auto& cvec = vec ? ls : rs;
        auto &cp = cvec.back();
        while (used[cp.first]) {
            cvec.pop_back();
            cp = cvec.back();
        }
        if ((vec && pos < cp.second) || (!vec && cp.second < pos)) {
            lenV += abs(cp.second - pos);
            pos = cp.second;
        }
        used[cp.first] = true;
        vec = !vec;
    }
    lenV += abs(pos);
    
    
    cout << max(lenU, lenV) << endl;
    
}



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

int main( ) {
  char c1,c2,c3;
  cin >> c1 >> c2 >> c3;
  
  if(c1 == '1'){
    c1 = '9';
  } else if(c1 == '9'){
    c1 = '1';
  }
  
  if(c2 == '1'){
    c2 = '9';
  } else if(c2 == '9'){
    c2 = '1';
  }
  
  if(c3 == '1'){
    c3 = '9';
  } else if(c3 == '9'){
    c3 = '1';
  }
  
  cout << c1 << c2 << c3;
  
}

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

int main() {
  string S;
  cin >> S;
  string T;
  cin >> T;
  
  int a = S.size();
  int b = T.size();
  int c = 0;
  
  if(b==a+1){
    for(int i=0; i < a; i++){
      if(S.at(i)==T.at(i)){
      }
      else{
        c++;
        break;
      }
    }//for
  }//if
  else{
    c++;
  }
  
  if(c==0){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
}

Measured memory_bytes: 3632
/*
Author:loceaner
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
	char c = getchar();
	int x = 0, f = 1;
	for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
	for( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
	return x * f;
}


int n, a[A];

bool cmp(int a, int b) {
	return a > b;
}

signed main() {
	n = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	sort(a + 1, a + 1 + n, cmp);
	bool col = 0;
	int it = 1, ans = 0;
	for (int i = 1; i < n; i++) {
		ans += a[it];
		if(it == 1) it++;
		else if(col == 1) col=0, it++;
		else col = 1;
	}
	cout << ans << '\n';
	return 0;
}


Measured memory_bytes: 4752
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <unordered_map>
#include <bitset>
#include <numeric>
#include <vector>
#include <unordered_set>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <numeric>
#include <cstdlib>


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

const ll mod=1000000007ll;
const ll inf=1000000000000000ll;



int main(){
    string s,ans;
    ll num;
    cin>>s;


    for(ll p=0;p<(1<<(s.length()-1));p++){
        num=s[0]-'0';
        ans=s[0];
        for(ll i=0;i<s.length()-1;i++){
            if(p&(1<<i)){
                num-=s[i+1]-'0';
                ans=ans+'-'+s[i+1];
            }
            else{
                num+=s[i+1]-'0';
                ans=ans+'+'+s[i+1];
            }
        }
        if(num==7){
            break;
            }
        }
        cout<<ans+"=7"<<endl;
    }


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

int main(){
    int a,b;
    cin >> a >> b;
    if(a > b){
    cout << "a > b" << endl;
    }
    else if(a == b){
    cout << "a == b" << endl;
    }
    else {
    cout << "a < b" << endl;  
    }
    
    return 0;
}


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

int main()
{
    int W, H, N;
    std::cin >> W >> H >> N;
    std::vector<int> p(N);
    for (int i = 0; i < N; i++) {
        std::cin >> p[i];
    }
    
    int ux = 0, uy = 0, ud = 0;
    int rx = 0, ry = 1, rd = 0;

    constexpr int dx[] = {+1, +0, -1, +0};
    constexpr int dy[] = {+0, +1, +0, -1};
    
    int adj_count = 0;
    for (int i = 0; i < N; i++) {
        if (p[i] == 0) {
            ux += dx[ud];
            uy += dy[ud];
            if ((ud == 0 && ux == W - 1) ||
                (ud == 1 && uy == H - 1)) {
                ud++;
            } 
        } else {
            rx += dx[rd];
            ry += dy[rd];
            if ((rd == 0 && rx == W - 2) ||
                (rd == 1 && ry == H - 2)) {
                rd++;
            }
        }
        for (int j = 0; j < 4; j++) {
            int nx = ux + dx[j];
            int ny = uy + dy[j];
            if (nx == rx && ny == ry) {
                adj_count++;
                break;
            }
        }
    }
    
    std::cout << adj_count << std::endl;
    
    return 0;
}

Measured memory_bytes: 3080
N = int(input())
D = [int(i) for i in input().split()]

key = max(D) + 1
count_list = [0] * key

for i in range(N):
    count_list[D[i]] += 1

counter = 1
if 0 in count_list:
    counter = 0

if count_list[0] != 1:
    counter = 0

if D[0] != 0:
    counter = 0 

for i in range(1, key - 1):
    counter *= count_list[i]**count_list[i + 1]

print(counter%998244353)

Measured memory_bytes: 14396
($H,$W)=glob<>;
@_=map[/./sg],<>;
for$Y(0..$H-1){for$X(0..$W-1){
	if($_[$Y][$X]eq'.'){
		for$y(($-=$Y-1)..$Y+1){for$x(($-=$X-1)..$X+1){
			$_[$Y][$X]+=$_[$y][$x]eq'#'
		}}
	}
}}
print@$_ for@_


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

using namespace std;
int main(int argc, char *argv[]) {
	int n;
	cin >> n;
	string s;
	cin >> s;
	
	string ans = "Yes";
	if (n % 2 == 1) {
		ans = "No";
	} else {
		for (int i = 0; i < n / 2; i++) {
			if (s[i] != s[i + n/2]) {
				ans = "No";
				break;
			}
		}
	}
	
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <climits>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string.h>
#include <iomanip>
#include <ctype.h>

#define ll          long long int
#define ull         unsigned long long int
#define pb          push_back
#define pii         pair<int,int>
#define F           first
#define S           second
#define vi          vector<int>
#define vli          vector<ll>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define M_PI        3.14159265358979323846
#define mp          make_pair
#define sz(x)       (int)x.size()
//#define hell        1000000007
#define matrix      vector<vector<ll>>
#define forn(i,a,b)  for(int i=a;i<b;i++)
#define eps 0.0000000001
#define all(a)      (a).begin(),(a).end()
const long long INF = (long long) 1e7+1;
const long long mod = (long long) 998244353 ;
const long long hell = (long long) 1000000007;

using namespace std;
#include <stdio.h>

ll power(ll x,ll y)
{
  ll res=1;
  while(y>0){
    if(y%2) res=(x%hell*res%hell)%hell;
    x=(x%hell*x%hell)%hell;
    y=y/2;
  }
  return res;
}

ll modinv(ll x){
  return power(x,hell-2);
}

void solve();


void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main(){
   fastIO();
   int test;
   //cin>>test;
   test=1;
   
   while(test--)
   solve();
}
//void sieve();
ll pw(ll k,ll x);
ll gcd(ll x,ll y);

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

void solve(){
    int a,b,m;
    cin>>a>>b>>m;
    ll pa[a],pb[b],pc[m];
    
    forn(i,0,a)
    cin>>pa[i];
    forn(i,0,b)
    cin>>pb[i];
    
    ll ans=INT_MAX;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        ll c;
        cin>>c;
        
        ans=min(ans,pa[x-1]+pb[y-1]-c);
    }
    sort(pa,pa+a);
    sort(pb,pb+b);
    ans=min(ans,pa[0]+pb[0]);
    cout<<ans;
}    


///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1

ll gcd(ll a,ll b)
{
    while( b != 0)
	{
		ll  t = b;
		b= a %t;
		a = t;
	}
	return a;
}


ll pw(ll k,ll x){
  if(x==0)
  return 1;
  if(x&1)
  return ((pw(k,x-1))*k);
  else
  return ((pw(k,x/2))*(pw(k,x/2)));
}





Measured memory_bytes: 1792
import heapq
N, M = map(int, input().split())
A = list(map(lambda x: -int(x), input().split()))
heapq.heapify(A)

ans = 0
for i in range(M):
    most_expensive = -heapq.heappop(A)
    most_expensive //= 2
    heapq.heappush(A, -most_expensive)

print(-sum(A))

Measured memory_bytes: 72216
local h, w = io.read("*n", "*n", "*l")
local tasks = {}
local len = {}
for i = 1, h do
  s = io.read()
  for j = 1, w do
    local idx = (i - 1) * w + j
    if s:sub(j, j) == "#" then
      len[idx] = 0
      table.insert(tasks, idx)
    else
      len[idx] = 1000000007
    end
  end
end
local done = 0

local function walk(src, dst)
  if len[src] + 1 < len[dst] then
    len[dst] = len[src] + 1
    table.insert(tasks, dst)
  end
end

for i = 1, h * w do
  local idx = tasks[i]
  if w < idx then walk(idx, idx - w) end
  if idx <= (h - 1) * w then walk(idx, idx + w) end
  if 1 < w then
    if idx % w ~= 0 then walk(idx, idx + 1) end
    if idx % w ~= 1 then walk(idx, idx - 1) end
  end
end
local ans = len[1]
local mma = math.max
for i = 2, h * w do
  ans = mma(ans, len[i])
end
print(ans)


Measured memory_bytes: 35932
//2017-11-5
//miaomiao
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define pb push_back
#define ppb pop_back
#define For(i, a, b) for(int i = (a); i <= (int)(b); ++i)

#define N (300000+5)

vector<int> ans;

int main(){	
	int k, n;
	scanf("%d%d", &k, &n);
	
	if(!(k&1)){
		printf("%d", k/2);
		For(i, 2, n) printf(" %d", k);
		return 0;
	}
	
	For(i, 1, n) ans.pb((k+1)/2);
	int nn = n / 2;

	while(nn--){
		if(ans.back() == 1) ans.ppb();
		else{
			--ans.back();
			For(i, ans.size()+1, n) ans.pb(k);
		}
	}

	For(i, 0, ans.size()-1) printf("%d%c", ans[i], i==((int)ans.size()-1)? '\n': ' ');

	return 0;
}

Measured memory_bytes: 3568
#include <iostream>
#define rep(i,n) for (int i=0; i<(n); i++)
using namespace std;

int main(){
    int n;
    cin >> n;
    int p[n];
    rep(i,n) cin >> p[i];

    int result = 1;
    int Min=p[0];
    for(int i=1; i<n; i++){
        Min = min(p[i], Min);
        if(p[i]==Min){
            result++;
        }
    }
    cout << result << endl;
    return 0;
}

Measured memory_bytes: 1024
a=input().split()
N=int(a[0])
X=int(a[1])
m=[]
min=X
for i in range(0,N):
    b=int(input())
    m.append(b)
    if min>m[i]:
        min=m[i]
    X-=m[i]
print(X//min+N)

Measured memory_bytes: 3064