akhauriyash/Code-Regression huggingface dataset:
import java.io.PrintWriter;
import java.util.*;

public class Main {

    class Pair {
        int c, p;

        public Pair(int c, int p) {
            this.c = c;
            this.p = p;
        }

        @Override
        public String toString() {
            return this.c + ", " + this.p;
        }
    }

    void run() {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = sc.nextInt();
        int m = sc.nextInt();
        List<Integer>[] list = new List[n];
        for (int i = 0; i < n; i++) {
            list[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            list[a].add(b);
            list[b].add(a);
        }

        int[] visited = new int[n];
        Arrays.fill(visited, -1);

        Deque<Pair> q = new ArrayDeque<>();
        q.add(new Pair(0, 0));

        while (!q.isEmpty()) {
            Pair p = q.remove();
            int c = p.c;
            if (visited[c] != -1) {
                continue;
            }

            int parent = p.p;
            visited[c] = parent;
            List<Integer> nexts = list[c];
            for (int j : nexts) {
                if (visited[j] != -1) {
                    continue;
                }
//                debug(j, c, parent);
                q.add(new Pair(j, c));
            }
        }

        for (int i = 0; i < n; i++) {
            if (visited[i] == -1) {
                System.out.println("No");
                return;
            }
        }
        System.out.println("Yes");
        for (int i = 1; i < n; i++) {
            System.out.println(visited[i] + 1);
        }


        // out.println("Hello World");
        out.flush();
    }

    void debug(Object... os) {
        System.err.println(Arrays.deepToString(os));
    }

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


Measured memory_bytes: 84132
#include <stdio.h>

int main(){
  int j=0, flag=0, A[100];
  char fx;
  while(1){
    scanf("%c",&fx);
    if(fx == '\n') break;
    if(fx >= '0' && fx <= '9'){
      if(flag)
    A[j] = A[j] * 10 + (fx-48);
      else A[j] = fx - 48;
      flag = 1;
    }
    else if(fx == '+'){
      A[j-2] += A[j-1];
      flag = 0;
      j--;
    }
    else if(fx == '-'){
      A[j-2] -= A[j-1];
      flag = 0;
      j--;
    }
    else if(fx == '*'){
      A[j-2] *= A[j-1];
      flag = 0;
      j--;
    }
    else if(fx == ' ' && flag){
      j++;
      flag = 0;
    }
    else continue;
  }
    printf("%d\n",A[0]);
    return 0;
}

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

#define ll long long
#define PB push_back
#define PII pair<int, int>
#define MP make_pair
#define all(x) x.begin(), x.end()
#define REP(x, y, z) for(int x = y; x <= z; x++)
#define REPP(x, y, z) for(int x = y; x >= z; x--)
#define F first
#define S second
#define MSET(x, y) memset(x, y, sizeof(x)) 
#define EB emplace_back
#define maxn

//structure

//declaration

//functions

int main(void)
{
	#ifdef DBG
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
	#endif

	int a, b, c;

	scanf("%d %d %d", &a, &b, &c);

	if(a+b+c >= 22)
		printf("bust\n");
	else
		printf("win\n");

	return 0;
}

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

typedef long long ll;
typedef double d;

#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (abs(a*b)/gcd(a,b))
#define sqr(a) (a*a)

#define tt int t; cin>>t; while(t--)
#define lo(i, a, b) for(int i=a; i<=b; i++)
#define rlo(i, a) for(int i=a; i>0; i--)
#define be(a) a.begin(),a.end()
#define sz(x) x.size();
#define nl cout<<"\n";
#define sp cout<<" ";
#define Y cout<<"YES"<<endl;
#define N cout<<"NO"<<endl;
#define nlng long long n; cin>>n;
#define nint int n; cin>>n;
#define br break;
#define cn continue;
#define vec vector < int >
#define vecll vector < long long >
#define op() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

int main()
{
    op();
    int x,y,l,k;
    map < int ,int > m;
    m[1]=m[3]=m[5]=m[7]=m[8]=m[10]=m[12]=1;
    m[4]=m[6]=m[9]=m[11]=2;
    m[2]=3;
 cin>>x>>y;
 if(m[x]==1 && m[y]==1 || m[x]==2 && m[y]==2  || m[x]==3 && m[y]==3)
    cout<<"Yes";
 else
    cout<<"No";




    return 0;
}



Measured memory_bytes: 3600
#include <bits/stdc++.h>
using namespace std;
#define mod ((long long)(1e9 + 7))

int main(void) {
  long long N, K;
  cin >> N >> K;
  long long ans = 0;
  for (int i = K; i <= N + 1; i++) {
    ans += (i * (N - i + 1)) + 1;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3632
#include <iostream>
#include <string>
using namespace std;
 
int main() {
	int n,k;
	string s;
 
	cin >> n >> k >> s;
	k--;
 
	s[k] = s[k] - 'A' +'a';
	cout << s << endl;
 
	
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <cassert>
#include <unordered_map>

#define _(x) { cout << #x << " = " << x << " "; }

const double E = 1e-8;
const double PI = acos(-1);

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, ans = 0, p = 0;
    cin >> n;
    for (int i = 0, a; i < n; ++i) {
        cin >> a;
        if (a % 2 == 0) p++;
        if (a % 2 == 0 && ( a % 3 == 0 || a % 5 == 0)) ans++;
    }

    cout << (ans == p ? "APPROVED" : "DENIED") << '\n';
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}

long long dp[100010][3];
long long a[100010][3];

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

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cin >> a[i][j];
            if (i == 0)
            {
                dp[i][j] = a[i][j];
            }
        }
    }

    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 3; k++)
            {
                if (j == k)
                    continue;
                chmax(dp[i][k], dp[i - 1][j] + a[i][k]);
            }
        }
    }

    long long res = 0;
    for (int j = 0; j < 3; ++j)
        chmax(res, dp[n - 1][j]);
    cout << res << endl;

    return 0;
}

Measured memory_bytes: 4992
#![allow(unused_imports)]

