akhauriyash/Code-Regression huggingface dataset:
MOD = 10**9+7
def modpow(a, n, mod)
  exp = Math.log(n, 2).floor
  rep = modpow_cl(a, n, exp, mod)
  return rep[0]
end

def modpow_cl(a, n, exp, mod)
  if exp < 0
    rep = [1, a]
  else
    if n >= 2**exp
      rep = modpow_cl(a, n-2**exp, exp-1, mod)
      rep[0] = rep[0]*rep[1] % mod
    else
      rep = modpow_cl(a, n, exp-1, mod)
    end
    rep[1] = rep[1]**2 % mod
  end
  return rep
end

n, k = gets.split.map(&:to_i)
cnt = Array.new(k+1, 0)

num = k
while num > 0
  cnt[num] = modpow(k/num, n, MOD)
  id = num*2
  while id <= k
    cnt[num] = (cnt[num]-cnt[id]) % MOD
    id += num
  end
  num -= 1
end

sum = 0
for i in 1..k
  sum = (sum + cnt[i]*i) % MOD
end
puts sum

Measured memory_bytes: 15088
s1 = int(input())
s2 = int(input())
if s1 > s2:
    s1, s2 = s2, s1
if s2 - s1 < 180:
    print((s1+s2)/2)
elif (s1+s2)/2 +180 < 360:
    print((s1+s2)/2 +180)
else:
    print((s1 + s2) / 2 - 180)



Measured memory_bytes: 5616
#include <stdio.h>

#define aaa(i) for ((i) = 1; (i) < 10; (i)++)

int main(void)
{
	int i, j;
	
	aaa(i){
		aaa(j){
			printf("%dx%d=%d\n", i, j, i * j);
		}
	}
	
	return (0);
}

Measured memory_bytes: 520
#include <bits/stdc++.h>
using namespace std;
struct Data{
	int x,idx;
	bool operator<(const Data&r)const{
		return x<r.x;
	}
}l[100010],r[100010];
int chk[100010],b[100010];
int main(){
	int i,j,n;
	long long s,ans=0;
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%d%d",&l[i].x,&r[i].x),l[i].idx=i,r[i].idx=i;
	sort(l+1,l+n+1);
	sort(r+1,r+n+1);
	s=abs(r[1].x);
	chk[r[1].idx]=1;
	for(i=1,j=n;;){
		for(;j&&chk[l[j].idx];--j);
		if(!j||r[i].x>l[j].x){
			ans=max(ans,s+abs(r[i].x));
			break;
		}
		s+=abs(l[j].x-r[i].x);
		chk[l[j].idx]=1;
		for(;i<=n&&chk[r[i].idx];++i);
		if(i>n||r[i].x>l[j].x){
			ans=max(ans,s+abs(l[j].x));
			break;
		}
		s+=abs(r[i].x-l[j].x);
		chk[r[i].idx]=1;
	}
	for(i=1;i<=n;++i)chk[i]=0;
	s=abs(l[n].x);
	chk[l[n].idx]=1;
	for(i=1,j=n;;){
		for(;i<=n&&chk[r[i].idx];++i);
		if(i>n||r[i].x>l[j].x){
			ans=max(ans,s+abs(l[j].x));
			break;
		}
		s+=abs(r[i].x-l[j].x);
		chk[r[i].idx]=1;
		for(;j&&chk[l[j].idx];--j);
		if(!j||r[i].x>l[j].x){
			ans=max(ans,s+abs(r[i].x));
			break;
		}
		s+=abs(l[j].x-r[i].x);
		chk[l[j].idx]=1;
	}
	printf("%lld",ans);
	return 0;
}


Measured memory_bytes: 2176
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		for (; sc.hasNext();) {

			int i = 0;

			int H = sc.nextInt();
			int W = sc.nextInt();

			if (!(H == 0 && W == 0)) {

				for (i = 0; i < H; i++) {

					if (i == 0 || i == H - 1) {
						for (int j = 0; j < W; j++) {
							System.out.print("#");
						}
						System.out.println("");

					} else {
						for (int j = 0; j < W; j++) {
							if (j == 0 || j == W - 1) {
								System.out.print("#");
							} else {
								System.out.print(".");
							}
						}
						System.out.println("");
					}
				}
			}

			if (H == 0 && W == 0) {
				break;
			}
			System.out.println("");
		}

	}

}



Measured memory_bytes: 34432
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int sum[400005],lazy[400005],a[300005],n,m;;
struct obj
{
    int l,r;
}p[300005];
bool cmp(obj x,obj y)
{
    return x.r-x.l<y.r-y.l;
}
void build(int k,int l,int r)
{
    lazy[k]=0;
    if(l==r)
    {
        sum[k]=a[l];
    }
    else
    {
        int mid=(l+r)>>1;
        build(k<<1,l,mid);
        build(k<<1|1,mid+1,r);
        sum[k]=sum[k<<1]+sum[k<<1|1];
    }
}
void push_down(int k,int l,int r)
{
    if(l==r)
    {
        sum[k]+=lazy[k];
        a[l]=sum[k];
    }
    else
    {
        lazy[k<<1]+=lazy[k];
        lazy[k<<1|1]+=lazy[k];
        sum[k]+=lazy[k]*(r-l+1);
    }
    lazy[k]=0;
}
void change(int k,int l,int r,int x,int y,int w)
{
    if(l>=x&&r<=y)
    {
        lazy[k]+=w;
    }
    else
    {
        push_down(k,l,r);
        int mid=(l+r)>>1;
        if(!(y<l||x>mid))
        {                 
            change(k<<1,l,mid,x,y,w);
        }             
        if(!(y<mid+1||x>r))
        {                
            change(k<<1|1,mid+1,r,x,y,w);
    	}
    }
}
int query(int k,int l,int r,int x)
{
    push_down(k,l,r);
    if(l==r)
    {
        return a[l];
    }
    int mid=(l+r)>>1;
    if(x<=mid)
    {
        return query(k<<1,l,mid,x);
    }
    return query(k<<1|1,mid+1,r,x);
}
int main()
{
    scanf("%d%d",&n,&m);
    build(1,1,m);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&p[i].l,&p[i].r);
    }
    int i=0;
    sort(p,p+n,cmp);
    for(int d=1;d<=m;d++)
    {
        while(i<n&&p[i].r-p[i].l<d)
        {
            change(1,1,m,p[i].l,p[i].r,1);
            i++;
        }
        int ans=n-i;
        for(int k=d;k<=m;k+=d)
        {
            ans+=query(1,1,m,k);
        }
        printf("%d\n",ans);
    }
    return 0;
}

