akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <stdlib.h>
#define NIL NULL

struct node {
	struct node *r;
	struct node *l;
	struct node *p;
	int key;
};

typedef struct node * Node;

Node r;


Node treeSearch(Node , int );
void treeDelete(Node );
void preorder(Node );
void inorder(Node );
void insert(int );




int main(void) {
  int n, i, a;
  char com[20];
  scanf("%d", &n);

  for (i = 0; i < n; ++i) {
    scanf("%s", com);
    if ( com[0] == 'f' ) {
      Node t = treeSearch(r, a);
      if ( t != NIL ) printf("yes\n");
      else printf("no\n");
    }

    else if ( com[0] == 'i') {
      scanf("%d", &a);
      insert(a);
    }

    else if ( com[0] == 'p') {
      inorder(r);
      printf("\n");
      preorder(r);
      printf("\n");
			
    }

    else if ( com[0] == 'd') {
      scanf("%d", &a);
      treeDelete(treeSearch(r, a));
    }
  }
	
  return 0;
}

Node treeSearch(Node r, int k) {
  if (r->key = k) return r;
  if (r->key < k) {

    if (r->l != NIL) return treeSearch(r->l, k);
    else return NIL;
  }
  else {
    if (r->r != NIL) return treeSearch(r->r, k);
    else return NIL;
  }
}

void insert(int k) {
  Node y = NIL;
  Node x = r;
  Node z;
  
  z = malloc(sizeof(struct node));
  z->key = k;
  z->l = NIL;
  z->r = NIL;
  
  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->l;
    }
    
    else {
      x = x->r;
    }
  }
  
  z->p = y;
  
  if (y == NIL) {
    r = z;
  }
  else if (z->key < y->key) {
    y->l = z;
  }
  else {
    y->r = z;
  }
}

void inorder(Node u) {

  if (u == NIL){
    return;
  }
  inorder(u->l);

  printf(" %d", u->key);

  inorder(u->r);
  return;
}

void preorder(Node u) {
  if (u == NIL){
    return;
  }
  printf(" %d", u->key);
  preorder(u->l);

  preorder(u->r);

  return;
}

void treeDelete(Node z) {
  
  Node y;
  
  Node x;
  
  return;
}



Measured memory_bytes: 25136
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner a = new Scanner(System.in);		
		int b = a.nextInt();
		long sum = 0;
		for(int i = 1; i <= b; i++) {
			if(i % 15 == 0) {
				
			}else if(i % 3 == 0) {
				
			}else if(i % 5 == 0) {
				
			}else {
				sum += i;
			}
		}
		System.out.println(sum);
	}
}

Measured memory_bytes: 35816
/*
======================
[     ___T_          ]
[    | 6=6 | =>HI :-)] 
[    |__o__|         ]
[ >===]__o[===<      ]
[     [o__]          ]
[      .".           ]
[      |_|           ]
[                    ]
======================
 */
 
#include<bits/stdc++.h>
using namespace std;
using lli = int64_t;
int main() {
	 ios::sync_with_stdio(false);
	 cin.tie(nullptr);
	 cout.tie(nullptr);
	 int T = 1;
	 //~ cin >> T;
	 for(int t = 1; t <= T; t++) {
		 int n;
		 cin >> n;
		 int input[n + 1];
		 map<int,int> count;
		 for(int i = 1; i <= n; i++){
			 cin >> input[i];
		 }
		 map<int,int> x,y;
		 for(int i = 1; i <= n; i++){
			 if(i & 1){
				 x[input[i]] += 1;
			 }else{
				 y[input[i]] += 1;
			 }
		 }
		 int max_count = 0,max_number = -1;
		 for(auto i : x){
			 if(max_count < i.second){
				 max_count = i.second;
				 max_number = i.first;
			 }
		 }
		 int max_count2 = 0,max_number2 = -1;
		 for(auto i : y){
			 if(max_count2 < i.second){
				 max_count2 = i.second;
				 max_number2 = i.first;
			 }
		 }
		 if(max_number2 == max_number){
			 int max_number3 = -1,max_count3 = 0;
			 if(max_count2 > max_count){
				 for(auto i : x){
					 if(i.first == max_number2){
						 continue;
					 }
					 if(i.second > max_count3){
						 max_count3 = i.second;
						 max_number3 = i.first;
					 }
				 }
				 if(max_number3 == -1){
					 cout << n - max_count2 << "\n";
				 }else{
					 cout << n - (max_count2 + max_count3) << "\n";
				 }
			 }else if(max_count2 < max_count){
				 for(auto i : y){
					 if(i.first == max_number){
						 continue;
					 }
					 if(i.second > max_count3){
						 max_count3 = i.second;
						 max_number3 = i.first;
					 }
				 }
				 if(max_number3 == -1){
					 cout << n - max_count << "\n";
				 }else{
					 cout << n - (max_count + max_count3) << "\n";
				 }
			 }else{
				 int max_count4 = 0,max_number4 = -1;
				 for(auto i : x){
					 if(i.first == max_number){
						 continue;
					 }
					 if(max_count3 < i.second){
						 max_count3 = i.second;
						 max_number3 = i.first;
					 }
				 }
				 for(auto i : y){
					 if(i.first == max_number){
						 continue;
					 }
					 if(max_count4 < i.second){
						 max_count4 = i.second;
						 max_number4 = i.first;
					 }
				 }
				 if(max_number4 == -1 or max_number3 == -1){
					 if(max_number4 == -1 and max_number3 == -1){
						 cout << n - max_count << "\n";
					 }else if(max_number3 == -1){
						 cout << n - (max_count + max_count4) << "\n";
					 }else{
						 cout << n - (max_count2 + max_count3) << "\n";
					 }
				 }else{
					 cout << n - (max_count + max(max_count3,max_count4)) << "\n";
				 }
			 }
		 }else{
			 cout << n - (max_count + max_count2) << "\n";
		 }
	 }
}