use text_io::*;
use proconio::*;
use std::collections::*;
use std::process::exit;
use std::cmp::*;
use itertools::Itertools;


fn main() {
    input! {
    a:usize,
    b:[(isize,isize,isize);a],
    }
    let mut c=0;
    let mut d=0;
    let mut e=0;
    for i in 0..a {
        if (b[i].0-e).abs()<((b[i].1-c).abs()+(b[i].2-d).abs()).abs() || ((b[i].1-c).abs()+(b[i].2-d).abs())%2!=(b[i].0-e).abs()%2 {println!("No");exit(0) }
        c=b[i].1;
        d=b[i].2;
        e=b[i].0;
    }
    println!("Yes")
}

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

int pop(void);
void push(int);

int top=0, Arr[1000];

int main()
{
  int a, b;
  char c[100];

  while( scanf("%s", c) != EOF ){
    if( c[0] == '+' ){
      a = pop();
      b = pop();
      push(a + b);
    }
    else if( c[0] == '-' ){
      b = pop();
      a = pop();
      push(a - b);
    }
    else if( c[0] =='*' ){
      a = pop();
      b = pop();
      push(a * b);
    }
    else push(atoi(c));
  }

  printf("%d\n", pop());

  return 0;
}

int pop()
{
  top--;
  return Arr[top+1];
}

void push(int num)
{
  Arr[++top] = num;
}

Measured memory_bytes: 548
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=1005;
const int MOD=1e9+7;

int n, m;
char a[N][N];
int cache[N][N];

int dp(int i, int j)
{
	if(i>n || j>m)
		return 0;
	if(a[i][j]=='#')
		return 0;
	if(i==n && j==m)
		return 1;
	int &ans=cache[i][j];
	if(ans!=-1)
		return ans;
	ans=dp(i+1, j) + dp(i, j+1);
	ans%=MOD;
	return ans;
}

int32_t main()
{
	IOS;
	memset(cache, -1, sizeof(cache));
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>a[i][j];	
	int ans=dp(1, 1);
	cout<<ans;
	return 0;
}

Measured memory_bytes: 9216
#include<bits/stdc++.h>
using namespace std;
long long dp[10005][101][2],mod=1000000007;
string k;
long long solve(int i,int n ,int su,bool tight,int d){
    if(i==n){
        return su==0;
    }
    if(dp[i][su][tight]!=-1)
        return dp[i][su][tight];
    int mx=(tight?k[i]-'0':9);
    long long ans=0;
    for(int j=0;j<=mx;j++){
        ans=(ans%mod+solve(i+1,n,(su+j)%d,tight&(j==mx),d)%mod)%mod;
    }
    dp[i][su][tight]=ans;
    return ans;
}

int main(){

long long d;
cin>>k;
cin>>d;

memset(dp,-1,sizeof(dp));
cout<<(solve(0,k.size(),0,1,d)-1+mod)%mod<<"\n";


}


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

using namespace std;

//#define int long long
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)
#define fi first
#define se second

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;
typedef pair<P, P> PP;

const int MOD = 1e9 + 7;
const int dy[]={0, 0, 1, -1};
const int dx[]={1, -1, 0, 0};

template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

int a[200000];
int cnt[200001];
map<int, int> mp;

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

    int N, K;
    cin >> N >> K;
    int c = 0;
    rep(i, N) {
        cin >> a[i];
        if(!cnt[a[i]]) ++c;
        ++cnt[a[i]];
    }
    sort(cnt, cnt + N + 1);
    int res = 0;
    FOR(i, 1, N + 1) {
       if(!cnt[i]) continue;
       if(c <= K) break;
       res += cnt[i];
       --c;
    }
    cout << res << endl;
    return 0;
}


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

void fail(){
	cout<<"No"<<endl;
	exit(0);
}
int a[100001];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m=0,s=0;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i],m=max(m,a[i]);
	for(int i=1;i<=n;i++)
		if(a[i]<m-1)
			fail();
		else if(m-1==a[i])	
			s++;
	if(s==0){
		if(m==n-1||m<=n/2)	cout<<"Yes"<<endl;
		else fail();
		return 0;
	}
	if(s>=m)	fail();
	if((m-s)*2>n-s)
		fail();
	else
		cout<<"Yes"<<endl;
}



Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin>>N;
  for(int i=1;i<=9;i++){
    for(int j=1;j<=9;j++){
      if(N==i*j){
        cout<<"Yes"<<endl;
        return 0;
      }
    }
  }
  cout<<"No"<<endl;
}

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


int height[7][7];
int field[7][7][7];
int n, m;
bool IsEnd(bool color) {
  static const int dx[] = { 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
  static const int dy[] = { 0, 1, 0, 1,-1, 0, 0, 1, 1, 1, 1,-1,-1};
  static const int dz[] = { 0, 0, 1, 0, 0, 1,-1, 1,-1, 1,-1, 1,-1};
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      for(int k = 0; k < n; k++) {
        for(int l = 0; l < 13; l++) {
          int x = i, y = j, z = k;
          int cnt = 0;
          while(0 <= x && x < n && 0 <= y && y < n && 0 <= z && z < n && field[y][x][z] == color) {
            x += dx[l];
            y += dy[l];
            z += dz[l];
            ++cnt;
          }
          if(cnt >= m) return(true);
        }
      }
    }
  }
  return(false);
}

int main() {
  int p;
  int x[7 * 7 * 7], y[7 * 7 * 7];

  while(cin >> n >> m >> p, n){ 
    fill_n(*height, 7 * 7, 0);
    fill_n(**field, 7 * 7 * 7, -1);
    for(int i = 0; i < p; i++) {
      cin >> x[i] >> y[i];
      --x[i], --y[i];
    }
    bool color = false;
    bool printed = false;
    for(int i = 0; i < p; i++, color = !color) {
      field[y[i]][x[i]][height[y[i]][x[i]]] = color;
      ++height[y[i]][x[i]];
      if(IsEnd(color)) {
        printed = true;
        cout << (color ? "White " : "Black ") << i + 1 << endl;
        break;
      }
    }
    if(!printed) cout << "Draw" << endl;
  }
}