Measured memory_bytes: 6912
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll=long long;
using ld=long double;
//#define int long long
#define _CRT_SECURE_NO_WARNINGS
#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
#define ALL(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int INF = 1e18 + 9;
constexpr long double pi = 3.141592653589793238462643383279;
int fact(int i) {
    if (i == 0) return 1;
    return (fact(i - 1)) * i;
}
int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
int keta(int n) {
    if (n == 0) return 1;
    int count = 0;
    while (n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
int ketasum(int n) {
    int sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
char toSmall(char c) { // 大文字を小文字へ
    return (c + 0x20);
}
char toLarge(char c) { // 小文字を大文字へ
    return (c - 0x20);
}
float myPower(int a, int n) { // aのn乗の計算
    float x = 1;
    while (n > 0) { // 全てのbitが捨てられるまで
        if (n & 1) { // 一番右のbitが１のとき
            x *= a;
        }
        a *= a;
        n >>= 1; // bit全体を右に1つシフトして一番右を捨てる
    }
    return x;
}

int main(void) {
    int h;
    cin >> h;
    int w;
    cin >> w;
    vector<string> a(h);
    for (int i = 0; i < h; i++) {
        cin >> a[i];
    }
    vector<bool> row(h, false);
    vector<bool> col(w, false);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] == '#') {
                row[i] = true;
                col[j] = true;
            }
        }
    }
    for (int i = 0; i < h; i++) {
        if (row[i]) {
            for (int j = 0; j < w; j++) {
                if (col[j]) {
                    cout << a[i][j];
                }
            }
            cout << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 3568
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<stack>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, pii > piii;
typedef pair<double, double> pdd;
typedef pair<int, char> pic;
typedef priority_queue<pii, vector<pii>, greater<pii> > RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007



signed main(){
    int u = 0, r = 0;
    int d = 0, l = 0;
    int x, y;
    cin >> x >> y;
    int f[3] = {0};
    f[0] = 0;
    f[1] = 0;
    f[2] = 1;
    int ans = 0;
    int mp[3];
    mp[0] = 1;
    mp[1] = 2;
    mp[2] = 3;
    for(int i = 0;;i++){
        if(l <= x && x <= r && d <= y && y <= u){
            ans = mp[i % 3];
            break;
        }
        f[0] = f[1];
        f[1] = f[2];
        f[2] = f[0] + f[1];
        int fb = f[2];
        if(i % 4 == 0){
            r += fb;
        }
        else if(i % 4 == 1){
            u += fb;
        }
        else if(i % 4 == 2){
            l -= fb;
        }
        else if(i % 4 == 3){
            d -= fb;
        }
    }
    cout << ans << endl;
    

    return 0;
}

/*

*/


Measured memory_bytes: 3120
#include<stdio.h>
main(){
int n,i;
scanf("%d",&n);
int x,y,z;
for(i=0;i<n;i++){
scanf("%d %d %d",&x,&y,&z);
if(x*x+y*y==z*z||x*x+z*z==y*y||y*y+z*z==x*x)
printf("YES\n");
else
printf("NO\n");

}
}

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

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

int main() {
  int n;	cin >> n;
  vector<int>	p(n);
  vector<int>	q(n);
  rep(i,n)	cin >> p[i];
  rep(i,n)	cin >> q[i];
  
  vector<int>	data(n);
  rep(i,n)	data[i]=i+1;
  
  int a=0;
  int b=0;
  int c=1;
  do{
    bool ok=true;
    rep(i,n){
      if(data[i]!=p[i]){
        ok=false;
        break;
      }
    }
    if(ok)	a=c;
    ok=true;
    rep(i,n){
      if(data[i]!=q[i]){
        ok=false;
        break;
      }
    }
    if(ok)	b=c;
    c++;
  }while(next_permutation(data.begin(),data.end()));
  
  cout << abs(a-b);
  
}


Measured memory_bytes: 3584
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;

class Main {
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		boolean first = true;
		while(in.hasNext()) {
			if(!first) {
				in.nextLine(); // ???????????¨??????
			}

			List<String> map = new ArrayList<>();
			map.add("          "); //????????¨
			for (int i = 0; i < 8; i++) {
				String str = in.nextLine();
				str = " " + str + " ";
				map.add(str);
			}
			map.add("          "); //????????¨
			solve(map);
			first = false;
		}
	}

	public static void solve(List<String> map) {
		List<List<String>> pat = new ArrayList<>();
		pat.add(
				Arrays.asList("11",
						          "11"));
		pat.add(
				Arrays.asList("1","1","1","1")
		);
		pat.add(
				Arrays.asList("1111")
		);
		pat.add(
				Arrays.asList("01",
						"11",
						"10")
		);
		pat.add(
				Arrays.asList("110",
						"011")
		);
		pat.add(
				Arrays.asList("10",
						"11",
						"01")
		);
		pat.add(
				Arrays.asList("011",
						"110")
		);

		int ans = 0;
		mainloop:
		for(int i=0;i<map.size();i++) {
			for(int j=0;j<map.get(0).length();j++) {
				// 1?????¢???
				if(map.get(i).charAt(j) == '1') {
					// ?????????????????£??????D???G
					if(map.get(i+1).charAt(j-1) == '1') {
						for(int k=0;k<pat.size();k++) {
							if(check(map, i, j-1, pat.get(k))) {
								ans = k; break mainloop;
							}
						}
					} else {
						for(int k=0;k<pat.size();k++) {
							if(check(map, i, j, pat.get(k))) {
								ans = k; break mainloop;
							}
						}
					}
					break mainloop;
				}
			}
		}
		System.out.println((char)(ans + 'A'));
	}

	public static boolean check(List<String> map, int i, int j, List<String> pat) {
		for(int k=0;k<pat.size();k++) {
			for(int l=0;l<pat.get(k).length();l++) {
				if(map.get(i+k).charAt(j+l) != pat.get(k).charAt(l)) return false;
			}
		}
		return true;
	}
}

Measured memory_bytes: 26708
n,m=map(int,input().split())
res=[set() for i in range(n)]
num=""
flag=False
for i in range(m):
  si,ci=map(int,input().split())
  if n!=1 and si==1 and ci==0:
    flag=True
    break
  res[si-1].add(ci)
for i in range(n):
  if len(res[i])>1 or flag:
    print(-1)
    break
  elif len(res[i])==1:
    num+=str(res[i].pop())
  elif i==0 and len(res[i])==0:
    num+=str(1)
  elif i!=0 and len(res[i])==0:
    num+=str(0)
else:
  if m==0:
    if n==1:
      print(0)
    else:
      print("1"+"0"*(n-1))
  else:
    print(num)
  

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

int main(int argc, char const *argv[])
{
    string a,b;
    while(cin>>a>>b){
        reverse(b.begin(),b.end());
        if(a==b){
            printf("YES\n");
        }
        else printf("NO\n");
    }
    return 0;
}

Measured memory_bytes: 256
import numpy as np

N = int(input())
X = list(map(int, input().split()))

M = np.median(X)

Y = sorted(X)
a = Y[int(len(Y)/2 - 1)]
b = Y[int(len(Y)/2)]

for x in X:
    if x <= a:
        print(b)
    else:
        print(a)

Measured memory_bytes: 34152
#include <bits/stdc++.h>
using namespace std;
template<class T> inline void chmin(T &a, T b) {if (a > b) a = b;}
template<class T> inline void chmax(T &a, T b) {if (a < b) a = b;}
const int INF = 1e9;

int main() {
    int n, k; cin >> n >> k;
    vector<int> h(n);
    for (int i=0; i<n; ++i) cin >> h[i];
    vector<int> dp(n+2*k, INF);
    dp[0] = 0;
    for (int i=0; i<n; ++i) {
        for (int j=1; j<=k; ++j) {
            chmin(dp[i+j], dp[i] + abs(h[i] - h[i+j]));
        }
    }
    cout << dp[n-1] << endl;
}

Measured memory_bytes: 1152
(a, b, c, x) = [int(input()) for _ in range(4)]
cnt = 0
for i in range(a+1):
    for j in range(b+1):
        for k in range(c+1):
            tmp = 500 * i + 100 * j + 50 * k
            if tmp == x: cnt += 1

print(cnt)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
main(){
    int N;
    string S;
    cin >> N >> S;
    string sub = S.substr(0,N/2);
    if(sub + sub == S){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in [0]*n]
    for a, b in ab:
        a, b = min(a, b), max(a, b)
        s = (a*b-1)**0.5
        s_int = int(s)
        if a*b-1 > 1000:
            for i in range(s_int-10, s_int+10):
                if i*i > a*b-1:
                    s_int = i - 1
                    break
        if a == b:
            ans = 2*s_int
        elif (a*b-1)//s_int == s_int:
            ans = 2*s_int-2
        else:
            ans = 2*s_int-1
        print(ans)


main()


Measured memory_bytes: 3064
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (a == b) {
      cnt++;
    } else {
      cnt = 0;
    }
    if (cnt == 3) {
      // cout << "Yes" << endl;
        flag = true;
    }
  }
  cout << (flag?"Yes":"No") << endl;
}