Measured memory_bytes: 8676
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <bitset>
#include <math.h>

typedef long long ll;
typedef unsigned long long ull;

#define vi std::vector<int>
#define vl std::vector<ll>
#define floop(n) for(int i = 0; i < n; ++i)
#define print(x) std::cout << x << std::endl;

const ll INF = 1e18;

int main()
{
    std::string s;
    std::cin >> s;
    int len = s.length(), left=-1, right=-1;

    if(len == 2)
    {
        if(s[0] == s[1])
        {
            print("1 2");
        }
        else
        {
            print("-1 -1");
        }
    }

    else
    {
        for(int i = 0; i < len-2; ++i)
        {
            if(s[i] == s[i+1] || s[i] == s[i+2])
            {
                left = i+1;
                right = i+3;
                break;
            }
        }

        print(left << " " << right);
    }

    return 0;
}

Measured memory_bytes: 3704
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



int main(){

	int N,M;
	scanf("%d %d",&N,&M);

	int sum = 0,tmp;
	for(int i = 0; i < M; i++){

		scanf("%d",&tmp);
		sum += tmp;
	}

	if(sum <= N){

		printf("%d\n",N-sum);
	}else{

		printf("-1\n");
	}

	return 0;
}


Measured memory_bytes: 3820
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,m = map(int,readline().split())
lst1 = list(map(int,readline().split()))
lst2 = [1]*n
for i in range(m):
    a,b = map(int,readline().split())
    a,b = a-1,b-1
    if lst1[a] == lst1[b]:
        lst2[a] = 0
        lst2[b] = 0
    elif lst1[a] > lst1[b]:
        lst2[b] = 0
    else:
        lst2[a] = 0

print(sum(lst2))




Measured memory_bytes: 84264
#include<bits/stdc++.h>
using namespace std;
int N,W,H,S,T;
int fie[555][555];
int rum[555][555];
int main(){
  while( cin >> N && N ){
    cin >>W >> H;
    memset(fie,0,sizeof(fie));
    
    for(int i=0;i<N;i++){
      int x,y;
      cin >>x >> y;
      fie[x][y] = 1;
    }
    cin >> S >> T;
    --S;--T;
    memset(rum,0,sizeof(rum));

    for(int i=1;i<=H;i++){
      for(int j=1;j<=W;j++){
	rum[j][i] = rum[j-1][i] + fie[j][i];
      }
    }

    for(int i=1;i<=H;i++){
      for(int j=1;j<=W;j++){
	rum[j][i] += rum[j][i-1];
      }
    }

    int res = 0;
    for(int i=1;i<=H;i++){
      for(int j=1;j<=W;j++){
	if( i+T > H || j + S > W ) continue;
	int num = rum[j+S][i+T] - rum[j-1][i+T] - rum[j+S][i-1] + rum[j-1][i-1];
	//	printf("%d %d  %d\n",j,i,num);
	res = max( res, num );
      }
    }
    cout << res << endl;
  }
}

Measured memory_bytes: 3572
#122-C
n,q=map(int,input().split())
s=input()
l=[list(map(int,input().split())) for i in range(q)]
m=[0]*n
for i in range(n-1):
    if s[i]=='A' and s[i+1]=='C':
        m[i+1]=m[i]+1
    else:
        m[i+1]=m[i]

for j in range(q):
    ans=m[l[j][1]-1]-m[l[j][0]-1]
    print(ans)

Measured memory_bytes: 30664
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo(os.Stdin, os.Stdout)
	defer io.Flush()
	n, m := io.NextInt(), io.NextInt()
	ans := n*(n-1)/2 + m*(m-1)/2
	io.Println(ans)
}

// Io is I/O object
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

// NewIo returns a new Io instance
func NewIo(r io.Reader, w io.Writer) *Io {
	return &Io{
		reader: bufio.NewReader(r),
		writer: bufio.NewWriter(w),
	}
}

// Flush flushes writer
func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

// NextLine scans a line from stdin
func (io *Io) NextLine() string {
	var buffer []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer = append(buffer, line...)
		if !isPrefix {
			break
		}
	}
	return string(buffer)
}

// Next return a word from stdin
func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	r := io.tokens[io.nextToken]
	io.nextToken++
	return r
}

// NextInt returns an integer from stdin
func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

// Println is a wrapper of fmt.Fprintln
func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}


Measured memory_bytes: 1780
#include<bits/stdc++.h>
#define rep(i,a,b) for(register int i=a;i<=b;++i)
#define rpd(i,a,b) for(register int i=a;i>=b;--i)
#define rep1(i,x) for(register int i=head[x];i;i=nxt[i])
typedef long long ll;
const int N=300+5;
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
string s;int n,m;int f[N][N][N];
void Max(int &x,int y){x=max(x,y);}
int main(){
    cin>>s;s=' '+s;
    n=s.length()-1;m=read();
    rep(i,1,n)rep(j,0,m)f[i][i][j]=1;
    rep(d,2,n)rep(i,1,n){
        int j=i+d-1;if(j>n)break;
        rep(k,0,m){
            if(k)Max(f[i][j][k],f[i][j][k-1]);
            Max(f[i][j][k],f[i+1][j][k]);
            Max(f[i][j][k],f[i][j-1][k]);
            if(s[i]==s[j])Max(f[i][j][k],f[i+1][j-1][k]+2);
            else if(k)Max(f[i][j][k],f[i+1][j-1][k-1]+2);
        }
    }
    printf("%d\n",f[1][n][m]);
    system("pause");
    return 0;
}
/*
区间DP
f[i][j][k]表示翻转[i,j]这个区间,修改k次的LCS
转移时修改一次因为翻转前后都能匹配,所以LCS+2
*/