Measured memory_bytes: 1160
#include <vector>
#include <map>
#include <set>
#include <cstdio>
using namespace std;
map<int,vector<int>>H;
void preorder(int node){
	printf(" %d",node);
	for(auto &e:H[node])if(e!=-1)preorder(e);
}
void inorder(int node){
	auto &children=H[node];
	if(children.size()>=1&&children[0]!=-1)inorder(children[0]);
	printf(" %d",node);
	if(children.size()>=2&&children[1]!=-1)inorder(children[1]);
}
void postorder(int node){
	for(auto &e:H[node])if(e!=-1)postorder(e);
	printf(" %d",node);
}
int main(){
	int N,a,b,c;
	scanf("%d",&N);
	set<int>roots;
	for(int i=0;i<N;i++)roots.insert(i);
	for(int i=0;i<N;i++){
		scanf("%d%d%d",&a,&b,&c);
		H[a].push_back(b),roots.erase(b);
		H[a].push_back(c),roots.erase(c);
	}
	int root=*roots.begin();
	puts("Preorder");
	preorder(root);puts("");
	puts("Inorder");
	inorder(root);puts("");
	puts("Postorder");
	postorder(root);puts("");
}

Measured memory_bytes: 2832
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <math.h>
#include <complex>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> p;
typedef complex<double> com;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const ll INF = 1e18;


int main() {
	int n, m; cin >> n >> m;
	vector<p> a(n);
	ll maxx = 0;
	rep(i, 0, n) {
		cin >> a[i].second >> a[i].first;
		maxx = max(maxx, a[i].second);
	}
	sort(a.begin(), a.end(), greater<p>());
	ll i=0,ans = 0;
	while (a[i].first >= maxx) {
		m -= a[i].first;
		ans++;
		if (m <= 0) break;
		i++;
		if (i == n) break;
	}
	if (m>0) ans += (m + maxx - 1) / maxx;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1792
N = int(input())
A = [int(_) for _ in input().split()]

s = 0
for i in range(N):
    tmp = A[i]
    A[i] += s
    s += tmp

ans = None
for i in range(0, N - 1):
    a = A[-1] - A[i]
    b = A[i]
    if ans is None:
        ans = abs(a-b)
    else:
        ans = min(ans,abs(a-b))
print(ans)


Measured memory_bytes: 24832
#include <stdio.h>

int main(){
  int a,b,i;
  while(1){
    scanf("%d",&a);
    if(a==0) break;
    for(i=0;i<9;i++){
      scanf("%d",&b);
      a-=b;
    }
    printf("%d\n",a);
  }
  return 0;
}

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

int main() {
	// your code goes here
	long long n,m,a[100005],i,sum[100005],sub[100005],j,b,c,size,ans;
	vector<pair<int,int>> s;
	cin >> n >> m;
	for(i=0;i<n;i++) {
		cin >> a[i];
	}
	sort(a,a+n);
	for(i=0;i<m;i++){
		cin >> b >> c;
		s.push_back(make_pair(c,b));
	}
	sort(s.begin(),s.end());
	for(i=m-1;i>=0;i--){
		for(j=1;j<=s[i].second;j++){
			size++;
			if(size>n) break;
			if(size==1) sub[size]=s[i].first;
			else sub[size]=sub[size-1]+s[i].first;
		}
		if(size>n) break;
	}
	sum[n-1]=a[n-1];
	for(i=n-2;i>=0;i--) sum[i]=sum[i+1]+a[i];
	ans=sum[0];
	for(i=1;i<=size;i++){
		ans=max(ans,sum[i]+sub[i]);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3704
#include<bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;

string S,T;
int cnt;

int main(){
	cin>>S>>T;
	for(int i=0;i<3;++i){
		cnt+=(S[i]==T[i]);
	}
	cout<<cnt<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}
ll gcd(ll a, ll b){if(b==0)return a;return gcd(b,a%b);}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}




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

  string str; cin >> str;

  
  rep(i,str.size()){
    if(i%2 == 0) cout << str[i] ;
  }
  cout << endl;
  //cout << fixed << setprecision(16) << ans << endl;
  
  return 0;
}


Measured memory_bytes: 592
from sys import stdin

n,a,b = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()
count = 0
kaigai = 0
for i in s:
    if i == "a":
        if count < a+b:
            print("Yes")
            count += 1
        else:
            print("No")
    elif i == "b":
        if count < a+b and kaigai < b:
            print("Yes")
            count += 1
            kaigai += 1
        else:
            print("No")
    else:
        print("No")

Measured memory_bytes: 4040
from math import sqrt
n,d = map(int,input().split())

x = []
a = 0
ans = 0

for i in range(n):
    x.append(list(map(int,input().split())))

for i in range(n-1):
    for j in range(i+1,n):
        for k in range(d):
            a += (x[i][k] - x[j][k])**2
        else:
            if sqrt(a).is_integer() == True:
                ans += 1
            a = 0

print(ans)

Measured memory_bytes: 3064
#include<stdio.h>
int main()
{
	int h,H;
	int k,K;
	int a=0,b=0,c=0,d=0;
	int e=0,f=0,g=0,n=0;
	int i,j;
		scanf("%d%d",&h,&H);
			scanf("%d%d",&k,&K);
				scanf("%d%d%d%d",&a,&b,&c,&d);
	e=h/10*c;
		f=H/20*d;
			g=k/10*c;
				n=K/20*d;
					i=a*h+b*H+e+f;
						j=a*k+b*K+g+n;
	if(i>j){
		printf("hiroshi\n");
	}
		else if(i<j){
			printf("kenjiro\n");
		}
			else if(i==j){
				printf("even\n");
			}
	return 0;
}


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

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        --a[i];
    }
    for(int i = 0; i < n; ++i){
        cin >> b[i];
    }
    for(int i = 0; i < n; ++i){
        cin >> c[i];
    }
    
    int res = 0;
    int now;
    for(int i = 0; i < n; ++i){
        res += b[a[i]];
        if(i == 0){
            now = a[i];
        }
        else{
            if(a[i] == now + 1){
                res += c[now];
            }
            now = a[i];
        }
    }
    
    cout << res << endl;
}

