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

int main(){
    long long K,A,B,ans=1;
    cin>>K>>A>>B;
    if((B-A)<=2)ans=K+1;
    else if((B-A)>=3){
        if(K<=A)ans=K+1;
        else if(A<K){
           ans+=A-1;
           K-=A-1;
           ans+=(B-A)*(K/2);
           if(K%2==1)ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
import queue


class Solution:

    def solve(self, H: int, W: int, S) -> int:

        def onEdge(pos):
            i = pos[0]
            j = pos[1]
            if 0 < i and i < H - 1 and 0 < j and j < W - 1:
                return False
            else:
                return True

        def bfs(start):
            INF = H*W+1
            dist = {}
            for i in range(H):
                for j in range(W):
                    dist[(i, j)] = INF

            max_dist = 0

            dist[start] = 0
            nexts = queue.Queue()
            nexts.put(start)

            while not nexts.empty():
                __next = nexts.get()

                # if onEdge(__next):
                max_dist = max(dist[__next], max_dist)

                for delta in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    h = __next[0] + delta[0]
                    w = __next[1] + delta[1]

                    if h < 0 or h > H - 1 or w < 0 or w > W - 1:
                        continue

                    if S[h][w] != "#":
                        if dist[(h, w)] == INF:
                            nexts.put((h, w))
                            dist[(h, w)] = dist[__next] + 1

            return max_dist

        ans = 0
        for i in range(H):
            for j in range(W):

                # skip if (i,j) is not on the edge
                # if not onEdge((i, j)):
                #     continue

                # skip if (i,J) is a wall
                if S[i][j] == "#":
                    continue

                ans = max(bfs((i, j)), ans)

        return ans


if __name__ == '__main__':

    # standard input
    H, W = map(int, input().split())
    S = []
    for h in range(H):
        S.append(input())

    # solve
    solution = Solution()
    print(solution.solve(H, W, S))


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

int main()
{
  int n, m;
  int a, b;
  cin >> n >> m;
  pair<int, int> pa;
  pa.first = 1;
  pa.second = n;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    if (a > pa.first) pa.first = a;
    if (b < pa.second) pa.second = b;

  }

  int ans = pa.second - pa.first;
  if (ans < 0) cout << 0;
  else cout << ans + 1 << endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  int n;
  int gx,gy;
  int i,j,k,l;
  int num;
  int x1,y1,x2,y2;

  cin>>n;

  rep(k,n){
    cin>>gx>>gy;
    cin>>num;

    int yoko[20][20]={};//0???????????????
    int tate[20][20]={};//0???????????????
    int dp[20][20]={};

    rep(l,num){
      cin>>x1>>y1>>x2>>y2;
      if(x1==x2)
	tate[min(y1,y2)][x1]=1;
      if(y1==y2)
	yoko[y1][min(x1,x2)]=1;
    }

    rep(i,gy+1) rep(j,gx+1){
      if(i>0 && tate[i-1][j]==0)
	dp[i][j]=dp[i-1][j];
      if(j>0 && yoko[i][j-1]==0)
	dp[i][j]+=dp[i][j-1];
      if(i==0 && j==0)
	dp[i][j]=1;
    }

    if(dp[gy][gx])
      cout<<dp[gy][gx]<<endl;
    else
      cout<<"Miserable Hokusai!"<<endl;
  }
}

Measured memory_bytes: 1164
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int sunu[] = new int[n];
    int k = sc.nextInt();
    for(int i = 0;i<k;i++){
      int d = sc.nextInt();
      for(int j = 0;j<d;j++){
        int a = sc.nextInt() -1;
        sunu[a]++;
      }
    }
    int count = 0;
    for(int i = 0;i<n;i++){
      if(sunu[i] == 0){
        count++;
      }
    }
    System.out.println(count);
  }
}


Measured memory_bytes: 45952
if __name__ == '__main__':
    N = int(input())
    p = list(map(int, input().split()))

    counter = 0
    if p[0] == 1:
        counter += 1
        p[0], p[1] = p[1], p[0]

    # print(p)
    for i in range(1, N - 1):
        if p[i] == i + 1:
            counter += 1
            if p[i - 1] != i and p[i + 1] != i + 2:
                p[i - 1], p[i] = p[i], p[i - 1]
            else:
                p[i], p[i + 1] = p[i + 1], p[i]
    if p[N - 1] == N:
        counter += 1

    print(counter)


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

struct StNumCnt
{
  StNumCnt() : m_nNum(0), m_nCnt(0) { }

  int m_nNum;
  int m_nCnt;

  static bool greater_nCnt(const StNumCnt& lhs, const StNumCnt& rhs)
  {
    return lhs.m_nCnt > rhs.m_nCnt;
  }
};

void fnInput(vector<int>& rvnNum)
{
  int nNumSiz;
  cin >> nNumSiz;
  rvnNum.resize(nNumSiz);

  for (int& rnNum : rvnNum)
    cin >> rnNum;
}