Measured memory_bytes: 110712
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cassert>
#include <exception>
#include <numeric>
using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i=(a); i<(n); ++i)
#define rrep(i,a,n) for(ll i=(a); i>(n); --i)
#define erep(i,a,n) for(ll i=(a); i<=(n); ++i)
#define rerep(i,a,n) for(ll i=(a); i>=(n); --i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define print(X) cout << (X) << endl
#define _ << " " <<
static const int INF = 1e+9+7;

int main(){
  while(true){
    int n;
    cin >> n;
    if(n == 0) break;
    int A[1010] = {};
    rep(i,0,n) cin >> A[i];

    int min = INF;
    rep(i,0,n){
      rep(j,i+1,n){
        if(abs(A[i]-A[j]) < min){
          min = abs(A[i]-A[j]);
        }
      }
    }
    print(min);
  }


  return 0;
}



Measured memory_bytes: 3096
import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0
for i in range(n):
    for j in range(i+1,n):
        k = bisect.bisect_left(l,l[i]+l[j])
        if k > j:
            ans += k - j -1 
print(ans)


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

vector<int> par(1e5); //親
vector<int> ran(1e5); //木の深さ
vector<int> diff_weight(1e5); //親ノードとの値の差分(自分-親)

//n要素で初期化
void init(int n){
    rep(i,n){
        par[i] = i;
        ran[i] = 0;
        diff_weight[i] = 0;
    }
}

//木の根を求める
int find(int x){
    if(par[x] == x) return x;
    else {
        int r = find(par[x]);
        diff_weight[x] += diff_weight[par[x]];
        return par[x] = r;
    }
}

//各ノードの重みの取得
int weight(int x){
    find(x); //経路圧縮
    return diff_weight[x];
}

//weight(y)-weight(x) = w となるように併合
void unite(int x,int y,int w){
    w += weight(x);w -= weight(y);
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(ran[x] < ran[y]) swap(x,y), w = -w;
    if(ran[x] == ran[y]) ++ran[x];
    par[y] = x;
    diff_weight[y] = w;
    return;
}

//xとyが同じ集合にいるときのweightの差
int diff(int x,int y){
    return weight(y)-weight(x);
}


//xとyが同じ集合に属するか否か
bool same(int x,int y){
    return find(x) == find(y);
}

int main(){
    int n,m; cin >> n >> m;
    init(n);
    rep(i,m){
        int l,r,d; cin >> l >> r >> d;
        l--;r--;
        if(same(l,r)){
            int dd = diff(l,r);
            if(dd != d){
                cout << "No" << endl;
                return 0;
            }
        }else unite(l,r,d);
    }
    cout << "Yes" << endl;
}


Measured memory_bytes: 1408
def fctr1(n): 
    f=[]
    c=0
    r=int(n**0.5)
    for i in range(2,r+2):
        while n%i==0:
            c+=1
            n=n//i
        if c!=0:
            f.append([i,c])
            c=0
    if n!=1:
        f.append([n,1])
    return f


n=int(input())
mod=1000000007
ans=1
l=[1 for _ in range(n)]
for i in range(2,n+1):
  f=fctr1(i)
  for x,y in f:
    l[x-1]+=y
for x in l:
  ans*=x
  if ans>=mod:
    ans%=mod

#print(l)
print(ans)

Measured memory_bytes: 3188
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector< vector<int> > VVI;
typedef pair<int,int> P;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
 
int main(){
  int N;
  cin>>N;
  VI X(N);
  rep(i,N) cin>>X[i];
  int sum=0;
  rep(i,N) sum+=X[i];
  sum/=N;
  ll ans0=0;
  rep(i,N){
    ans0+=(sum-X[i])*(sum-X[i]);
  }
  ll ans1=0;
  rep(i,N){
    ans1+=(sum-1-X[i])*(sum-1-X[i]);
  }
  if(ans0>ans1) ans0=ans1;
  ans1=0;
  rep(i,N){
    ans1+=(sum+1-X[i])*(sum+1-X[i]);
  }
  if(ans0>ans1) ans0=ans1;
  cout<<ans0<<endl;
}

Measured memory_bytes: 256
n,k = map(int, input().split())
As = list(map(int, input().split()))

def f(x):
    # サイズxで切る
    cnt = 0
    for a in As:
        # 斬らなければイケない本数
        cnt += int((a-0.01)/x)
        if cnt > k:
            return False
    # 斬らなければいけない本数がk本以下
    return cnt <= k

l = 10**-2
r = 10**9
while r-l > 10**-1:
    m = (l*r)**0.5
    if f(m):
        r = m
    else:
        l = m
print(int(l)+1)

Measured memory_bytes: 94420
import java.lang.Math.abs
import java.math.BigInteger

fun read() = readLine()!!

operator fun BigInteger.dec(): BigInteger = this.subtract(BigInteger.ONE)

// 階乗 (n!)
tailrec fun fact(n: BigInteger, ans: BigInteger = BigInteger.ONE) : BigInteger {
    if(n.compareTo(BigInteger.ONE) < 1) return ans
    return fact(n.dec(), ans * n)
}

// 最大公約数
tailrec fun gcd(a: Int, b: Int): Int {
    if(b == 0) return a
    return gcd(b, a % b)
}