Measured memory_bytes: 3616
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <numeric>
#include <functional>
#include <cmath>
#include <cassert>
#include <string>
#include <iostream>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int mod = 1000000007;
const ll MOD = 1000000007;
const ll INF = 1LL << 60;


int solve()
{
   ll x,y,z,k;
   cin >> x >> y >> z >> k;
   ll a[x], b[y], c[z];

   for(int i=0; i < x; i++){
      cin >> a[i];
   }
   for(int i=0; i < y; i++){
      cin >> b[i];
   }
   for(int i=0; i < z; i++){
      cin >> c[i];
   }

   

   vector<ll> t;
   for(int i=0; i < x; i++){
      for(int j=0; j < y; j++){
         t.push_back(a[i] + b[j]);
      }
   }
   sort(t.begin(), t.end(), greater<ll>());
   sort(c, c+z, greater<ll>());
   vector<ll> ans;
   for(int i=0; i < z; i++){
      for(int j=0; j < min(k,x*y); j++){
         ans.push_back(c[i] + t[j]);
      }
   }
   sort(ans.begin(), ans.end(), greater<ll>());

   for(int i=0; i < k; i++){
      cout << ans[i] << endl;
   }


   return 0;
}

int main()
{
   //cout.precision(10)
   ios::sync_with_stdio(false);
   cin.tie(0);

   solve();
}