Measured memory_bytes: 256
print('1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51'.split(', ')[int(input())-1])

Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n,k;
  string s;
  cin>>n>>k>>s;
  char tmp=s.at(0);
  vector<int> g;
  int count=0;
  if(tmp=='1') g.push_back(0);
  for(int i=0;i<n;i++){
    if(s.at(i)==tmp) count++;
    else{
      g.push_back(count);
      count=1;
      tmp=s.at(i);
    }
  }
  g.push_back(count);//(連続する0の数、1の数、0の数...という列ができた)
  int summax,sumtmp;
  for(int i=0; i<2*k&&i<g.size(); i++){
      summax +=g.at(i);//まず左端の0から始めたときの和をとる
  }
  sumtmp=summax;
  for(int i=1; i<g.size(); i+=2){
    //g.at(i)から始めた場合の和をとって比較
    for(int j=i-1; j>i-3&&j>=0;j--){//手前二つ分の数を引く
      sumtmp -=g.at(j);
    }
    for(int j=i+2*k-1; j<i+2*k+1&&j<g.size(); j++){
      sumtmp +=g.at(j);
    }
   
    summax=max(summax,sumtmp);
  }
  cout<<summax<<endl;
}

Measured memory_bytes: 1020
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int longest = 0, sum = 0;
        for (int i = 0; i < n; i++) {
            int temp = sc.nextInt();
            longest = Math.max(longest, temp);
            sum += temp;
        }
        if (sum-longest > longest) System.out.println("Yes");
        else System.out.println("No");
    }
}

Measured memory_bytes: 22740
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1000000007;
using Graph = vector<vector<int>>;

signed main() {
  int N;
  cin >> N;
  
  Graph G(N);
  for( int i = 0; i < N-1; i++ ){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    G.at(A).push_back(B);
    G.at(B).push_back(A);
  }
  
  //フェネック
  vector<int> Fdist(N, -1);
  queue<int> que;
  
  Fdist.at(0) = 0;
  que.push(0);
  
  while( !que.empty() ){
    int v = que.front();
    que.pop();
    
    for( int nv : G.at(v) ){
      if( Fdist.at(nv) != -1 ) continue;
      Fdist.at(nv) = Fdist.at(v)+1;
      que.push(nv);
    }
  }
  
  //すぬけ
  vector<int> Sdist(N, -1);
  Sdist.at(N-1) = 0;
  que.push(N-1);
  
  while( !que.empty() ){
    int v = que.front();
    que.pop();
    
    for( int nv : G.at(v) ){
      if( Sdist.at(nv) != -1 ) continue;
      Sdist.at(nv) = Sdist.at(v)+1;
      que.push(nv);
    }
  }
  
  int snuke = 0;
  int fenne = 0;
  for( int i = 0; i < N; i++ ){
    if( Sdist.at(i) < Fdist.at(i) ) snuke++;
    else fenne++;
  }
  
  if( fenne > snuke ) cout << "Fennec" << endl;
  else cout << "Snuke" << endl;
}

Measured memory_bytes: 7936
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--)
#define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++)
#define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--)
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll INF = numeric_limits<ll>::max()/4;
const int n_max = 1e5+10;

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;}

int main(){
    ll n,k; cin >> n >> k;
    vector<ll> a(n), b(n);
    rep(i,n)cin >> a[i] >> b[i];
    ll ans = 0;
    ll temp = 0;
    rep(i,n)if((a[i] | k) == k)temp += b[i];
    chmax(ans, temp);
    temp = 0;

    // debug(ans);

    ll kdash, adash;
    rep(i,35){
        kdash = k >> i;
        if((kdash & 1) == 0)continue;
        rep(j,n){
            adash = a[j] >> i;
            if((adash & 1) == 1)continue;
            if((adash | kdash) == kdash)temp += b[j];
        }

        chmax(ans, temp);
        // debug(i);debug(kdash);debug(temp);
        temp = 0;
    }

    cout << ans << endl;
}

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

int main(){	
    while (1) {
        int h, w;
        cin >> h >> w;
        if (h == 0) break;

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if ((i + j) % 2 == 0) {
                    cout << "#";
                }
                else {
                    cout << ".";
                }
            }
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}


Measured memory_bytes: 3072
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstring>

using namespace std;