int fnMinChangeCnt(const vector<int>& cnrvnNum)
{
  map<int, int> mpNumCntEvn, mpNumCntOdd;

  for (int i = 0; i < cnrvnNum.size(); ++i)
    if ( i % 2 == 0 )
      mpNumCntEvn[ cnrvnNum[i] ]++;
    else
      mpNumCntOdd[ cnrvnNum[i] ]++;

  vector<StNumCnt> voNumCntEvn( mpNumCntEvn.size() ),
                   voNumCntOdd( mpNumCntOdd.size() );

  int n = 0;
  for (pair<int, int> PNumCnt : mpNumCntEvn)
  {
    voNumCntEvn[n].m_nNum   = PNumCnt.first;
    voNumCntEvn[n++].m_nCnt = PNumCnt.second;
  }

  n = 0;
  for (pair<int, int> PNumCnt : mpNumCntOdd)
  {
    voNumCntOdd[n].m_nNum   = PNumCnt.first;
    voNumCntOdd[n++].m_nCnt = PNumCnt.second;
  }

  sort(begin(voNumCntEvn), end(voNumCntEvn), StNumCnt::greater_nCnt);
  sort(begin(voNumCntOdd), end(voNumCntOdd), StNumCnt::greater_nCnt);

  int nMinChangeCnt = INT_MAX;

  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      if (voNumCntEvn[i].m_nNum != voNumCntOdd[j].m_nNum)
        nMinChangeCnt = 
          min( nMinChangeCnt, 
               (int)cnrvnNum.size() - voNumCntEvn[i].m_nCnt - voNumCntOdd[j].m_nCnt );

  return nMinChangeCnt;
}
  
int main()
{
  vector<int> vnNum;
  
  fnInput(vnNum);
  cout << fnMinChangeCnt(vnNum) << endl;

  return 0;
}

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

int main()
{
	string s;
	cin >> s;
	int ans;
	for (int i = 0; i < s.size(); i++) {
		if (s.at(i) == '+') ans++;
		else ans--;
	}
	cout << ans << "\n";
}

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

int main(){
  int n;
  string s[10000];

  cin >> n;
  long long ans = 0;
  int a = 0;
  int b = 0;
  int ba = 0;
  for(int i = 0; i < n ; i++){
    cin >> s[i];
    for(int j = 0; j < s[i].length()-1 ; j++) if(s[i][j] == 'A' && s[i][j+1] == 'B') ans++;
    if(s[i][0] == 'B' && s[i][s[i].length()-1] == 'A') ba++;
    else if(s[i][0] == 'B') b++;
    else if(s[i][s[i].length()-1] == 'A') a++;
  }
  if(ba > 0)ans += ba - 1;
  if(ba == 0) ans += min(a, b);
  else if(b != 0 || a != 0) ans += min(a,b) + 1;

  cout << ans << endl;
}


Measured memory_bytes: 768
#include <iostream>
#include <string>
#define OPEN 1
#define CLOSE 0
using namespace std;

int main() {
  int n;
  cin >> n;
  string u[n];
  for (int i = 0; i < n; i++)
    cin >> u[i];

  int m;
  cin >> m;
  int status = CLOSE;
  for (int i = 0; i < m; i++) {
    string t;
    cin >> t;
    int j;
    for (j = 0; j < n; j++) {
      if (t == u[j]) {
	if (status == CLOSE) {
	  cout << "Opened by " << t << endl;
	  status = OPEN;
	} else {
	  cout << "Closed by " << t << endl;
	  status = CLOSE;
	}
	break;
      }
    }
    if (j == n)	cout << "Unknown " << t << endl;
  }
}