Measured memory_bytes: 43236
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
constexpr double EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max()/2;
constexpr int MOD = 1e9+7;

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

    int a,b,c;cin>>a>>b>>c;
    if(a==b) cout<<c<<endl;
    else if(a==c) cout<<b<<endl;
    else cout<<a<<endl;
}


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

typedef struct Node{
  int value;
  Node *parent, *left, *right;
} Node;

Node *NIL, *root;

void insert(int k){
  Node *y = NIL, *x = root, *z;

  z = (Node *)malloc(sizeof(Node));
  z->value = k;
  z->left = z->right = NIL;

  while(x != NIL){
    y = x;
    if(z->value < x->value) x = x->left;
    else x = x->right;
  }
  z->parent = y;

  if(y == NIL) root = z;
  else if(z->value < y->value) y->left = z;
  else y->right = z;
}

Node* find(Node *x, int k){
  if(x == NIL || x->value == k) return x;

  if(k < x->value) return find(x->left, k);
  else return find(x->right, k);
}

void preParse(Node *node){
  if(node == NIL) return ;

  cout << " " << node->value;
  if(node->left != NIL) preParse(node->left);
  if(node->right != NIL) preParse(node->right);
}

void inParse(Node *node){
  if(node == NIL) return ;

  if(node->left != NIL) inParse(node->left);
  cout << " " << node->value;
  if(node->right != NIL) inParse(node->right);
}

int main(){

  int n, key;
  string command;

  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> command;
    if(command == "insert"){
      cin >> key;
      insert(key);
    }else if(command == "print"){
      inParse(root);
      cout << endl;
      preParse(root);
      cout << endl;
    }else if(command == "find"){
      cin >> key;
      if(find(root, key)) cout << "yes" << endl;
      else cout << "no" << endl;
    }
  }

  return 0;
}



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

using namespace std;

int main()
{
    int n;
    cin>>n;
    
    vector<int> A(n);
    for(int i=0;i<n;i++) cin>>A[i];
    
    int num=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n-1;j++){
            if(A[j]>A[j+1]){
                swap(A[j],A[j+1]);
                num++;
            }
        }
    }
    
    for(int i=0;i<n-1;i++) cout<<A[i]<<" ";
    cout<<A[n-1]<<endl<<num<<endl;
    
    return 0;
}

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

int modPow(int x, int n, int mod)
{
	int ret = 1;
	int mul = x;
	while ( n > 0 ) {
		if ( n & 1 ) ret = (int64_t)ret * mul % mod;
		mul = (int64_t)mul * mul % mod;
		n >>= 1;
	}
	return ( ret );
}

int modInv(int x, int mod)
{
	return ( modPow(x, mod - 2, mod) );
}

int modFactrial(int n, int mod)
{
	return ( n <= 1 ? 1 : (int64_t)n * modFactrial(n - 1, mod) % mod );
}

int modChoose(int n, int k, int mod)
{
	return ( (int64_t)modFactrial(n, mod) * modInv((int64_t)modFactrial(k, mod) * modFactrial(n - k, mod) % mod, mod) % mod );
}

int main()
{
	int N, M, K;
	int pat;
	const int mod = 1000000007;

	cin >> N >> M >> K;
	
	pat = modChoose(N * M - 2, K - 2, mod);
	
	int ans = 0;	
	for ( int i = 1; i < M; i++ ) {
		(ans += ((int64_t)i * (M - i) % mod * N % mod * N) % mod) %= mod;
	}
	for ( int i = 1; i < N; i++ ) {
		(ans += ((int64_t)i * (N - i) % mod * M % mod * M) % mod) %= mod;
	}

	cout << (int64_t)ans * pat % mod << endl;

	return ( 0 );
}


Measured memory_bytes: 6528
import sys
import os
from sys import stdin

# stdin = open(os.path.dirname(__file__) + '/input.txt')  # local test
input = stdin.readline
sys.setrecursionlimit(20000000)


n = int(input())
weights = list(map(int, input().split()))
weights += [0, 0, 0]

dp = [9999999999] * (n + 3)
dp[0] = 0

for i in range(n - 1):
    step1 = abs(weights[i] - weights[i + 1])
    step2 = abs(weights[i] - weights[i + 2])

    dp[i + 1] = min(dp[i + 1], dp[i] + step1)
    dp[i + 2] = min(dp[i + 2], dp[i] + step2)

print(dp[n - 1])


Measured memory_bytes: 50512
N,M,Q=map(int,input().split())
query=[]
for i in range(Q):
    a,b,c,d=map(int,input().split())
    query.append([a,b,c,d])

ans=0
def dfs(list):
    global ans
    if len(list)==N:
        test=0
        for i in range(0,Q):
            a,b,c,d=query[i]
            if list[b-1]-list[a-1]==c:
                test+=d
        ans=max(test,ans)
        return 0
    if list:
        m=list[-1]
        for i in range(m,M+1):
            dfs(list+[i])
    else:
        for i in range(1,M+1):
            dfs([i])

dfs([])
print(ans)

Measured memory_bytes: 73112
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> a(n+1);
  vector<int> b(n);
  for(int i=0;i<=n;i++){
    cin>>a[i];
  }
  for(int i=0;i<n;i++){
    cin>>b[i];
  }
  vector<int> sub(n,0);
  long long sum=0;
  int buf;
  for(int i=0;i<n;i++){
    if(a[i]>=b[i]){
      sum+=b[i];
    }
    else{
      sum+=a[i];
      if(a[i+1]>=b[i]-a[i]){
	a[i+1]-=b[i]-a[i];
	sum+=b[i]-a[i];
      }
      else {
	sum+=a[i+1];
	a[i+1]=0;
      }
    }
  }
  cout<<sum<<endl;
  return 0;
}