int main(){
  string n;
  cin>>n;
  if((n[0] == n[1] && n[1] == n[2]) || (n[1] == n[2] && n[2]  == n[3])){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;
  }

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // sort
#include <map> // pair
#include <queue>
#include<iomanip>
#include <cmath>
#include <set>

#define ABS(x) ((x)>0?(x):-(x))
#define MIN(x,y) ((x)>(y)?(y):(x))

using namespace std;
typedef long long ll;

ll newton(long long int a, long long int  b)
{
	if(a==b)
		return a;
	while(a != 0 && b != 0)
	{
		if(a > b)
		{
			b += a;
			a = b-a;
			b -= a;
		}
		b %= a;
	}
	return a;
}

struct UnionFind {
	vector<int> p;
	UnionFind(int N):p(N)
	{
		for(int i=0; i< N; i++)
		{
			p[i] = i;
		}
	}
	int root(int x)
	{
		if(p[x] == x)
			return x;
		return p[x] = root(p[x]);
	}
	void unite(int x, int y)
	{
		int rx = root(x);
		int ry = root(y);
		if(rx == ry) return;
		p[rx] = ry;
	}
	bool issame(int x, int y)
	{
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
};

//大きな数に対する割り算をするとき、MODを指定することで掛け算の値に変換できる

ll div2mul_calc(ll a, ll b, ll m)
{
	if (b == 0) {
        return 1;
    } else if (b % 2 == 0) {
        ll d = div2mul_calc(a, b / 2, m);
        return (d * d) % m;
    } else {
        return (a * div2mul_calc(a, b - 1, m)) % m;
    }
}
ll div2mul(ll n, ll mod)
{
	return div2mul_calc(n, mod-2, mod);
}

ll func(ll W, ll H)
{
	ll ans = 0;
	for(ll i=0; i < W; ++i)
	{
		if(i > 0)
		{
			ans += i*(i+1)/2;
		}
		if (i < W - 1)
		{
			ans += (W-i)*(W-i-1)/2;
		}
	}
	ans /= (ll)2;
	ans *= H;
	ans %= (ll)1000000007;
	ans *= H;
	ans %= (ll)1000000007;
	return ans;
}

int main()
{
	ll N,M,K;
	ll i,j,k,l;
	cin >> N >> M >> K;
	ll ans = func(N,M)+func(M,N);
	ans %= (ll)1000000007;
	ll S = (ll)N*M;
	/*
	for(i=1;i<=K;++i)
	{
		ans*=(S-i);
		ans/=i;
		ans %= 1000000007;
	}
	*/
	ll div = 1;
	ll mul = S-2;
	for(i=1; i <= K-2; ++i)
	{
		ans *= (S-i-1);
		ans %= (unsigned long long)1000000007;
		ans *= div2mul(i,(unsigned long long)1000000007);
		ans %= (unsigned long long)1000000007;
	}

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




































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

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

ll x, a;

int main()
{
   // freopen("test.in","r",stdin);

    scanf("%lld", &x);
    a = sqrt(2 * x);

    while ( (a * (a + 1)) / 2 < x) a++;
    printf("%lld", a);


    return 0;
}


Measured memory_bytes: 256
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    sum = 0
    for i in range(1, N + 2):
        if sum < N:
            sum += i
        else:
            S = i
            break
    a = sum - N
    for j in range(1, S):
        if j == a:
            continue
        else:
            print(j)


if __name__ == "__main__":
    main()


Measured memory_bytes: 3408
N = int(input())
W = [int(x) for x in input().split()]
buf_1 = 0
buf_2 = 0
buf_3 = 1000000000000000

for i in range(N) :
    for j in range(N) :
        if j <= i :
            buf_1 += W[j]
        else :
            buf_2 += W[j]
    buf_3 = min(buf_3,abs(buf_1 - buf_2))
    buf_1 = 0
    buf_2 = 0

print(buf_3)

Measured memory_bytes: 3064
n,a,b = map(int,input().split())
li = list(input())
c=a+b
for stu in li:
  if stu=='a':
    if c==0:
      print("No")
    else:
      c-=1
      print("Yes")
  elif stu=='b':
    if b==0 or c==0:
      print("No")
    else:
      c-=1
      b-=1
      print("Yes")
  else:
    print("No")

Measured memory_bytes: 4712
import java.util.Scanner;

class Main{

    static Scanner s;
    static int temp;

    public static void main(String args[]){
        s = new Scanner(System.in);
        
        int a = s.nextInt();
        int b = s.nextInt();
        
        
        System.out.println(gcd(a,b));
    }

    public static int gcd(int x, int y){
        if(x == y){
            return x;
        }
        else if(x == 0){
            return y;
        }
        else if (y == 0){
            return x;
        }
        else{
            if(x > y){
                return gcd(y,x%y);
            }
            else{
                return gcd(x,y%x);
            }
        }
    }
}


Measured memory_bytes: 26120
fun main(args: Array<String>) {
    val numList = readLine()!!.split(" ").map(String::toInt).toMutableList()

    var tmp1 = numList[0]
    var tmp2 = numList[1]
    numList[0] = tmp2
    numList[1] = tmp1

    //---------

     tmp1 = numList[0]
     tmp2 = numList[2]
    numList[0] = tmp2
    numList[2] = tmp1

    val a = numList[0]
    val b = numList[1]
    val c = numList[2]

    println("$a $b $c")
}

Measured memory_bytes: 37844
#include <iostream>
#include <cmath> 
#include <algorithm>
#include <vector>
#include <functional>
#include <iomanip>
using namespace std;

int n; long long k;
vector<int> a, f;

long long biSearch(long long lt, long long rt);

int main(){
    cin >> n >> k;
    a.resize(n); f.resize(n);
    long long sum = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        sum += a[i];
    }
    if(sum <= k){
        cout << 0 << endl;
        return 0;
    }
    for(int i = 0; i < n; i++) cin >> f[i];
    sort(f.begin(), f.end());
    sort(a.begin(), a.end(), greater<int>());
    
    long long mx = 0;
    for(int i = 0; i < n; i++){
        mx = max(mx, (long long)a[i]*f[i]);
    }

    cout << biSearch(1, mx) << endl;
    return 0;
}

long long biSearch(long long lt, long long rt){
    if(lt == rt) return lt;
    long long ave = (lt + rt) / 2;
    long long difSum = 0;
    for(int i = 0; i < n; i++){
        long long kunrengo = ave / f[i];
        difSum += max(0LL, a[i]-kunrengo);
    }
    if(difSum <= k) return biSearch(lt, ave);
    else return biSearch(ave+1, rt);
    
}

Measured memory_bytes: 1792
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int m, f, r;
    int sum;

     while (true) {
      m = sc.nextInt();
      f = sc.nextInt();
      r = sc.nextInt();
      sum = m + f;

      if (m < 0 && f < 0 && r < 0)
        break;

      if (m < 0 || f < 0) {
        System.out.println("F");
      } else if (sum >= 80) {
        System.out.println("A");
      } else if (sum >= 65 && sum < 80) {
        System.out.println("B");
      } else if (sum >= 50 && sum < 65) {
        System.out.println("C");
      } else if (sum >= 30 && sum < 50) {
        if (r >= 50) {
          System.out.println("C");
        } else {
          System.out.println("D");
        }
      } else if (sum < 30) {
        System.out.println("F");
      }

    }
  }
}