Measured memory_bytes: 3240
const int LG = 21;
const int FN = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
string Yes[2] = {"No", "Yes"};
string YES[2] = {"NO", "YES"};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(FN);
    __ufact.resize(FN);
    __rev.resize(FN);
    __rev[1] = 1;
    for (int i = 2; i < FN; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < FN; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (k < 0 || k > n) return 0;
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int Root(int x, vector<int> &root)
{
    if (x == root[x]) return x;
    return root[x] = Root(root[x], root);
}

void Merge(int v, int u, vector<int> &root, vector<int> &sz)
{
    v = Root(v, root), u = Root(u, root);
    if (v == u) return;
    if (sz[v] < sz[u])
    {
        sz[u] += sz[v];
        root[v] = u;
    }
    else
    {
        sz[v] += sz[u];
        root[u] = v;
    }
}

int ok(int x, int n)
{
    return 0 <= x && x < n;
}

void bfs(int v, vi &dist, vector<vi> &graph)
{
    fill(all(dist), -1);
    dist[v] = 0;
    vi q = {v};
    for (int i = 0; i < q.size(); i++)
    {
        for (auto u : graph[q[i]])
        {
            if (dist[u] == -1)
            {
                dist[u] = dist[q[i]] + 1;
                q.push_back(u);
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int k;
    cin >> k;
    cout << (k / 2) * (k - k / 2);
}

/* Note:
Check constants at the beginning of the code.
    N is set to 4e5 but be careful in problems with large constant factor.
    Setting N in every problem is more effective.
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/


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

using namespace std;
using ll = long long;
constexpr int INF = 1 << 29;

int main() {
    int n, d, x;
    cin >> n >> d >> x;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        int s;
        scanf("%d", &s);
        for (int j = 0; j*s+1 <= d; j++) {
            sum++;
        }
    }
    cout << (sum + x) << endl;
    return 0;
}


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

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

    for (int i = 0; i < n; i++) {
        if (a[i] % 2 == 0 && a[i] % 3 != 0 && a[i] % 5 != 0) {
            cout << "DENIED" << endl;
            return 0;
        }
    }
    cout << "APPROVED" << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=100000+100;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

inline ll readll()
{
    ll x=0;int f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

int n,a[200200];
map<int,int> mp;

int main()
{
    n=read();
    rep(i,1,n) a[i]=read();
    ll ans=0;
    rep(i,1,n)
    {
        ans+=mp[i-a[i]];
        mp[i+a[i]]++;
    }
    cout << ans << endl;
    return 0;
}

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

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

pll A[100005];
vector<ll> B[100005];

int main()
{
    ll N, M;
    cin >> N >> M;
    REP(i, N)
    {
        cin >> A[i].first >> A[i].second;
        B[A[i].first].push_back(A[i].second);
    }

    priority_queue<ll, vector<ll>> q;
    ll ans = 0;
    REP(i, M + 1)
    {
        REP(j, B[i].size())
        {
            q.push(B[i][j]);
        }
        if (q.empty())
            continue;

        ll t = q.top();
        q.pop();
        ans += t;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 7296
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	
	ll n, b,r;
	cin>>n>>b>>r;
	ll md = (n)%(b+r);
	n -= md;
	cout<<(n/(b+r))*b + min(md, b); 	
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>

using namespace std;
using llong = long long;
const int MOD=1000000007;

int main(int argc, char**argv){
    int n;
    cin >> n;
    bool ans = false;
    for (int i = 0; i <= n/4; i++){
        for (int k = 0; k <= n/7; k++){
            if (i*4 + k*7 == n) {
                ans = true;
                break;
            }
        }
    }
    if (ans) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}





Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define For(i,n) for(int i=0;i<n;i++)
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,k,s;
    cin>>n>>k>>s;
    if(n==k){
        For(i,n){
            cout<<s;
            if(i!=n-1){cout<<' ';}
        }
        return 0;
    }
    else if(n>k&&s>3){
        rep(i,0,k+1){
            if(i%2==0)cout<<1<<' ';
            else{cout<<s-1<<' ';}
        }
        rep(i,0,n-k-1){
            cout<<s-2;
            if(i!=n-k-2){cout<<' ';}
        }
        return 0;
    }
    else{
        rep(i,0,k){
            cout<<s<<' ';
        }
        rep(i,0,n-k){
            cout<<10000000;
            if(i!=n-k-1){
                cout<<' ';
            }
        }
        return 0;
    }
}


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

int main(){
  string S;
  cin >> S;
  
  int AA = (S.at(0)-'0')*10 + (S.at(1)-'0');
  int BB = (S.at(2)-'0')*10 + (S.at(3)-'0');
  
  int yymm = 0;
  int mmyy = 0;
  if((0 <= AA && AA <= 99) && (1 <= BB && BB <= 12)) yymm = 1;
  if((0 <= BB && BB <= 99) && (1 <= AA && AA <= 12)) mmyy = 1;
  
  if(1 == yymm && 1 == mmyy) cout << "AMBIGUOUS" << endl;
  else if(1 == yymm) cout << "YYMM" << endl;
  else if(1 == mmyy) cout << "MMYY" << endl;
  else cout << "NA" << endl;
  
}

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

int main(){
  int a, b, C;
  cin >> a >> b >> C;
  double rad = (C*acos(-1))/180.0;

  double S, L, h;
  S = (1/2.0)*a*b*sin(rad);
  L = a + b + sqrt(a*a + b*b - 2*a*b*cos(rad));
  h = b*sin(rad);
  printf("%lf\n%lf\n%lf\n", S, L, h);

return 0;
}

Measured memory_bytes: 3464
#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()		 //sort(all(vi S)) sort(all(string S))
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }

using P = pair<int, int>;		//P.first, P.second
typedef long long ll;
typedef vector<int> vi;
typedef set<int> seti;
typedef vector<string> vs;

const int MOD = 1e9+7;
const int INF = 1e9;
int main() {
    int N;
    cin>>N;
    vector<ll> x(N),y(N),h(N);
    int id = 0;
    for(int i = 0; i < N; ++i){
        cin>>x[i]>>y[i]>>h[i];
        if(h[i]>0){
            id = i;
        }
    }

    for(int a = 0; a <= 100; ++a){
        for(int b = 0; b <= 100; ++b){
            bool AllOK = true;
            ll H = h[id] + abs(a - x[id]) + abs(b - y[id]);
            if(H == 0) continue;
            for(int i = 0; i < N; ++i){
                ll nowH = max(ll(0),H - abs(a-x[i]) - abs(b-y[i]));
                if(nowH != h[i]){
                    AllOK = false;
                }
            }
            if(AllOK){
                cout<<a<<" "<<b<<" "<<H<<endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int sol = k;
    for (int i = 0; i < n - 1; ++i)
        sol *= k - 1;
    cout << sol;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, s, n) for (int i = (s); i < (n); i++)
#ifdef LOCAL
#define INPUT_FILE                   \
  ifstream in("atcoder-problems/arc093_a.txt"); \
  cin.rdbuf(in.rdbuf());
#define print_vec(v) \
rep(l, v.size()) { cout << v[l] << " "; } \
cout << endl;
#else
#define INPUT_FILE
#define print_vec(v)
#endif
#define CIN_OPTIMIZE \
  cin.tie(0);        \
  ios::sync_with_stdio(false);
typedef pair<int, int> P;
typedef long long ll;
typedef pair<ll, ll> pl;
const int INF = 100100100;
const ll LINF = 1e18+100;
const int MOD = 1e9 + 7;

int main() {
  INPUT_FILE CIN_OPTIMIZE;

  int N;
  cin >> N;
  
  vector<int> c(N + 2);
  c[0] = 0;
  c[N + 1] = 0;
  repi(i, 1, N + 1) { cin >> c[i]; }

  ll tot = 0;
  rep(i, N + 1) { tot += abs(c[i] - c[i + 1]); }

  repi(i, 1, N + 1) {
    int f = c[i - 1];
    int s = c[i];
    int l = c[i + 1];

    cout << tot - abs(f - s) - abs(s - l) + abs(f - l) << endl;
  }
}

Measured memory_bytes: 1664
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        try (Scanner in = new Scanner(System.in)) {
            int A = in.nextInt();
            int B = in.nextInt();
            if (A == 1) {
                A += 13;
            }
            if (B == 1) {
                B += 13;
            }
            System.out.println(A > B ? "Alice" : A < B ? "Bob" : "Draw");
        }
    }
}


Measured memory_bytes: 21844
import heapq

def find_root(x):
  if x == nodes[x]:
    return x
  else:
    root = nodes[x] = find_root(nodes[x])
    return root

nv, ne = map(int, input().split())
nodes = [i for i in range(nv)]
q = []

for _ in range(ne):
  s, t, w = map(int, input().split())
  heapq.heappush(q, (w, s, t))

sum = 0

while q:
  w, s, t = heapq.heappop(q)
  root_s = find_root(s)
  root_t = find_root(t)
  if root_s != root_t:
    nodes[root_s] = root_t
    sum += w

print(sum)



Measured memory_bytes: 23252
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include<cstdio>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int N;
  cin >>  N;
  
  vector<int> p(N);
  for (int i = 0; i < N; ++i) cin  >> p.at(i);
  
  int ans = 0;
  
  for (int i = 1; i < N - 1; ++i) {
    int array[3] = { p.at(i - 1), p.at(i), p.at(i + 1)};
    sort(array, array + 3); 
    if (array[1] == p.at(i)) {
      ++ans ;
    }
  }
  
  cout << ans ;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cassert>

using namespace std;
typedef long long lli;
const int N = 10;
const int M = 50;
const lli INF = 1LL << 40;

int w, h, f, m, o;
int dat[N][N];
lli dp[N+1][N][1<<N][M+1];

main(){
  while(cin >> w >> h && (w|h)){
    cin >> f >> m >> o;
    assert(3 <= w && w <= 10);
    assert(3 <= h && h <= 10);
    assert(1 <= f && f <= 10000);
    assert(3 <= m && m <= 50);
    assert(o <= m);
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> dat[i][j];
      }
    }
    fill(dp[0][0][0], dp[N+1][0][0], -INF);
    for(int i=0;i<w;i++){
      if(o - 1 > 0){
        if(dat[0][i] >= 0){
          dp[0][i][1<<i][min(m, (o-1+dat[0][i]))] = 0;
        }else if(dat[0][i] < 0){
          if(f + dat[0][i] >= 0)
            dp[0][i][1<<i][o-1] = dat[0][i];
        }
      }
    }
    for(int i=0;i<h-1;i++){
      lli nm, cost;
      for(int j=0;j<(1<<w);j++){ // 2
        for(int k=1;k<=m;k++){
          for(int d=0;d<w;d++){
            int l = w-1, r = 0;
            for(int k2=0;k2<w;k2++){
              if((j >> k2) & 1){
                l = min(l, k2);
                r = max(r, k2);
              }
            }
            if(l > 0 && k - abs(l-d) - 1 > 0){
              if(dat[i][l-1] >= 0){
                nm = min(m, (k - abs(l-d) - 1 + dat[i][l-1]));
                cost = 0;
              }else if(dat[i][l-1] < 0){
                nm = k - abs(l-d) - 1;
                cost = dat[i][l-1];
              }
              if(f + dp[i][d][j][k] + cost >= 0)
                dp[i][l-1][j|(1<<(l-1))][nm] = max(dp[i][l-1][j|(1<<(l-1))][nm], dp[i][d][j][k] + cost);
            }
            if(r + 1 < w && k - abs(r-d) - 1 > 0){
              if(dat[i][r+1] >= 0){
                nm = min(m, (k - abs(r-d) - 1+ dat[i][r+1]));
                cost = 0;
              }else if(dat[i][r+1] < 0){
                nm = k - abs(r-d) - 1;
                cost = dat[i][r+1];
              }
              if(f + dp[i][d][j][k] + cost >= 0)
                dp[i][r+1][j|(1<<(r+1))][nm] = max(dp[i][r+1][j|(1<<(r+1))][nm], dp[i][d][j][k] + cost);
            }
            for(int p=l;p<=r;p++){
              if(k - abs(p-d) - 1 > 0){
                if(dat[i+1][p] >= 0){
                  nm = min(m, (k - abs(p-d) - 1 + dat[i+1][p]));
                  cost = 0;
                }else if(dat[i+1][p] < 0){
                  nm = k - abs(p-d) - 1;
                  cost = dat[i+1][p];
                }
                if(f + dp[i][d][j][k] + cost >= 0)
                  dp[i+1][p][1<<p][nm] = max(dp[i+1][p][1<<p][nm], dp[i][d][j][k] + cost);
              }
            }
          }
        }
      }
    }
    lli valM = -INF;
    for(int j=0;j<(1<<w);j++){
      for(int k=1;k<=m;k++){
        for(int d=0;d<w;d++){
          valM = max(valM, dp[h-1][d][j][k]);
          //cout << d << ' ' << j << ' ' << k << ' ' << dp[2][d][j][k] << endl;
        }
      }
    }
    if(f + valM < 0){
      cout << "NA" << endl;
    }else{
      cout << abs(valM) << endl;
    }
  }
}

Measured memory_bytes: 46044
from collections import Counter

n = int(input())
C = [Counter(input()) for _ in range(n)]
ret = ''
for k, v in C[0].items():
  count = v
  for c in C[1:]:
    count = min(c.get(k, 0), count)
  if count > 0:
    ret += k * count
print(''.join(sorted(ret)))

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

typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int32_t main()
{
    IOS
    int n;
    cin>>n;
    int ans=0;
    for (int i=2;i*i<=n;i++)
    {
        if (n%i==0)
        {
            int cnt=0;
            while (n%i==0)
                n/=i,cnt++;
           for (int j=1;j<=10;j++)
           {
               if (j*(j+1)/2<=cnt) ans++;
           }
        }
    }
    if (n!=1) ans++;
    cout<<ans<<endl;
}



Measured memory_bytes: 3652
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
template <class T> inline bool chmin(T &a, T b) {if (a > b){a = b;return true;}return false;}
template <class T> inline bool chmax(T &a, T b) {if (a < b){a = b;return true;}return false;}

ll is_p[201010];
int A[201010];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,k; cin>>n>>k;
    for(int i=0; i<n; i++){
        int t;cin>>t;
        A[i+1] = t;
    }
    for(int i=0; i<201010; i++) is_p[i] = 0;
    is_p[1]=1;
    ll now = 1;
    ll next = -1;
    ll r_begin, r_end;
    while(1){
        next = A[now];
        if(is_p[next] == 0){
            is_p[next]=is_p[now]+1;
        }else{
            r_begin = next;
            r_end = now;
            break;
        }
        now = next;
    }
    cerr<<r_begin<<" "<<r_end<<endl;
    ll r_size = is_p[r_end]-is_p[r_begin]+1;
    ll r_dist = is_p[r_begin]-1;
    cerr<<r_size<<" "<<r_dist<<endl;
    if(r_dist >= k+1){
        for(int i=0; i<201010;i++){
            if(is_p[i]==k+1){
                cout<<i<<endl;
                return 0;
            }
        }
    }
    else {
        // 探すべき距離
        ll f = ((k - r_dist) % r_size) + r_dist+1;
        cerr<<f<<endl;
        for(int i=0; i<201010;i++){
            if(is_p[i]==f){
                cout<<i<<endl;
                return 0;
            }
        }
    }

}

Measured memory_bytes: 5928
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>a,b,c;
int n;
main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int k;cin>>k;
		a.push_back(k);
		b.push_back(k);
		c.push_back(k);
	}
	if(prev_permutation(b.begin(),b.end()))
	{
		for(int i=0;i<n;i++)cout<<(i?" ":"")<<b[i];
		cout<<endl;
	}
	for(int i=0;i<n;i++)cout<<(i?" ":"")<<a[i];
	cout<<endl;
	if(next_permutation(c.begin(),c.end()))
	{
		for(int i=0;i<n;i++)cout<<(i?" ":"")<<c[i];
		cout<<endl;
	}
}



Measured memory_bytes: 3132
N, K = list(map(int, input().split()))
S = list(input())
if S[K - 1] == "A":
    S[K - 1] = "a"
elif S[K - 1] == "B":
    S[K - 1] = "b"
else:
    S[K - 1] = "c"

print("".join(S))


Measured memory_bytes: 2940
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <cstdio>
#include <cstring>

using namespace std;

#define Rep(i,a,b) for(int i = a; i < b; i++)
#define rep(i,b) Rep(i,0,b)
#define rrep(i,a) for(int i = a; i >= 0; i--)
#define allof(a) (a).begin(), (a).end()

typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

/*
// 最大公約数
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

// 最小公倍数
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}*/

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

	ll n, c;
	cin >> n >> c;
	vector<ll> x(n + 2), v(n + 2);
	Rep(i, 1, n + 1) cin >> x[i] >> v[i];
	x[0] = 0;
	x[n + 1] = c;

	ll cal = 0;
	vector<vector<ll>>dp(2, vector<ll>(n + 2, 0));

	Rep(i, 1, n + 1) {
		// 最大で移動する地点までのカロリーの最大値をあらかじめ求める
		cal += v[i];
		cal -= x[i] - x[i - 1];
		dp[0][i] = max(dp[0][i - 1], cal);
	}
	cal = 0;
	// 反時計回りの場合も考える
	for (int i = n; i >= 1; i--) {
		cal += v[i];
		cal -= x[i + 1] - x[i];
		dp[1][i] = max(dp[1][i + 1], cal);
	}

	ll maxCal = 0;

	rep(i, n + 1) {
		// 時計回り
		cal = dp[0][i] + dp[1][i + 1] - x[i];
		maxCal = max(maxCal, cal);
		// 反時計回り
		cal = dp[0][i] + dp[1][i + 1] - (c - x[i + 1]);
		maxCal = max(maxCal, cal);
	}

	cout << maxCal << endl;

	return 0;
}

Measured memory_bytes: 4224
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n,w;
    cin>>n>>w;
    vector<ll>weights(n);
    vector<ll>values(n);
    for (ll i = 0;i<n;i++){cin>>weights[i];cin>>values[i];}
    vector<vector<ll>>dp(n,vector<ll>(w+1));
    for (ll i = 0;i<n;i++){
        for (ll j = 0;j<=w;j++){
            if (i==0 && j<weights[i])dp[i][j] = 0;
            else if (i==0)dp[i][j] = values[i];
            else if (j<weights[i])dp[i][j] = dp[i-1][j];
            else dp[i][j] = max(dp[i-1][j],values[i]+dp[i-1][j-weights[i]]);
        }
    }
    cout<<dp[n-1][w]<<endl;
    return 0;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  int A = 1;
  for (int i = 0; i < N; i++) {
    if (A > K)
      A += K;
    else if (A <= K)
      A *= 2;
  }
  cout << A << endl;
}


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

#define INF INT_MAX

using namespace std;

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

int main(void){
 	cin.tie(0);
    ios::sync_with_stdio(false);
	double N,K;
	cin >> N >> K;

	double ans = 0;
	for(int i = 1;i <= N;i++){
		// cout << "i:" << i << endl;
		double a = ((log(K/i))/log(2));
		// printf("pre_a:%.20f\n",a);
		// cout << (int)a << endl;
		a = (double)(a - (int)a) == 0 ? a : a+1; 
		// printf("next_a:%.20f\n",a);
		// cout << (int)a << endl;
		if(a < 0.0)a = 0.0;
		ans += pow(0.5,(int)a)/N;
	}
	std::cout << std::fixed;
	std::cout << std::setprecision(13) << ans << endl;
	return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
#include<cmath>
#include<typeinfo>
#include<vector>
#define rep(i,n) for(int i=0;i<(int)n;i++)
using namespace std;
int main(int argc, char const *argv[]) {
    int a[3];
    bool is_even[3];
    int even=0,odd=0;
    int spell=0;
    rep(i,3)cin>>a[i];
    //rep(i,3)cout<<a[i];
    rep(i,3){
        if(a[i]%2==0){
            even++;
            is_even[i]=true;
        }
        else{
            odd++;
            is_even[i]=false;
        }
    }
    //rep(i,3)cout<<is_even[i];
    //cout << even << odd;
    if(even==2){
        rep(i,3){
            if(is_even[i])a[i]++;
        }
        spell++;
    }else if(odd==2){
        rep(i,3){
            if(!is_even[i])a[i]++;
        }
        spell++;
    }
    //rep(i,3)cout << a[i];
    //cout << spell;
    int max=0;
    rep(i,3){
        if(a[i]>max)max=a[i];
    }
    rep(i,3){
        spell += (max-a[i])/2;
    }
    cout << spell << endl;

    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
  int k, s;
  int x, y, z;
  int count = 0;
  
  scanf("%d%d", &k, &s);
  
  for (x = 0; x <= k; x++) {
    for (y = 0; y <= k; y++) {
        z = s - x - y;
        if (z <= k && z >= 0)
          count++;
    }
  }
  printf("%d", count);
  return 0;
}

Measured memory_bytes: 128
#include <stdio.h>

int main()
{
  int s;
  scanf("%d",&s);
  int i;
  int a[1000000];
  int j;
  int f;
  f = 0;
  i = 0;
  a[i] = s;
  while (i < 1000000)
  {
    j = 0;
    f = 0;
    while (j < i)
    {
      if (a[i] == a[j] && f == 0)
      {
        f++;
      }
      else if (a[i] == a[j] && f == 1)
      {
        printf("%d",j + 1);
        return (0);
      }
      j++;
    }
    if (a[i] % 2 == 0)
    {
      a[i + 1] = a[i] / 2;
    }
    else if (a[i] % 2 != 0)
    {
      a[i + 1] = 3 * a[i] + 1;
    }
    
    i++;
  }
}

Measured memory_bytes: 2176
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
input = sys.stdin.readline

# bisect_left(lists, 3)
# bisect_right(lists, 3)

def main():
	A = int(input())
	B = int(input())
	C = int(input())
	D = int(input())
	E = int(input())
	k = int(input())

	if E - A > k:
		print(":(")
	else :
		print("Yay!")


	
if __name__ == "__main__":
	main()
	

Measured memory_bytes: 3060
#####segfunc######
def segfunc(x,y):
  return x|y 

def init(init_val):
  #set_val
  for i in range(n):
    seg[i+num-1]=init_val[i]  
  #built
  for i in range(num-2,-1,-1) :
    seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
  
def update(k,x):
  k += num-1
  seg[k] = x
  while k:
    k = (k-1)//2
    seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
  
def query(p,q):
  if q<=p:
    return ide_ele
  p += num-1
  q += num-2
  res=ide_ele
  while q-p>1:
    if p&1 == 0:
      res = segfunc(res,seg[p])
    if q&1 == 1:
      res = segfunc(res,seg[q])
      q -= 1
    p = p//2
    q = (q-1)//2
  if p == q:
    res = segfunc(res,seg[p])
  else:
    res = segfunc(segfunc(res,seg[p]),seg[q])
  return res

#####単位元######
ide_ele = set([])

#num:n以上の最小の2のべき乗
n = int(input())
S = [set([s]) for s in input()]
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num

init(S)
q = int(input())
for _ in range(q):
  a,b,c = input().split()
  if a == '1':
    b = int(b)-1
    update(b, set([c]))
  elif a == '2':
    b = int(b)-1
    c = int(c)
    print(len(query(b,c)))

Measured memory_bytes: 376364
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
#define rg register
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
const int Maxn = 1e5 + 5;
const double Pi = acos(-1);
typedef int arr[Maxn];
//ll Add(ll x, ll y) { return (x += y) >= Mod ? x - Mod : (x < 0 ? x + Mod : x); }
ll min(ll x, ll y) { return x < y ? x : y; }
ll max(ll x, ll y) { return x > y ? x : y; }
inline ll read()
{
	rg ll res = 0, sign = 1;
	rg char ch = getchar();
	while (!isdigit(ch))
	{
		if (ch == '-')
			sign = -1;
		ch = getchar();
	}
	do
	{
		res = (res << 3) + (res << 1) + (ch ^ 48);
		ch = getchar();
	} while (isdigit(ch));
	return res * sign;
}
int A, B, H, M;
double x, y;
int main()
{
	cin >> A >> B >> H >> M;
	x = 2 * Pi * (H / 12.0 + M / 720.0);
	y = 2 * Pi * M / 60;
	printf("%.9f\n", sqrt(A * A + B * B - 2 * A * B * cos(x - y)));
	return 0;
}
//C

Measured memory_bytes: 4152
#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

using ll = long long;
using P = pair<int, int>;

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 = 1001001001;
const long long INFLL = LLONG_MAX / 3;
const double eps = (1e-9);

int dfs(vector<vector<int>>& to, vector<bool> visited, int s = 0) {
	visited[s] = true;
	if (!EXIST(visited, false)) return 1;
	int ret = 0;
	for (auto& v : to[s]) {
		if (visited[v]) continue;
		ret += dfs(to, visited, v);
	}
	return ret;
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> to(n, vector<int>());
	rep(i, m) {
		int a, b; cin >> a >> b; a--; b--;
		to[a].push_back(b);
		to[b].push_back(a);
	}

	vector<bool> visited(n, false);
	int ans = dfs(to, visited);
	cout << ans << endl;

	return 0;
}


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

using namespace std;

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

const int maxN = 2e5 + 10;

int n, k, c;
string s;

int lwork[maxN], rwork[maxN];

int main()
{
	cin>>n>>k>>c>>s;
	rep(i, n)
	{
		int add = (s[i] == 'o' ? 1 : 0);
		if(i == 0) lwork[i] = add;
		else if(i < c + 1) lwork[i] = max(add, lwork[i - 1]);
		else lwork[i] = max(lwork[i - 1], lwork[i - c - 1] + add); 
	}
	rep(_i, n)
	{
		int i = n - 1 - _i;
		int add = (s[i] == 'o' ? 1 : 0);
		if(_i == 0) rwork[i] = add;
		else if(_i < c + 1) rwork[i] = max(add, rwork[i + 1]);
		else rwork[i] = max(rwork[i + 1], rwork[i + c + 1] + add);
	}
	rep(i, n)
	{
		int all = 0;
		if(i > 0) all += lwork[i - 1];
		if(i < n - 1) all += rwork[i + 1];
		if(all < k) cout<<i + 1<<endl;
	}
	return 0;
}

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

#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);

#define FIO                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)

#define PI 3.14159265358979323846264338327950L

int main()
{
    //FILE;
    FIO;
    double a, b, h, m, i, rad;
    cin >> a >> b;
    cin >> h >> m;
    rad = PI * 2 * ((double)h / 12.0 + ((double)m / 60.0) / 12.0 - (double)m / 60.0);

    long double rsq = (long double)(a * a + b * b) - (long double)(2 * a * b) * cosl(rad);
    printf("%20.20Lf\n", sqrtl(rsq));
    //system("pause");
    return 0;
}

Measured memory_bytes: 4184
fn main() {
    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")
        };
    }
    
    input!{
        a:usize,
        b:usize
    }

    let mut ans = "Odd";
    if a%2==0 || b%2==0{
        ans = "Even";
    }

    println!("{}",ans);
}




Measured memory_bytes: 4352
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; }    //最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }     //最小公倍数
using Graph = vector<vector<int>>;
ll inf=300000000000000000;
const double PI = 3.14159265358979323846;
int main(){
  int n;
  cin >> n;
  int a[n];
  rep(i,n){
    int b;
    cin >> b;
    b--;
    a[b]=i;
  }
  //rep(i,n)a[i]=i;
  set<int> s;
  s.insert(n);
  set<int> t;
  t.insert(1);
  ll ans=0;
  rep(i,n){
    if(i%1000==0){
      //cout << clock() << endl;
    }
    int b=a[i];
    ans+=(ll)(i+1)*(ll)(*s.lower_bound(b)-b)*(ll)(b-(*t.lower_bound(-1*b))*-1);
    //cout << ans << " " << (*lower_bound(s.begin(),s.end(),b)-b) << " " <<  (*lower_bound(t.begin(),t.end(),-1*b)-b) << " " << b << endl;
    s.insert(b);
    t.insert(-1*b);
  }
  cout << ans << endl;
}