Measured memory_bytes: 1408
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
#include <stack>
using namespace std; 

#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 pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

bool p[101][101];

int main()
{
	while(1) {
		int n, m, k;
		cin >> n;
		if (!n) break;
		memset(p, false, sizeof(p));
		rep(i,0,n) {
			cin >> m;
			rep(j,0,m) {
				int t; 
				cin >> t;
				p[i][t] = true;
			}
		}
		Vi l;
		cin >> k;
		rep(i,0,k) {
			int t;
			cin >> t;
			l.pb(t);
		}
		
		Vi  f;
		rep(i,0,n) {
			bool f2 = true;
			rep(j,0,k) {
				if (p[i][ l[j] ] == false) {
					f2 = false;
					//printf("wrong i=%d l[j]=%d\n",i,l[j]);
					break;
				}
				//printf("correct i=%d l[j]=%d\n",i,l[j]);
			}
			if (f2) f.pb(i);
		}
		//cout << "answer is ";
		if (len(f) == 1) cout << f[0]+1 << endl;
		else cout << -1 << endl;
	}

	return 0;
}

Measured memory_bytes: 1232
import java.util.*;
public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
        String S = sc.next();
        char[]ss = S.toCharArray();

        int cou = 0;
        for(int i=0; i<S.length()/2; i++){
            if(ss[i]!=ss[S.length()-1-i]){
                cou++;
    }
}
        for(int i=0; i<(S.length()-1)/2/2; i++){
            if(ss[i]!=ss[(S.length()-1)/2-1-i]){
                cou++;
    }
}
         for(int i=((S.length()+3)/2)-1; i<S.length(); i++){
            if(ss[((S.length()+3)/2)-1]!=ss[S.length()-1]){
                cou++;
    }
}
        if(cou==0){
            System.out.println("Yes");    
        }else{
            System.out.println("No");
    }
}
}


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

int main(){
    int n, t, e; cin>>n>>t>>e;
    vector<int> x(n);
    for(int i=0; i<n; i++) cin>>x[i];

    for(int i=0; i<n; i++){
        for(int j=0; ; j++){
            if(t + e < j * x[i]) break;

            if(t - e <= j * x[i] && j * x[i] <= t + e){
                cout << i + 1 << endl;
                return 0;
            }
        }
    }

    cout << -1 << endl;
}


Measured memory_bytes: 3092
#include<bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int x, a, b;
    cin >> x >> a >> b;
    if(a>=b) {
        cout << "delicious" << endl;
    } else if(a+x>=b) {
        cout << "safe" << endl;
    } else {
        cout << "dangerous" << endl;
    }
}

Measured memory_bytes: 256
package main

import (
	"fmt"
	"os"
)

const (
	maxValuePerItem = 1000
	debug           = 0
)

type item struct {
	w int
	v int
}

func main() {
	var n, w int
	fmt.Scan(&n, &w)
	items := make([]item, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&(items[i].w))
		fmt.Scan(&(items[i].v))
	}
	maxValueSum := maxValuePerItem * n

	// dp[i][sum_v] = i個目までのitemを任意に選択した際、価値がsum_vとなるような最軽量値
	dp := make([][]int, n)
	for i := 0; i < n; i++ {
		// 0を含むため+1が必要
		dp[i] = make([]int, maxValueSum+1)
		for j := 0; j < maxValueSum+1; j++ {
			dp[i][j] = -1
		}
	}
	dp[0][0] = 0

	// i == 0のとき
	for j := 0; j < maxValueSum+1; j++ {
		if items[0].v == j {
			dp[0][j] = items[0].w
		}
	}
	if debug == 1 {
		fmt.Println(dp[0][0:10])
	}

	for i := 1; i < n; i++ {
		for j := 0; j < maxValueSum+1; j++ {
			if j-items[i].v < 0 {
				dp[i][j] = dp[i-1][j]
				continue
			}

			var w1, w2 int
			w1 = dp[i-1][j]
			w2 = dp[i-1][j-items[i].v]

			if w1 == -1 && w2 == -1 {
				dp[i][j] = -1
			} else if w1 == -1 {
				dp[i][j] = w2 + items[i].w
			} else if w2 == -1 {
				dp[i][j] = w1
			} else {
				w2 = w2 + items[i].w
				if w1 > w2 {
					dp[i][j] = w2
				} else {
					dp[i][j] = w1
				}
			}
		}
		if debug == 1 {
			fmt.Println(dp[i][0:10])
		}
	}

	for j := maxValueSum; j >= 0; j-- {
		if dp[n-1][j] != -1 && dp[n-1][j] <= w {
			fmt.Println(j)
			os.Exit(0)
		}
	}
}


Measured memory_bytes: 81536
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<string,int>;
using T = tuple<string,int,int>;