Measured memory_bytes: 26156
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	_, divlist := getDivlistSlow(n)
	ans := 10000000000
	for _, a := range divlist {
		ans = min(ans, f(a, n/a))
	}

	fmt.Println(ans)

}
func f(a int, b int) int {
	return max(keta(a), keta(b))
}

// 約数列挙リストを作成(愚直)
func getDivlistSlow(a int) (cnt int, divlist []int) {
	for i := 1; i*i <= a; i++ {
		if a%i == 0 {
			cnt++
			divlist = append(divlist, i)
			if i*i != a { // 6*6=36 などを重複カウントしない
				cnt++
				divlist = append(divlist, a/i)
			}
		}
	}
	sort.Ints(divlist) // 昇順ソート
	return cnt, divlist
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// 正数の桁数を返す
func keta(n int) int {
	keta := 0
	if n == 0 {
		return 1
	}
	for n > 0 {
		keta++
		n /= 10
	}
	return keta
}

// ---- readfunc
func scanInit() {
	const cap = 200 * 1024 // Default=64
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}


Measured memory_bytes: 1872
#include <bits/stdc++.h>
using namespace std;
int n,m,ans;
int total=0;
int ind=0;
int head[100010];
int num[100010],low[100010];
struct edge
{
    int from;
    int to;
    int next;
}input[100010];
void add_edge(int a,int b)
{
    total++;
    input[total].from=a;
    input[total].to=b;
    input[total].next=head[a];
    head[a]=total;
}
void dfs(int now,int father)
{
    ind++;
    num[now]=ind;
    low[now]=ind;
    for(int e=head[now];e!=0;e=input[e].next)
    {
        if(num[input[e].to]==0)
        {
            dfs(input[e].to,now);
            low[now]=min(low[now],low[input[e].to]);
            if(low[input[e].to]>num[now])++ans;
                
        }
        else if(input[e].to!=father)
            low[now]=min(low[now],num[input[e].to]);
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        cin>>a>>b;
        add_edge(a,b); 
        add_edge(b,a);
    }
	for(int i=1;i<=n;++i)if(!num[i])dfs(i,i);
	cout<<ans;
}

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

int main() {
    string s;
    cin >> s;
    int n = s.size();

    string ans;
    char a, b;
    if (s[0] == s[n - 1]) {
        a = s[0];
        b = s[1];
    } else {
        a = s[0];
        b = s[n - 1];
    }
    
    bool is_a = true;
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (is_a == true and s[i] == a) {
            count++;
            is_a = false;
        } else if (is_a == false and s[i] == b) {
            count++;
            is_a = true;
        }
    }
    if ((n - count) % 2 == 1) {
        cout << "First" << endl;
    } else {
        cout << "Second" << endl;
    }
    
    return 0;
}


Measured memory_bytes: 512
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
  int id;
  struct node *parent;
  struct node *left;
  struct node *right;
}Node;

void print_tree(Node *x,int t);
Node *make_tree(int *pre, int * in, int i, int j, int n, Node *p);

int main()
{
  int i,n,*preorder,*inorder;
  scanf("%d",&n);
  preorder = (int*)malloc(sizeof(int) * n);
  inorder = (int*)malloc(sizeof(int) * n);
  for (i = 0; i < n; i++) {
    scanf("%d",preorder+i);
  }
  for (i = 0; i < n; i++) {
    scanf("%d",inorder+i);
  }
  Node *root = make_tree(preorder,inorder,0,0,n,NULL);
  print_tree(root,0);
  return 0;
}


void print_tree(Node *x,int t) {
  if(x->left != NULL) print_tree(x->left,1);
  if(x->right != NULL) print_tree(x->right,1);
  if(t == 1)printf("%d ", x->id);
  if(t == 0)printf("%d\n", x->id);
}

Node *make_tree(int *pre, int * in, int i, int j, int n, Node *p) {
  if(i >= n) return NULL;
  Node *p1 = (Node*)malloc(sizeof(Node));
  p1->id = pre[i];
  p1->parent = p;
  p1->left = NULL;
  p1->right = NULL;
  if(pre[i] != in[j]) {
    p1->left = make_tree(pre,in,i+1,j,n,p1);
  }else{
    Node *tmp = p1;
    while(1) {
      if(p == NULL) {
	break;
      }
      if(p->id == in[j+1]) {
	j++;
	tmp = p;
      }
      p = p->parent;
    }
    tmp->right = make_tree(pre,in,i+1,j+1,n,tmp);
  }
  return p1;
}


Measured memory_bytes: 2756
using System;

class Swapping_Two_Numbers
{
    public static void Main()
    {
        while (true)
        {
            string number_str = Console.ReadLine();
            string[] tmp_number = number_str.Split(' ');
            int a = int.Parse(tmp_number[0]);
            int b = int.Parse(tmp_number[1]);

            if (a == 0 && b == 0) break;


            if (a > b)
            {
                int tmp = a;
                a = b;
                b = tmp;
            }

            Console.WriteLine("{0} {1}", a, b);
        }
    }
}

Measured memory_bytes: 9324
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;