// フィボナッチ数
//
tailrec fun fib(n: BigInteger, a: BigInteger = BigInteger.ZERO, b: BigInteger = BigInteger.ONE): BigInteger {
    if(n == BigInteger.ONE) return a
    return fib(n.dec(), b, a + b)
}

// 累乗(バイナリ法)
// a^n
tailrec fun pow(a: BigInteger, n: Int, ans: BigInteger = BigInteger.ONE): BigInteger {
    if(n == 0) return ans
    return pow(a * a, n.ushr(1), if(n.and(1) == 1) ans * a else ans)
}

tailrec fun newton(
    f: (Double) -> Double,
    df: (Double) -> Double,
    x: Double,
    eps: Double = Float.MIN_VALUE.toDouble()
): Double {
    val xnew = x - f(x) / df(x)
    if(abs(xnew - x) < eps) return xnew
    return newton(f, df, xnew, eps)
}

tailrec fun iteration(
    r: (Double) -> Double,
    x: Double,
    eps: Double = Float.MIN_VALUE.toDouble()
): Double {
    val xnew = r(x)
    if(abs(xnew - x) < eps) return xnew
    return iteration(r, xnew, eps)
}

// 二分探索 (木構造使ってない)
fun binarySearch(list: List<Int>, target: Int): Int {
    if(target < list.first() || list.last() < target) return -1

    var first = 0
    var last = list.size
    while (first + 1 != last) {
        val center = first + (last - first) / 2
        val centerVal = list[center]

        if(centerVal == target) return centerVal

        if(centerVal < target) first = center
        else last = center
    }
    return -1
}

fun main(vararg args: String) {
    println(when(read()) {
        "Sunny" -> "Cloudy"
        "Cloudy" -> "Rainy"
        "Rainy" -> "Sunny"
        else -> "Err"
    })
}

Measured memory_bytes: 33732
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,ans=0;
    cin >> n;
    vector<long long> vec(5);
    for(int i=0;i<5;i++){
        cin >> vec.at(i);
    }
    sort(vec.begin(),vec.end());
    ans += 4 + (n+vec.at(0)-1)/vec.at(0);
    cout << ans << endl;
}

Measured memory_bytes: 256
import sys
def main():
    input=sys.stdin.readline
    N,K=map(int,input().split())
    H=list(map(int,input().split()))
    if N-1<=K:
        print(H[N-1]-H[0])
        return
    inf=10**9+7
    dp=[inf]*N
    dp[0]=0

    for i,h in enumerate(H):
        if i==0:continue
        m=inf
        M=max(0,i-K)
        dp[i]=min([dpt+abs(h-ht) for ht, dpt in zip(H[M:i],dp[M:i])])
    print(dp[N-1])
    return
if __name__=="__main__":
    main()

Measured memory_bytes: 14016
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#define inf1 1000000009
#define inf -1
using namespace std;
int main(){
 long long t,a[200006],i,sum;sum=0;
 cin>>t;
 for(int i=1;i<=t;i++)
    cin>>a[i];
 for( i=t;i>1;i--)
    if(a[i]>i)break;
   else
        if(a[i]>a[i-1]+1)break;
 else if(a[i]==a[i-1]+1)sum+=1;
 else sum+=a[i];
 if(i>1||a[1])cout<<"-1\n";
 else cout<<sum<<endl;
}

Measured memory_bytes: 1792
#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: 1796
#include <iostream>
#include <bits/stdc++.h>
#define ll long long int 
#define MOD 1e9 + 7 
#define INF 1<<30
using namespace std ;
 
 
int main() {
        ios::sync_with_stdio(false);
        int n ;
        cin >> n;
        long sum = 0 ;
        long a , b ;
        for(int i = 0 ; i < n; i++) {
                cin >> a >> b ;
                sum += ( b - a + 1 ) ;
        }
        cout << sum << endl ;
 
}

Measured memory_bytes: 256
N = gets.chomp.to_i
heights = gets.chomp.split(' ').map(&:to_i)

result = true
down = 0

# iとi+1を比べて高さ1以内の差ならok
heights.each_with_index.reverse_each do |h, i|
  break if i == 0
  if heights[i - 1] - (h - down) > 1
    result = false
    break
  elsif heights[i - 1] - (h - down) == 1
    # iを下げる
    down = 1
  else
    down = 0
  end
end

puts result ? "Yes\n" : "No\n"


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

using namespace std;

typedef long long ll;

#define MOD (int)1e9+7
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>b;--i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()

ll powM(ll a,ll b,ll m )
{ 
  a%=m;
  ll ans=1;
  while(b>0)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }

return ans;
}



int main()
{


ll n;cin>>n;

vl a(n);
ll p=1e9+7;
ll ans=0,mxn=0;

for(int i=0;i<n;i++)
{
  cin>>a[i];
//mxn=max(mxn,a[i]);
}

for(ll pos=0;pos<64;pos++)
{
  ll c=0,d=0;
  for(ll i=0;i<n;i++)
  {
if(a[i]&1)c++;
else d++;
a[i]>>=1;
  }
 // if(d==n)break;
  ans+=((1LL<<pos)%p)*(c*d%p);
  ans%=p;
}

cout<<ans<<"\n";

return 0;

}


Measured memory_bytes: 2560
#include<stdio.h>
int main(){
	int w,h,x,y,r;
	scanf("%d%d%d%d%d",&w,&h,&x,&y,&r);
	if(r+y <= h && y-r >= 0){
		if(x+r <= w && x-r >= 0){
			printf("Yes\n");}
			else{
				printf("No\n");}
	}
	else{
		printf("No\n");}
	return 0;
}

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

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