Measured memory_bytes: 23052
package main

import (
	"fmt"
)

func main() {
	var a, b, k int
	fmt.Scanf("%d %d %d", &a, &b, &k)
	if a <= k {
		k -= a
		a = 0

		if b <= k {
			k -= b
			b = 0
		} else {
			b -= k
		}
	} else {
		a -= k
	}

  fmt.Printf("%d %d", a, b)
}

Measured memory_bytes: 512
#include<stdio.h>

int main(void){
    int i,j;
    char w[20],t[2000];
    char end[20] = "END_OF_TEXT";

    scanf("%s",w);

    j = 0;
    while(1){
        scanf("%s",t);
        for(i = 0;i < 12;i++)
            if(t[i] != end[i])
                break;
        if(i == 12)
            break;

        i = -1;
        while(t[++i] !='\0')
            if(t[i] >= 'A' && t[i] <= 'Z')
                t[i] += 0x20;

        i = 0;
        while(1){
            if(t[i] != w[i])
                break;
            if(t[i] == '\0'){j++;break;}
            i++;
        }
    }
    printf("%d\n",j);
    return 0;
}

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

int N, L;
const int MAX_N = 100;
string S[MAX_N];

int main() {
    cin >> N >> L;
    for (int i = 0; i < N; i++) cin >> S[i];

    sort(S, S+N);
    
    for (int i = 0; i < N; i++) {
        cout << S[i];
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 256
// 標準入力から一行を読み取り、指定の型に変換する
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 main() {
    let n: usize = read();
    let mut v: Vec<u64> = read_vec();
    v.sort();
    let mut value = v[0] as f64;

    for i in 1..n
    {
        value =  (v[i] as f64 + value) / 2.0;
    }

    println!("{:?}", value);
}


Measured memory_bytes: 4352
import java.io.*;

public class Main {
    public static void main(String[] args) {
        int h;
        int w;
        int n;

        try (InputScanner is = new InputScanner(System.in)) {
            h = is.ni();
            w = is.ni();
            n = is.ni();
        }
        if (h > w) {
            System.out.println((n + (h - 1)) / h);
        } else {
            System.out.println((n + (w - 1)) / w);
        }

    }

    //-------------------------------------------------------------------
    public static class InputScanner implements Closeable {
        private final BufferedReader br;
        private String[] element;
        private int elementIndex;

        public InputScanner(InputStream is) {
            this.br = new BufferedReader(new InputStreamReader(is));
        }

        public String next() {
            return this.nextElement();
        }

        //next int
        public int ni() {
            return Integer.parseInt(this.nextElement());
        }

        //next long
        public long li() {
            return Long.parseLong(this.nextElement());
        }

        //next int array
        public int[] nia(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = this.ni();
            }
            return arr;
        }

        private void readLine() {
            try {
                this.element = this.br.readLine().split("\\s");
            } catch (IOException e) {
                e.printStackTrace();
            }
            this.elementIndex = 0;
        }

        private String nextElement() {
            if (!hasElement()) {
                this.readLine();
            }
            return this.element[this.elementIndex++];
        }

        private boolean hasElement() {
            return this.element != null && this.element.length > this.elementIndex;
        }

        @Override
        public void close() {
            if (this.br != null) {
                try {
                    this.br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
//-------------------------------------------------------------------
}


Measured memory_bytes: 21204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AtCoder
{
    class ABC177A
    {
        static void Main(string[] args)
        {
            string[] str1 = Console.ReadLine().Split(' ');
            int d = int.Parse(str1[0]);
            int t = int.Parse(str1[1]);
            int s = int.Parse(str1[2]);
            string ans = "No";
            if(s*t >= d)
            {
                ans = "Yes";
            }
            Console.WriteLine(ans);
        }
    }
}

Measured memory_bytes: 27524
#include <string.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <istream>
#define INF 2147483647
#define LLINF 9223372036854775807
typedef long long ll;
using namespace std;
int main(){
	string s;
	cin>>s;
	vector<int> v;
	for(int i=0;i<s.size();i++){
		v.push_back((int)(s[i]-'0'));
	}
	ll cnt=0;
	int last=0;
	for(int i=v.size()-1;i>=0;i--){
		v[i]+=last;
		last=v[i]/10;
		v[i]%=10;
		int sum=0;
		for(int j=i-1;j>=0;j--){
			if(v[j]<5)break;
			else sum++;
			if(sum>=6-v[i])break;
		}
		if(v[i]<5||(sum<6-v[i])||(v[i]==5&&i==0))cnt+=v[i];
		else cnt+=10-v[i],last++;
	}
	cnt+=last;
	cout<<cnt<<endl;
	return 0;
}

Measured memory_bytes: 7932
// -*- coding:utf-8-unix -*-

use proconio::input;

fn main() {
    input! {
        _: usize,
        k: usize,
        s: String,
    }
    let k = k - 1;
    let mut ans = String::default();
    for (i, c) in s.chars().enumerate() {
        if i==k {
            ans.push(c.to_lowercase().nth(0).unwrap());
        }
        else {
            ans.push(c);
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 2200
import sys

readline = sys.stdin.readline
N, M = map(int, readline().split())
x = set()
y = set()
for _ in range(M):
    a, b = map(int, readline().split())
    if a == 1:
        x.add(b)
    if b == N:
        y.add(a)
print('POSSIBLE' if x & y else 'IMPOSSIBLE')


Measured memory_bytes: 19024
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main(){
  int K, N;
  cin >> K >> N;
  vector<int> house(N);
  vector<int> len(N);
  int max_len = 0;
  int max_itr = 0;
  rep(i, N){
    cin >> house.at(i);
    if(i > 0){
      len.at(i - 1) = house.at(i) - house.at(i - 1);
      if(max_len < len.at(i - 1)){
      	max_len = len.at(i - 1);
      	max_itr = i - 1;
      }
    }
    if(i == N - 1){
      len.at(i) = K - house.at(i) + house.at(0);
      if(max_len < len.at(i)){
      max_len = len.at(i);
      max_itr = i;
      }
    }
  }
  
  int ans = 0;
  rep(i, N){
    if(i != max_itr)
      ans += len.at(i);
  }
  cout << ans << endl;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> h(n);
  for(int i = 0;i < n;i++){
    cin >> h.at(i);
  }
  int ans = 1;
  int mxH = h.at(0);
  for(int i = 1;i < n;i++){
    if(h.at(i) >= mxH){
      ans++;
      mxH = h.at(i);
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256