int main(){
   string s;
   cin >> s;
   int size = s.size();
   int cnt = 0;
   for(int i=0;i<size;i++){
      if(s[i]=='x') cnt++;
   }
   if( cnt >=8 ) cout << "NO"<<endl;
   else cout << "YES"<<endl;

   return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
typedef pair<LL,int> LP;
const int INF=1<<30;


void array_show(int *a,int n){
	for(int i=0;i<n;i++)printf("%d%c",a[i],(i!=n-1?' ':'\n'));
}
void array_show(LL *a,int n){
	for(int i=0;i<n;i++)printf("%lld%c",a[i],(i!=n-1?' ':'\n'));
}

int t[100005][3];
int dp[100005][3];

int main(){
	int n;
	int i,j,k;
	cin>>n;
	for(i=0;i<n;i++)for(j=0;j<3;j++)cin>>t[i][j];
	for(i=0;i<n;i++){
		dp[i+1][0]=max(dp[i][1],dp[i][2])+t[i][0];
		dp[i+1][1]=max(dp[i][0],dp[i][2])+t[i][1];
		dp[i+1][2]=max(dp[i][0],dp[i][1])+t[i][2];
	}
	cout<<max({dp[n][0],dp[n][1],dp[n][2]})<<endl;
	return 0;
}


Measured memory_bytes: 2560
#include <stdio.h>

int yomisute(char str[],int i)
{
	while(str[i] != ' ')
	{
		i++;
	}
	i++;
	return i;
}

int main(void)
{
	int b_num,c_num;
	char a,b,c;
	char str[72];
	
	fgets(str,sizeof(str),stdin);
	a = str[0];
	
	b_num = yomisute(str,0);
	b = str[b_num];
	
	c_num = yomisute(str,b_num);
	c = str[c_num];
	
	printf("%c%c%c",a-32,b-32,c-32);
	
	return 0;
}


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

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

int main()
{
    int n, m; cin >> n >> m;
    vector<int> bi(m), pi(m);
    int k, s, ans = 0;
    rep(i, m){
        cin >> k;
        bi[i] = 0;
        rep(j, k){
            cin >> s;
            bi[i] |= (1 << (s-1));
        }
    }
    rep(i, m){
        cin >> pi[i];
    }

    rep(b, (1 << n)){
        bool isAllOn = true;
        rep(i, m){
            if((bitset<12>((bi[i] & b)).count() % 2) != pi[i])
                isAllOn = false;
        }
        if(isAllOn) ans++;
    }

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


Measured memory_bytes: 3604
N,M = map(int,input().split())
A = [0]*N
for i in range(N):
    A[i] = list(map(int,input().split()))

B = sorted(A,key=lambda x:x[0])
i = 0
price = 0
for a,b in B:
    if i+b < M:
        i += b
        price += a * b
    else:
        price += a * (M-i)
        break
print(price)

Measured memory_bytes: 29792
import Control.Monad
import Data.List
import Data.Monoid
import Data.IORef

unwrap [x] = x

initlist :: Int -> [Integer]
initlist n
    | (n == 0) = [0]
    | otherwise = 0 : (initlist (n - 1))

data Tree a = Leaf !Int !a | Node !Int !Int !a !(Tree a) !(Tree a)

buildtree :: Monoid a => [a] -> Int -> Int -> Tree a
buildtree xs l r 
    | ((l + 1) == r) = Leaf l (unwrap xs)
    | otherwise = Node l r mempty (buildtree ys l m) (buildtree zs m r)
    where
        m = (l + r) `div` 2
        (ys,zs) = Data.List.splitAt (m - l) xs

get :: Monoid a => Tree a -> Int -> Int -> a
get (Leaf i x) a b
    | (a <= i && i < b) = x 
    | otherwise = mempty
get (Node l r x xt yt) a b 
    | (b <= l || r <= a) = mempty
    | (a <= l && r <= b) = x
    | otherwise = get xt a b `mappend` get yt a b

val :: Monoid a => Tree a -> a
val (Leaf i x) = x
val (Node l r x xt yt) = x

update :: Monoid a => Tree a -> Int -> a -> Tree a
update (Leaf i x) j k = (Leaf i (x `mappend` k))
update (Node l r x xt yt) i k = 
    if (i < ((l + r) `div` 2)) then
        let nt = update xt i k
        in Node l r ((val nt) `mappend` (val yt)) nt yt
    else
        let nt = update yt i k
        in Node l r ((val xt) `mappend` (val nt)) xt nt

main = do
    [n,q] <- fmap(map read . words) getLine
    let xs = replicate n (mempty :: Sum Int)
    t <- newIORef (buildtree xs 0 n)
    replicateM_ q $ do
        [com,x,y] <- fmap(map read . words) getLine
        rt <- readIORef t
        if (com == 0) then do
            writeIORef t (update rt (x - 1) (Sum y))
        else do
            print $ getSum (get rt (x - 1) y)       



Measured memory_bytes: 56264
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

int main() {
    ll n, m;
    cin >> n >> m;
    ll a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll okini = 1;
    ll okival = 0;
    int innum[m + 1];
    int outnum[m + 1];
    fill(innum, innum + m + 1, 0);
    fill(outnum, outnum + m + 1, 0);
    ll ans = 0;
    ll defa[m + 1];
    fill(defa, defa + m + 1, 0);
    for (int i = 0; i < n - 1; i++) {
        innum[a[i]]++;
        outnum[a[i + 1]]++;
        if (okini <= a[i + 1] && (a[i] < okini || a[i + 1] < a[i])) {
            okival++;
            ans += a[i + 1] - okini + 1;
        } else {
            if (a[i] < a[i + 1]) {
                // if (okini == a[i]) {
                //     okival++;
                // }
                ans += a[i + 1] - a[i];
            } else {
                ans += m + 1 - a[i] + a[i + 1] - 1;
            }
        }
        if (a[i] < a[i + 1]) {
            defa[a[i + 1]] += a[i + 1] - a[i];
        } else {
            defa[a[i + 1]] += m + 1 - a[i] + a[i + 1] - 1;
        }
    }
    ll tmp = ans;
    //cout << tmp << " " << okival << endl;
    for (int i = 2; i <= m; i++) {
        tmp -= okival;
        tmp += defa[i - 1];
        okival += innum[i - 1];
        okival -= outnum[i - 1];
        if (tmp < ans) {
            ans = tmp;
        }
        //cout << tmp << " " << okival << endl;
    }
    cout << ans << endl;
}


Measured memory_bytes: 2560
//for文を10^8回回して適当な処理をすると1秒くらいだった気がするけど、最近はもっと速いのかなあ。
#include <iostream>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int powmod(int a, int n, int mod) {
	if (n == 0) return 1;
	if (n & 1) return (a * powmod(a, n - 1, mod)) % mod;
	return powmod((a * a) % mod, n / 2, mod);
}

int mod = 1000000007;
int inv[100001];	//inv[n] = 1/1 + 1/2 + … + 1/nのmod 10^9 + 7バージョン
int fact[100001];	//fact[n] = n!を10^9 + 7で割った余り
int n;
int a[100000];

signed main() {
	int i;
	
	cin >> n;
	rep(i, n) cin >> a[i];
	
	rep(i, n) { inv[i + 1] = inv[i] + powmod(i + 1, mod - 2, mod); inv[i + 1] %= mod; }
	fact[0] = 1;
	rep(i, n) { fact[i + 1] = (i + 1) * fact[i]; fact[i + 1] %= mod; }
	
	int ans = 0;
	rep(i, n) {
		int prob = inv[i + 1] + inv[n - i] - inv[1];
		prob = (prob + mod) % mod;
		ans += prob * a[i];
		ans %= mod;
	}
	ans *= fact[n];
	ans %= mod;
	
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 2560
#include<bits/stdc++.h>
using namespace std;
const int64_t INF = 1e16;
int main() {
	
	int n,e;
	cin >> n >> e;
	vector<vector<pair<int,int>>> g(n);
	vector<vector<int64_t>> dp(n,vector<int64_t>(n,INF));
	for(int i=0; i<e; i++){
		int s,t,d;
		cin >> s >> t >> d;
		dp.at(s).at(t) = d;
	}
	for(int i=0; i<n; i++) dp.at(i).at(i) = 0;
	for(int k=0; k<n; k++){
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				dp.at(i).at(j) = min(dp.at(i).at(j), dp.at(i).at(k)+dp.at(k).at(j));
			}
		}
	}
	bool flg=1;
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			if(i==j && dp.at(i).at(j)<0) flg=0;
		}
	}
	if(flg){
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				if(dp.at(i).at(j)<1e10) cout << dp.at(i).at(j) << (j!=n-1?" ":"");
				else cout << "INF" << (j!=n-1?" ":"");
			}
			cout << endl;
		}
	}
	else cout << "NEGATIVE CYCLE" << endl;
}