int main() {
    string s, t;
    cin >> s >> t;

    int cnt = 0;
    int maxmatch = 0;
    rep(i, s.size() - t.size() + 1) {
        rep(j, t.size()) {
            if(s.at(i + j) == t.at(j)) cnt++;
        }
        if(cnt > maxmatch) maxmatch = cnt;
        cnt = 0;
    }
    cout << t.size() - maxmatch << endl;
}

Measured memory_bytes: 3636
import sys
N, Y = map(int,input().split())

for k in range(N+1):
	for j in range(N+1 - k):
		i = N - k -j
		if k*10000 + j*5000 + i*1000 == Y:
			print(k, j, i)
			sys.exit()
print("-1 -1 -1")


Measured memory_bytes: 2940
// 解説PDF https://img.atcoder.jp/abc145/editorial.pdf
// の解法1を実装

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>


using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i,a,b) for(int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())

#define pb push_back
#define mp make_pair
#define mt make_tuple


vector<vector<ll>> dp_solver(vector<ll>& A, vector<ll>& B, ll T) {

    ll N = SIZE(A);
    vector<vector<ll>> dp(N + 1, vector<ll>(3001));

    REP(n, N) {
        ll a = A[n];
        ll b = B[n];
        REP(time, T + 1) {
            dp[n + 1][time] = dp[n][time];
        }

        REP(time, T + 1) {
            auto before_time = time - a;
            if (before_time < 0) continue;
            dp[n + 1][time] = max(dp[n + 1][time],
                                  dp[n][before_time] + b);
        }
    }
    return dp;
}


ll solve(ll N, ll T, vector<ll>& A, vector<ll>& B) {
    auto dp1 = dp_solver(A, B, T);

    reverse(ALL(A));
    reverse(ALL(B));
    auto dp2 = dp_solver(A, B, T);
    reverse(ALL(A));
    reverse(ALL(B));

    ll ans = 0;

    // 最後に食べる料理をnとする
    REP(n, N) {
        ll local_ans = 0;
        REP(t, T) {
            auto tmp1 = dp1[n][t];
            auto tmp2 = dp2[N - n - 1][T - 1 - t];
            local_ans = max(local_ans, tmp1 + tmp2 + B[n]);
        }
        ans = max(ans, local_ans);
    }

    return ans;
}

int main(void)
{
    cin.sync_with_stdio(false);
    ll N, T;
    cin >> N >> T;
    vector<ll> A(N);
    vector<ll> B(N);

    REP(n, N) {
        ll a, b;
        cin >> a >> b;
        A[n] = a;
        B[n] = b;
    }
    cout << solve(N, T, A, B) << endl;
    return 0;
}


Measured memory_bytes: 141184
/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

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

  int64_t N;
  cin >> N;
  // const int64_t L = static_cast<int64_t>(T.length());
  int64_t mm = 0;
  // vector<int64_t> A(N);
  int64_t result = 0;
  for (int64_t i = 0; i < N; ++i) {
    int64_t a;
    cin >> a;
    result += max(mm - a, 0L);
    mm = max(mm, a);
  }
  cout << result << endl;
  return 0;
}


Measured memory_bytes: 3572
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <array>
#include <vector>
#include <functional>
using namespace std;
 
int main(void) {
	int N, M, i, j, k, l, count = 0, flag = 0;
	cin >> N >> M;
	vector<vector<char>> NN(N, vector<char>(N));
	vector<vector<char>> MM(M, vector<char>(M));
 
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++)
			cin >> NN[i][j];
	}
	for (i = 0; i < M; i++) {
		for (j = 0; j < M; j++)
			cin >> MM[i][j];
	}
 
	for (i = 0; i < N+1-M; i++){
		for (j = 0; j < N + 1 - M; j++) {
			count = 0;
			if (NN[i][j] == MM[0][0]) {
				for (k = 0; k < M; k++) {
					for (l = 0; l < M; l++) {
						if (NN[k+i][l+j] == MM[k][l]) {
							count++;
						}
					}
				}
			}
			if (count == M*M) {
				cout << "Yes" << endl;
				flag = 1;
				break;
			}
		}
		if (flag==1)break;
	}
	if (flag == 0)cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 256
package main

import "fmt"

func isInt(x float64) bool { return float64(int(x)) == x }

func main() {
	var r, g, b, n int
	fmt.Scan(&r, &g, &b, &n)
	bb := float64(b)

	ans := 0
	for i := 0; i <= n; i++ {
		for j := 0; i+j <= n; j++ {
			rest := float64(n - (r*i + g*j))
			if rest >= 0 && isInt(rest/bb) {
				ans++
			}
		}
	}
	fmt.Println(ans)
}


Measured memory_bytes: 512
A,B,K = map(int,input().split())
ans = []
for i in range(A,A+K):
    if i >= A and i <= B:
        ans.append(i)

for i in range(B-K+1,B+1):
    if i >= A and i <= B:
        ans.append(i)
setans = set(ans)
ans = list(setans)
ans.sort()
for a in ans:
    print(a)

Measured memory_bytes: 3064
def main():
    sx, sy, tx, ty = map(int, input().split())

    dy = ty - sy
    dx = tx - sx

    cmd = ""
    cmd += ("U"*dy + "R"*dx)
    cmd += ("D"*dy + "L"*dx)
    cmd += ("L" + "U"*(dy+1) + "R"*(dx+1) + "D")
    cmd += ("R" + "D"*(dy+1) + "L"*(dx+1) + "U")

    print(cmd)

if __name__ == "__main__":
    main()