int main(){
  int n, l; cin >> n >> l;
  int app = 0;
  int app2 = 0;
  int ans = 0;
  int temp = 10001000;
  for(int i = 1; i <= n; i++){
    app += l + i - 1;
  }
  int a = 100100100;
  for(int i = 1; i <= n; i++){
    int temp = app;
    temp -= l + i - 1;
    int b = abs(temp - app);
    if(b < a){
      a = b;
      ans = temp;
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
[N, M] = [int(i) for i in input().split()]
submits = [0]*M

for i in range(M):
    submits[i] = input().split()

ac = 0

acs = set([])
was = {}

for i in range(1, N+1):
    was[str(i)] = 0

for item in submits:
    if item[0] in acs:
        continue
    elif item[1] == "WA":
        was[item[0]] += 1
    else:
        ac += 1
        acs.add(item[0])
        
pen = 0

for key in was.keys():
    if key in acs:
        pen += was[key]
        
print(' '.join([str(ac), str(pen)]))


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

int main()
{
    int n;
    int *a;
    int w;
    int r = 0;
    int min_wr = 1001;
    int min_wl = 1001;
    int i;
    
    scanf("%d", &n);
    
    a = (int *)malloc(sizeof(int) * n);
    
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] == 0)
        {
            r++;
        }
    }
    
    if((r == 0) || (r == n))
    {
        puts("0");
    }
    else
    {
        for(i = 0; i < n; i++)
        {
            scanf("%d", &w);
            if(a[i] == 0)
            {
                if(w < min_wr)
                {
                    min_wr = w;
                }
            }
            else
            {
                if(w < min_wl)
                {
                    min_wl = w;
                }                
            }
        }
        printf("%d\n", min_wr + min_wl);
    }
    
    return 0;
}


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

int main(){
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	if(abs(a - c) <= d ||( abs(a - b) <= d && abs(b - c) <= d))cout << "Yes" << endl;
	else cout << "No" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << endl; }
#define YN(f) cout << (f ? "YES" : "NO") << endl
#define Yn(f) cout << (f ? "Yes" : "No") << endl
#define yn(f) cout << (f ? "yes" : "no") << endl
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

int main() {
	ll X; cin >> X;
	ll q = X / 500, r = X % 500;
	ll ans = 1000 * q + r / 5 * 5;
	cout << ans << endl;	
}

Measured memory_bytes: 256
#include<stdio.h>
int main(){
  int n,t;
  scanf("%d%d",&n,&t);
  int i,tmp,hoge,ans=10e7;
  for(i=0;i<n;i++){
    scanf("%d%d",&tmp,&hoge);
    if(hoge<=t){
      if(tmp<ans)ans=tmp;
    }
  }
  if(ans==10e7)puts("TLE");
  else
    printf("%d\n",ans);
  return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

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

    int digit = 0;

    vector<ll> nums(17), acum(17);
    FOR(i, 1, 16) nums[i] = pow(26, i);
    FOR(i, 1, 16) acum[i] = acum[i-1] + nums[i];

    REP(i, 8) cerr << "acum[i] = " << acum[i] << endl;

    REP(i, 16) {
        if(N <= acum[i]) {
            digit = i;
            break;
        }
    }

    cerr << "digit = " << digit << endl;

    vector<int> ans;
    while(digit > 0) {
        // cerr << "N = " << N << " digit = " << digit << endl;
        if(digit == 1) {
            ans.push_back(N);
            break;
        }
        // assert(N < acum[digit]);
        FOR(i, 1, 26)
            if(N <= ll(i) * nums[digit-1] + acum[digit-1]) {
                ans.push_back(i);
                N -= ll(i-1) * nums[digit-1] + acum[digit-1];
                N += acum[digit-2];
                cerr << "substruct: " << ll(i-1) * nums[digit-1] + acum[digit-1] << " N = " << N << endl;
                digit--;
                break;
            }
    }

    cerr << "ans : ";
    for(int x: ans) cerr << x << " ";
    cerr << endl;

    for(int x: ans) {
        char c = (char)('a' + x - 1);
        cout << c;
    }
    cout << endl;

    return 0;
}

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

bool hor(vector<vector<char>> sign,int h,int w,int x){
  int sum=0;
  for(int i=0;i<w;i++){
    if(sign.at(x).at(i)!='.'){
      sum++;
      //cout<<sum;
    }
  }
  return sum!=0;
}
bool ver(vector<vector<char>> sign,int h,int w,int y){
  int sum=0;
  for(int i=0;i<h;i++){
    if(sign.at(i).at(y)!='.')
      sum++;    
  }
  return sum!=0;
}



int main(){
  int h,w;
  cin>>h>>w;
  
  vector<vector<char>> sign (h,vector<char>(w));
  
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++)
    {
      cin >> sign.at(i).at(j); 
      //cout<< sign.at(i).at(j);
    }
  }
  
  for(int i=0;i<h;i++){
    //cout<<i;
    //cout<<hor(sign,h,w,i)<<endl;
    if(hor(sign,h,w,i)){
      for(int j=0;j<w;j++){
        if(ver(sign,h,w,j))
          cout<<sign.at(i).at(j);
      }
      cout<<endl;
    }
  }
  
  
}

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

using namespace std;

using ll = long long int;

int main(){
    
    ll N;
    array<ll, 200000> A;
    
    cin >> N;
    for(ll i = 0; i < N; ++i){
        cin >> A[i];
    }
    
    ll sum = 0;
    for(ll i = 0; i < N; ++i){
        sum += A[i];
    }
    
    ll mn = 0x7FFFFFFFFFFFFFFF;
    ll x = A[0];
    ll y = sum - x;
    for(ll i = 1; i < N; ++i){
        mn = min(mn, abs(x - y));
        x += A[i];
        y -= A[i];
    }
    
    cout << mn << endl;
    
    return 0;
}

Measured memory_bytes: 1792
#define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(2);
}
#define int ll