Measured memory_bytes: 3260
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>

#define REP(i,n) for(i=0;i<n;i++)
#define ll long long

int main(void){
    char s[100];
    int i;
    bool yn=1;

    scanf("%s",&s[0]);
    
    for(i=0;i<strlen(s);i=i+2){
        if(s[i]=='L') yn=0;
    }

    for(i=1;i<strlen(s);i=i+2){
        if(s[i]=='R') yn=0;
    }

    if(yn) printf("Yes");
    else printf("No");
    return 0;
}

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

struct Dice{
    int s[6];
    void move(char c){
        int tmp;
        if(c=='N'){
            tmp=s[0];
            s[0]=s[1];
            s[1]=s[5];
            s[5]=s[4];
            s[4]=tmp;
        }
        else if(c=='S'){
            tmp=s[0];
            s[0]=s[4];
            s[4]=s[5];
            s[5]=s[1];
            s[1]=tmp;
        }
        else if(c=='E'){
            tmp=s[0];
            s[0]=s[3];
            s[3]=s[5];
            s[5]=s[2];
            s[2]=tmp;
        }
        else if(c=='W'){
            tmp=s[0];
            s[0]=s[2];
            s[2]=s[5];
            s[5]=s[3];
            s[3]=tmp;
        }
    }
    int get_top(){
        return s[0];
    }
};

int main(){
    Dice dice;
    for(int i=0;i<6;i++)cin>>dice.s[i];
    string s;
    cin>>s;
    for(int j=0;j<s.size();j++)dice.move(s[j]);
    cout<<dice.get_top()<<endl;

    return 0;
}


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

// definition {{{ 1

// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x,y) scanf("%d%d", &x, &y)
#define Scd3(x,y,z) scanf("%d%d%d", &x, &y, &z)

#define Scll(x) scanf("%llu", &x)
#define Scll2(x,y) scanf("%llu%llu", &x, &y)
#define Scll3(x,y,z) scanf("%llu%llu%llu", &x, &y, &z)

#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2

// constants {{{ 2
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// }}} 2

// systems {{{ 2
#define Rep(x,y) for(int x = 0; x < y; x++)
#define Repe(x,y,z) for(int x = z; x < y; x++)
// }}} 2

// output {{{ 2
#define YESNO(a) (a)?printf("YES\n"):printf("NO\n");
// }}} 2

// }}} 1

int main() {

    int D;
    Scd(D);
    Rep(i,D) printf("%lld\n", (i^2194801)%26+1 );

}



Measured memory_bytes: 3824
#include <iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
	int s, w; cin >> s >> w;
	if (s <= w) {
		cout << "unsafe" << '\n';
	}
	else cout << "safe" << '\n';




}

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

int main() {
    int K;cin >> K;
    string S;cin >> S;
    int siz = S.size();
    K = min(siz,K);
    for (int i = 0;i < K;i++){
        cout << S[i];
    }
    if (siz > K) cout << "..." << endl;
    cout << endl;
}

Measured memory_bytes: 3640
use proconio::{input, fastout};

#[fastout]
fn main() {
    input! { t: usize }

    for _ in 0..t {
        input! { n: u64, m: u64, a: u64, b: u64 }
        println!("{}", floor_sum(n, m, a, b));
    }
}

fn floor_sum(n: u64, m: u64, mut a: u64, mut b: u64) -> u64 {
    let mut r = 0;
    if a >= m {
        r += (n - 1) * n * (a / m) / 2;
        a %= m;
    }
    if b >= m {
        r += n * (b / m);
        b %= m;
    }

    let y = (a * n + b) / m;
    if y > 0 {
        let x = y * m - b;
        r += (n - (x + a - 1) / a) * y;
        r += floor_sum(y, a, m, (a - x % a) % a);
    }
    r
}


Measured memory_bytes: 4772
using System;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            string a;
            a = Console.ReadLine();
            if(a.ToUpper() == a)
            {
                Console.WriteLine("A");
            }
            else
            {
                Console.WriteLine("a");
            }

        }
    }
}


Measured memory_bytes: 26064