Measured memory_bytes: 9148
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <stdio.h>
#include <vector>
#define int long long
#define endre getchar();getchar();return 0
#define moder 1000000007
#define inf 1000000000000000000
#define rep(i,n) for(int i=0;i<n;i++)
#define P pair<int,int>
#define all(v) v.begin(),v.end()
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end())
using namespace std;
bool prime(int n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n%i == 0)return false;
	}
	return n != 1;
}
int gcd(int x, int y) {
	if (x < y)swap(x, y);
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x * y / gcd(x, y);
}
int mod_pow(int x, int y, int mod) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x%mod;
		}
		x = x * x%mod;
		y >>= 1;
	}
	return res;
}
int kai(int x) {
	if (x == 0)return 1;
	return (kai(x - 1)*x) % moder;
}
int comb(int x, int y) {
	return kai(x)*mod_pow(kai(x - y), moder - 2, moder) % moder*mod_pow(kai(y), moder - 2, moder) % moder;
}
map<int, int> factor(int m) {
	map<int, int>res;
	for (int i = 2; i*i <= m; i++) {
		while (m%i == 0) {
			res[i]++;
			m /= i;
		}
	}
	if (m != 1)res[m]++;
	return res;
}

/*--------Library Zone!--------*/


signed main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	a = max(a, c) - min(a, c);
	b = max(b, d) - min(b, d);
	cout << (a / gcd(a, b) + b / gcd(a, b) - 1)*gcd(a, b) << endl;
	endre;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = {0, i + 1};
  }
  for (int i = 0; i < m; i++) {
    int e;
    cin >> e;
    v[e - 1].first = -(i + 1);
  }
  sort(begin(v), end(v));
  for (auto e : v) {
    cout << e.second << endl;
  }
}



Measured memory_bytes: 4408
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <fstream>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int> PI;
typedef vector<PI> VPI;
typedef unsigned long long ull;
typedef long long ll;

#define FOR(i, n) for(int i=0;i<(n);++i)
#define REP(i,s,n) for(int i=s;i<=n;++i)
#define SZ(x) ((int)(x).size())
#define LOOP(i,x) FOR(i,SZ(x))
#define IT(it,x) for(typeof((x).begin()) it = (x).begin();it!=(x).end();++it)
#define ALL(x) (x).begin(), (x).end()
#define ST first
#define ND second
#define MP make_pair
#define PB push_back
#define gc getchar

const int INF = 1e9 + 5;
const int MAXN = 3007;

double p[MAXN];

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

	int n;
    scanf("%d", &n);
    // dp[heads]
    // if we had i tosses, then tails=i-heads
    vector<double> dp(n + 1); // dp[i] - p-bility that there are i heads so far
    dp[0] = 1;
    for(int coin = 0; coin < n; ++coin) {
        double p_heads;
        scanf("%lf", &p_heads);
        for(int i = coin + 1; i >= 0; --i) {
            dp[i] = (i == 0 ? 0 : dp[i-1] * p_heads) + dp[i] * (1 - p_heads);
        }
    }

    double answer = 0;
    for(int heads = n/2+1; heads <= n; ++heads) answer += dp[heads];

    printf("%.10lf\n", answer);

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int,Int> pii;
typedef pair<Int,double> pid;
typedef pair<double,double> pdd;
typedef pair<Int,pii> pip;
typedef pair<double,Int> pdp;
#define A first
#define B second
#define pb(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x,0,sizeof x)
#define pdebug() printf("%d\n",__LINE__)
#define Itype(a) typeof(a.begin()) 
#define REP(i,a,b) for(int i = (a);i <= (b);i++)
#define FORO(i,n) REP(i,0,(int)n-1)
#define FORI(i,n) REP(i,1,(int)n)
#define FORIT(i,t) for(Itype(t) i = t.begin();i != t.end();i++)
#define eps 1e-6
#define sqr(x) ((x)*(x))
#define dist(_a,_b) sqrt(sqr(_a.A-_b.A)+sqr(_a.B-_b.B))
#define norm(_a) sqrt(sqr(_a.A)+sqr(_a.B))
template<typename T>void getMin(T &a,T b) { if(a > b) a = b; }
template<typename T>void getMax(T &a,T b) { if(a < b) a = b; }
const int inf = 1000000007;

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char A[1024][1024];
int d[1024][1024];

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

    int H, W;
    cin >> H >> W;
    queue<pii> q;
    FORO(i, H) {
        cin >> A[i];
        FORO(j, W) {
            d[i][j] = inf;
            if(A[i][j] == '#') {
                d[i][j] = 0;
                q.push(pii(i, j));
            }
        }
    }

    int ans = 0;
    while(!q.empty()) {
        pii p = q.front(); q.pop();
        getMax(ans, d[p.A][p.B]);
        //cout << p.A << " " << p.B << endl;
        for(int r = 0;r < 4;r++) {
            int nx = p.B + dx[r];
            int ny = p.A + dy[r];
            if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
            if(d[ny][nx] < inf) continue;
            d[ny][nx] = d[p.A][p.B] + 1;
            q.push(pii(ny, nx));
        }
    }

    cout << ans << endl;

    return 0;
}

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

#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
	string s;
	cin >> s;

	string s_rev = s;
	reverse(s_rev.begin(), s_rev.end());

	int cnt;
	for(int i = 0; i < s.size(); i++){
		if(s[i] != s_rev[i]){
			cnt++;
		}
	}

	cout << cnt / 2 << endl;
}

Measured memory_bytes: 256
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def num(key):
    if key == 1:
        return 2
    elif key == 7:
        return 3
    elif key == 4:
        return 4
    elif key == 9 or key == 6:
        return 6
    elif key == 8:
        return 7
    else:
        return 5