void solve() {
	while (true) {
		int n, m, s, g1, g2;
		cin >> n >> m >> s >> g1 >> g2;
		if (n == 0) return;
		int dist[200][200] = {};
		REP(i, 200) {
			REP(q, 200) {
				if (i != q) {
					dist[i][q] = 1e9;
				}
			}
		}
		REP(t, m) {
			int a, b, c;
			cin >> a >> b >> c;
			dist[a][b] = c;
		}
		n++;
		REP(t, 3) {
			REP(i, n) {
				REP(q, n) {
					REP(j, n) {
						dist[i][q] = min(dist[i][q], dist[i][j] + dist[j][q]);
					}
				}
			}
		}
		int ans = 1e18;
		for (int i = 0; i < n; ++i) {
			ans = min(ans, dist[s][i] + dist[i][g1] + dist[i][g2]);
		}
		cout << ans << endl;
	}
}

#undef int
int main() {
	init();
	solve();
}


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

long long x[10][10];

int get_first_digit(int n)
{
	while (n >= 10) n /= 10;
	return n;
}

int main()
{
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i)
	{
		x[get_first_digit(i)][i % 10]++;
	}
	
	long long ans = 0;
	for (int i = 1; i < 10; ++i)
	{
		for (int j = 1; j < 10; ++j)
		{
			ans += x[i][j] * x[j][i];
		}
	}
	
	cout << ans << '\n';
}


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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int MOD = (1e9)+7;
const int N = 1e6+5;

signed main()
{
    FAST
    int tc=1;
    //cin>>tc;
    for(int ti=1;ti<=tc;ti++)
    {
        int n,k;
        cin>>n>>k;
        int a[n+1],vis[n+1];
        for(int i=1;i<=n;i++)cin>>a[i],vis[i]=0;
        vector<int> cyc;
        int curr = 1,start;
        while(1)
        {
            cyc.pb(curr);
            vis[curr] = 1;
            curr = a[curr];
            if(vis[curr])
            {
                start = curr;
                break;
            }
        }
        vector<int> rep;
        int flag = 0,cnt = 0;;
        for(int i=0;i<cyc.size();i++)
        {
            if(cyc[i] == start)flag = 1;
            if(flag)rep.pb(cyc[i]);
            else cnt++;
        }
        int ans;
        if(k<cyc.size())ans = cyc[k];
        else
        {
            k -= cyc.size();
            k %= rep.size();
            ans = rep[k];
        }
        cout<<ans<<endl;
    }
    return 0;
}


Measured memory_bytes: 10756
import java.util.Scanner

/**
  * Created by ui on 2016/02/07.
  */
object PrintARectangle3 {


  def inputHW (s:Scanner)= {
    val h = s.nextInt
    val w = s.nextInt
    (h,w)
  }
  def printRectangle (x:Int, y:Int): Unit = {
    val s = "".padTo(x, '#')
    for (i <- 0 until y) println(s)
    println
  }
  def run: Unit = {

    val sc: Scanner = new java.util.Scanner(System.in)
    var a = inputHW(sc)
    var h = a._1
    var w = a._2
    while (h != 0 || w != 0) {
    printRectangle(w,h)
      a = inputHW(sc)
      h = a._1
      w = a._2
    }
  }
}
/**
  * Created by ui on 2016/02/04.
  */
object Main {
  def main(args: Array[String]): Unit = {
    PrintARectangle3.run
  }
}

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

int main(){
int n;
cin>>n;
vector<int> vec(n);
for (int i = 0; i < n; i++) {
  cin >> vec.at(i);
}
int sw=0;
for (int i=0;i<n;i++){
    int minj=i;
    for (int j=minj+1;j<n;j++ ){
        if(vec[j]<vec[minj]){
            minj=j;
        }
    }
    if(i!=minj){
        sw++;
    }
    swap(vec[i],vec[minj]);
        
       
       
            
           
    
}
 for(int j=0;j<n;j++) cout << vec[j] << (j==n-1?'\n':' ');
 cout<<sw<<endl;

return 0;
}



Measured memory_bytes: 3084
//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<unordered_set>
#include<bitset>
#include<complex>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

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

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD =  (int)1e9 + 7;
const double EPS = 1e-9;

int DP[64][3];

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

	DP[63][0] = 1;
	rrep(i, 0, 63)
	{
		int d = (N >> i) & 1;
		if (d == 0)
		{
			//(0,0)
			DP[i][0] += DP[i + 1][0];
			DP[i][2] += DP[i + 1][1];
			DP[i][2] += DP[i + 1][2];

			//(0,1),(1,0)
			DP[i][1] += DP[i + 1][1];
			DP[i][2] += DP[i + 1][2];

			//(1,1)
			DP[i][0] += DP[i + 1][1];
			DP[i][2] += DP[i + 1][2];
		}
		else
		{
			//(0,0)
			DP[i][1] += DP[i + 1][0];
			DP[i][2] += DP[i + 1][1];
			DP[i][2] += DP[i + 1][2];

			//(0,1),(1,0)
			DP[i][0] += DP[i + 1][0];
			DP[i][2] += DP[i + 1][1];//
			DP[i][2] += DP[i + 1][2];

			//(1,1)
			DP[i][1] += DP[i + 1][1];
			DP[i][2] += DP[i + 1][2];
		}

		rep(j, 0, 3)DP[i][j] %= MOD;
	}

	int ans = 0;
	rep(i, 0, 3)
	{
		ans += DP[0][i];
	}

	cout << ans % MOD << endl;

	return 0;
}

Measured memory_bytes: 256
//hey1729gautam@gmail.com
  #include<bits/stdc++.h>
 using namespace std;
 #define bolt ios::sync_with_stdio(false)
 #define n_l '\n'
 #define ll long long
 template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c){ return string({c}); } template<size_t N> string to_string(bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgs(){;} template<typename Heads, typename... Tails> void dbgs(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgs(T...); }
 #define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgs(__VA_ARGS__); cout << endl;
 int main() {

    bolt;
    cin.tie(NULL);
    cout.tie(NULL);
    ll L, R; cin >> L >> R;
    if(R - L + 1 >= 2019) {
    	cout << "0" << endl;
    }	
    else {
    	ll z = INT_MAX;
    	for(ll i = L; i <= R; i++) {
    		for(ll j = L; j <= R; j++) {
    			if(i == j) 
    				continue;
    		     z = min(z, ((i  % 2019) * (j % 2019)) % 2019);
    		}
    	}
    	cout << z << endl;
    }

  
    return 0;
 }

Measured memory_bytes: 256
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = LI()
    B = LI()

    cnt = 0
    pre = A[-1]
    for i in range(n-1, -1, -1):
        ai = pre
        aj = A[i]
        b = B[i]
        # print(ai, aj, b)
        if ai >= b:
            cnt += b
            pre = aj
        else:   # ai < b
            cnt += ai
            b = b - ai
            if aj >= b:
                cnt += b
                pre = aj - b
            else:
                cnt = cnt + aj
                pre = 0
    print(cnt)






if __name__ == '__main__':
    solve()


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

long long m,n,ans;

int main()
{
	scanf("%lld %lld",&n,&m);
	if(n==1||m==1) {
		printf("1");
		return 0;
	}
	printf("%lld",(n*m+1ll)>>1);
}

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

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
        #define eprintf(...) 42
#endif

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(x) (x).begin(),(x).end()
#define foreach(u,v) for(auto (u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL<<60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*
  変色回数を固定する
  i回変色
  
*/

int main()
{
  int n, x;
  cin >> n >> x;
  
  vi a(n);
  rep(i, n) cin >> a[i];

  ll ans = linf;
  rep(i, n){
    set<int> s; 
    ll tmp = (ll)i*x;
    rep(j, i){
      s.insert(a[(n-j-1)%n]);
    }
    
    rep(j, n){
      s.insert(a[j]);
      tmp += *s.begin();
      s.erase(a[(j-i+n)%n]);
    }

    ans = min(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 384
use std::io;
use std::cmp::Ordering;

fn main() {
    let mut input = String::new();

    io::stdin().read_line(&mut input)
        .expect("Failed to read line");

    let vec: Vec<&str> = input.split_whitespace().collect();

    let input1: i32 = vec[0].parse().unwrap_or(0);
    let input2: i32 = vec[1].parse().unwrap_or(0);

    match input1.cmp(&input2) {
        Ordering::Less    => println!("a < b"),
        Ordering::Equal   => println!("a == b"),
        Ordering::Greater => println!("a > b"),
    }
}



Measured memory_bytes: 5024
import java.util.Arrays;
import java.util.Scanner;

public class Main {

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

	Scanner sc = new Scanner(System.in);

	void run() {
		for (;;) {
			int n = sc.nextInt();
			int m = sc.nextInt();
			if ((n | m) == 0) {
				break;
			}
			int a[] = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = sc.nextInt();
			}

			int max = -1;
			// まずは小さい順にソート
			Arrays.sort(a);
			for (int i = 0; i < n; i++) {
				// 同じアイテムを2つ取ることは禁止されているので、
				// [i+1, n)の範囲で二分探索を行う
				int j = Arrays.binarySearch(a, i + 1, n, m - a[i]);
				if (j < 0) {
					j = -j - 2;
				}
				if (j > i) {
					max = Math.max(max, a[i] + a[j]);
				}
			}
			if (max == -1) {
				System.out.println("NONE");
			} else {
				System.out.println(max);
			}
		}
	}
}



Measured memory_bytes: 45684
#include <bits/stdc++.h>
#define REP(i, n) for(ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for(ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF (1ll << 60)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
using vl = vector<ll>;
using vvl = vector<vl>;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;
using ll = long long;
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

void print(const std::vector<int> &v) {
    std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
    std::cout << std::endl;
}
double to_deg(double r) { return r * 180.0 / (atan(1.0) * 4.0); }
ll modpow(ll a, ll p) {
    if(p == 0)
        return 1;
    if(p % 2 == 0) {
        ll halfp = p / 2;
        ll half = modpow(a, halfp);
        return half * half % MOD;
    } else {
        return a * modpow(a, p - 1) % MOD;
    }
}

ll calcComb(ll a, ll b) {
    if(b < 0) {
        return 0;
    }
    if(a < 0) {
        return 0;
    }
    if(a < b) {
        return 0;
    }
    if(b > a - b) {
        return calcComb(a, a - b);
    }
    ll ansMul = 1;
    ll ansDiv = 1;
    REP(i, b) {
        ansMul *= (a - i);
        ansDiv *= (i + 1);
        ansMul %= MOD;
        ansDiv %= MOD;
    }
    ll ans = ansMul * modpow(ansDiv, MOD - 2);
    ans %= MOD;
    return ans;
}

int main() {
    int X;
    int Y;
    cin >> X >> Y;
    if((X + Y) % 3 != 0) {
        cout << 0 << endl;
        return 0;
    }
    if(X == 0 and Y == 0) {
        cout << 0 << endl;
        return 0;
    }
    if(X / Y > 2 or Y / X > 2) {
        cout << 0 << endl;
        return 0;
    }
    int cnt = (X + Y) / 3;
    int diff = abs(X - Y);
    int small = (cnt - diff) / 2;
    int large = (cnt + diff) / 2;
    cout << calcComb(cnt, small) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,x=0;
string S;
cin>>N>>S;
int sum[N]={0};
for(int i=0;i<N;i++){
    if(S[i]=='I')x++;
    else x--;
    sum[i]=x;
}
sort(sum,sum+N);
if(sum[N-1]<=0)cout<<0;
else cout<<sum[N-1];
return 0;
}

Measured memory_bytes: 256