N, M, = readline().split()
N = int(N)
M = int(M)
A = list(map(int, readline().split()))
A.sort(reverse = True)
dp = [-float('inf'),] *(N+1)
dp[0] = 0
for i in range(1, N+1):
    temp = []
    for j in range(M):
        if i - num(A[j]) >= 0:
            temp.append(dp[i-num(A[j])] + 1)
    if len(temp) > 0:
        dp[i] = max(temp)

digit = dp[N]
ans = []
for i in range(digit):
    for j in range(M):
        if N - num(A[j]) >= 0 and dp[N-num(A[j])] == dp[N] - 1:
            ans.append(A[j])
            N = N - num(A[j])
            break
for i in range(digit):
    write(str(ans[i]))
write("\n")

Measured memory_bytes: 3828
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
long long gcd(long long a, long long b) {
	return b ? gcd(b, a % b) : a;
}
signed main() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int L[n], R[n];
	L[0] = a[0], R[n - 1] = a[n - 1];
	for (int i = 1; i < n; i++) {
		L[i] = gcd(L[i - 1], a[i]);
	}
	for (int i = n - 2; i >= 0; i--) {
		R[i] = gcd(R[i + 1], a[i]);
	}
	int ans = R[1];
	for (int i = 1; i < n - 1; i++) {
		ans = max(ans, gcd(L[i - 1], R[i + 1]));
	}
	ans = max(ans, L[n - 2]);
	cout << ans << endl;
	return 0;
}

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

typedef long long ll;

int main(){
    int N; cin >> N;
    vector<int> vec(N);
    vector<int> v(N);
    for (int i = 0; i < N; i++) {
        cin >> vec.at(i);
    }
    v = vec;
    sort(v.begin() , v.end());
    int max1 = v.at(N-1);
    int max2 = v.at(N-2);
    for(int i=0;i<N;i++){
        if(vec.at(i) == max1) cout << max2 << endl;
        else cout << max1 << endl;
    }
}

Measured memory_bytes: 3200
#include<stdio.h>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
	int i, a[11], b[11],x=0,y=0;
	for (i = 0; i < 10; i++){
		scanf("%d", &a[i]);
	}
	for (i = 0; i < 10; i++){
		scanf("%d", &b[i]);
	}
	sort(a, a + 10, greater<int>());
	sort(b, b + 10, greater<int>());
	for (i = 0; i <= 2; i++){
		x += a[i];
		y += b[i];
	}
	printf("%d %d\n", x, y);
	return 0;
}

Measured memory_bytes: 1040
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

pair<int, int> freq(int N){
	int a = N % 10;
	int b = 0;
	int x = N;
	while (x){
		b = x % 10;
		x = x/10;		
	}
	
	return pair<int, int> (b, a);
	
}
 
int main() {
	int N;
	cin >> N;
	map <pair<int, int>, int> DD;
	for(int i = 1; i <= N; i++){
		pair <int, int> P = freq(i);
		DD[P]++;
	}
	/*
	for(const auto e: DD){
		cout << e.first.first << " " << e.first.second <<" " << e.second << endl; 
		
	}*/
	long long count = 0;
	for(int i = 1; i <= N; i++){
		pair <int, int> P2 = freq(i);
		count += DD[pair<int, int> (P2.second, P2.first)];
	}	
	
	cout << count << endl;
	
    return 0;
}


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

int main(){
  int K,S,x,y,z;
  cin >> K >> S;

  int res=0;
  for(int x=0; x <= K; x++){
    for(int y=0; y <= K; y++){
      z = S-(x+y);
      if(z>=0 && z<=K)res++;
    }
  }
  
  cout << res << endl;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> p(n);
  for(int i = 0;i<n;i++){
    cin >> p.at(i);
  }
  int count = 0;
  for(int i = 0;i<n-2;i++){
    int a = p.at(i);
    int b = p.at(i+1);
    int c = p.at(i+2);
    if(a<b&&b<c||a>b&&b>c) count++;
  }
  cout << count << endl;
}

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

int n;
int s[212345];

signed main(){
  cin>>n;
  rep(i,n){
    cin>>s[i];
    if(i)s[i]+=s[i-1];
  }
  int ans=1e18;
  rep(i,n-1){
    ans=min(ans,abs(s[i]*2-s[n-1]));
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 1792
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.B
{
    public class Program
    {
        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var (A, B, C, D) = Read4Longs();

            while (true)
            {
                C -= B;
                if (C <= 0) return "Yes";

                A -= D;
                if (A <= 0) return"No";
            }
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}


Measured memory_bytes: 28108
n = int(input())
ll = list(map(int, input().split()))
ll.sort(reverse=True)

ans = 0

for i in range(n-2):
    j = i + 1
    k = n - 1
    while k > j:
        if ll[i] < ll[j] + ll[k]:
            ans += k - j
            j += 1
        else:
            k -= 1

print(ans)


Measured memory_bytes: 3188
import fractions
a, b, c, d = map(int, input().split())
lcm = c * d // fractions.gcd(c, d)
print(b - a + 1 - (b // c - (a-1) // c) - (b // d - (a-1) // d) + (b // lcm - (a-1) // lcm))

Measured memory_bytes: 5048
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

template<class T>
struct BellmanFord {
private:
    struct Edge {
        int from, to;
        T cost;
    };
    
    int n;
    vector<vector<int>> graph;
    vector<bool> used;
    vector<Edge> edges;
    vector<int> prev;
    
    void dfs(int v) {
        if (used[v]) return;
        used[v] = true;
        for(auto u : graph[v]) {
            dfs(u);
        }
    }
    
public:
    const T inf = numeric_limits<T>::max() / 2 - 1;
    vector<T> dist;
    vector<int> path;
    
    BellmanFord(){};
    BellmanFord(int _n) {
        n = _n;
        graph.resize(n);
        used.resize(n);
        dist.resize(n);
        prev.resize(n);
    }
    
    void add_edge(int from, int to, T cost) {
        edges.push_back((Edge){from, to, cost});
        graph[from].push_back(to);
    }
    
    bool get_distance(int from, int to = -1) {
        vector<bool> reach(n, true);
        for(int i = 0; i < n; i++) {
            fill(used.begin(), used.end(), false);
            dfs(i);
            if (to != -1) reach[i] = used[to];
        }
        fill(dist.begin(), dist.end(), inf);
        fill(prev.begin(), prev.end(), -1);
        dist[from] = 0;
        for(int i = 0; i < n; i++) {
            bool update = false;
            for(auto e : edges) {
                if ((!reach[e.from]) || (!reach[e.to]) || (dist[e.from] == inf)) continue;
                if (dist[e.to] > (dist[e.from] + e.cost)) {
                    dist[e.to] = dist[e.from] + e.cost;
                    prev[e.to] = e.from;
                    update = true;
                }
            }
            if (!update) break;
            if (i == (n - 1)) {
                return false;
            }
        }
        return true;
    }
    
    void get_path(int to) {
        path = vector<int>(0);
        for (; to != -1; to = prev[to]) {
            path.push_back(to);
        }
        reverse(path.begin(), path.end());
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n, m, p;
    cin >> n >> m >> p;
    BellmanFord<ll> bf(n);
    rep(i, m) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        bf.add_edge(a, b, p - c);
    }
    if (!bf.get_distance(0, n - 1)) {
        cout << -1 << endl;
    }
    else {
        cout << max(0LL, -bf.dist[n - 1]) << endl;
    }
    return 0;
}


Measured memory_bytes: 640
using System;
using System.Linq;

namespace ABC152
{
    class A
    {
        static void Main(string[] args)
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            if (input[0] == input[1]) Console.WriteLine("Yes");
            else Console.WriteLine("No");

        }
    }
}


Measured memory_bytes: 13396
import sys, math
from functools import lru_cache
sys.setrecursionlimit(10**9)

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    S, T = [input(), input()]

    ns = len(S)
    nt = len(T)

    m = math.inf

    for i in range(ns-nt+1):
        cnt = 0
        for j in range(nt):
            if S[i:i+nt][j] != T[j]:
                cnt += 1
        m = min(m, cnt)

    print(m)

if __name__ == '__main__':
    main()


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

int main() {
    int S; cin >> S;
    int A=S/100,B=S%100; bool C,D;
    if(0<A && A<=12) C=true;
    else C=false;
    if(0<B && B<=12) D=true;
    else D=false;
    if(C && D)cout << "AMBIGUOUS" << endl;
    else if(C)cout << "MMYY" << endl;
    else if(D)cout << "YYMM" << endl;
    else cout << "NA" << endl; 
    return 0;
}

Measured memory_bytes: 256
/*input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
*/
/*

**************************
*						 *
* AUTHOR :: shado_w      *
*						 *
**************************

*/
#include<bits/stdc++.h>
#define lld          long long int
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<lld,lld>
#define msi         map<string,lld>
#define mis         map<lld, string>
#define mpi         map<pair<lld ,lld >,lld >
#define pii         pair<lld,lld>
#define vi          vector<lld>
#define vpi 		vector<pii>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (lld)x.size()
#define hell        1000000007
#define rrep(i,m,n)	for(lld i=m;i>=n;i--)
#define rep(i,m,n)	for(lld i=m;i<n;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
#define pr(x) 		cout<< x << " ";
#define pre(x)		cout<< x << endl ;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; 
//change null_type to get ordered_map
#define N  16
lld ans[1<<N];
lld dp[1<<N];
lld a[N][N];
void solve()
{
	lld n;
	cin >> n;
	rep(i,0,n){
		rep(j,0,n){
			cin >> a[i][j];
		}
	}
	rep(i,0,1<<n){
		lld cnt=0;
		rep(j,0,n){
			rep(k,j+1,n){
				if((i>>j)&1 && (i>>k)&1){
					cnt+=a[j][k];
				}
			}
		}
		ans[i]=cnt;
	}

	rep(i,0,1<<n){
		dp[i]=ans[i];
		for(int j=i;j>0;j=(j-1)&i){
			dp[i]=max(dp[i],dp[j]+ans[j^i]);
		}
	}
	cout << dp[(1<<n)-1] << endl;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1;
	//cin>>tests;
	while(tests--)
	{
		solve();
	}
}

Measured memory_bytes: 4636
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    bool cards[4][13];

    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 13; j++){
            cards[i][j] = false;
        }
    }

    char suit;
    int number;
    for (int i = 0; i < n; i++) {
        cin >> suit >> number;
        switch(suit){
            case 'S': cards[0][number-1] = true; break;
            case 'H': cards[1][number-1] = true; break;
            case 'C': cards[2][number-1] = true; break;
            case 'D': cards[3][number-1] = true; break;
        }
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 13; j++) {
            if (cards[i][j] == false) {
                switch (i){
                    case 0: cout << "S " << j+1 << endl; break;
                    case 1: cout << "H " << j+1 << endl; break;
                    case 2: cout << "C " << j+1 << endl; break;
                    case 3: cout << "D " << j+1 << endl; break;
                }
            }
        }
    }
    return 0;
}

Measured memory_bytes: 3080