akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 11111
int tra[111][111][111],tr2[111];
int nap[MAX];
int dp[111][111];
int main(){
  int s,t,u,n,o,d;
  cin>>s>>t>>u>>n>>o>>d;
  int e[t],c[t];
  for(int i=0;i<t;i++) cin>>e[i]>>c[i];
  int h[n],a[n];
  for(int i=0;i<n;i++) cin>>h[i]>>a[i];
   
  
  memset(nap,0,sizeof(nap));
  for(int i=0;i<n;i++){
    for(int j=a[i];j<MAX;j++){
      nap[j]=max(nap[j],nap[j-a[i]]+h[i]);
    }
  }
   
  //for(int i=1;i<MAX;i++) nap[i]=max(nap[i],nap[i-1]);
   
  //for(int j=0;j<MAX;j++) if(nap[j]) cout<<j<<" "<<nap[j]<<endl;
   
  memset(tra,-1,sizeof(tra));
  tra[0][0][0]=0;
  for(int i=0;i<t;i++){
    for(int j=0;j<=u;j++){
      for(int k=0;k<=s;k++){
    if(tra[i][j][k]<0) continue;
    tra[i+1][j][k]=max(tra[i+1][j][k],tra[i][j][k]);
    if(j+1<=u&&k+e[i]<=s) 
      tra[i+1][j+1][k+e[i]]=max(tra[i+1][j+1][k+e[i]],tra[i][j][k]+c[i]);
      }
    }
  }
   
  for(int k=0;k<=s;k++) tr2[k]=tra[t][u][k];//,cout<<k<<" "<<tra[t][u][k]<<endl;
 
  memset(dp,-1,sizeof(dp));
  dp[0][s]=0;
  for(int i=0;i<d;i++){
    for(int j=0;j<=s;j++){
      if(dp[i][j]<0) continue;
      for(int k=0;k<=j;k++){
    if(tr2[k]<0) continue;
    dp[i+1][min(j-k+o,s)]=max(dp[i+1][min(j-k+o,s)],dp[i][j]+nap[tr2[k]]);
      }
    }
  }
  int ans=-1;
  for(int k=0;k<=s;k++) ans=max(ans,dp[d][k]);
  cout<<ans<<endl;
  return 0;
}

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

int main() {
  int n; cin >> n;
  vector<int> p(n), sorted_p(n);
  rep(i,n) {
    cin >> p[i];
  }
  sorted_p = p;
  sort(sorted_p.begin(), sorted_p.end());
  bool ans = false;
  if (p == sorted_p) ans = true;
  rep(i,n) {
    for (int j=i+1; j<n; j++) {
      vector<int> test(n);
      test = p;
      swap(test[i], test[j]);
      if (test == sorted_p) {
        ans = true;
        break;
      }
    }
  }
  if (ans) cout << "YES" << endl;
  else cout << "NO" << endl;
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    R=[[] for _ in range(n)]
    for _ in range(n-1+m):
        u,v=map(int,input().split())
        u-=1; v-=1
        E[u].append(v)
        R[v].append(u)

    # longest path (topological sort)
    dp=[None]*n
    def dfs(v):
        if(dp[v] is not None):
            return dp[v]
        res=0
        for nv in E[v]:
            res=max(res,dfs(nv)+1)
        dp[v]=res
        return res

    for v in range(n):
        dfs(v)

    # 各頂点に対して、逆辺の dp の値の最小が親
    ans=[None]*n
    root=max((dp[v],v) for v in range(n))[1]
    ans[root]=-1
    for v in range(n):
        if(v==root): continue
        p=min((dp[p],p) for p in R[v])[1]
        ans[v]=p

    print(*map(lambda x:x+1,ans),sep='\n')
resolve()

Measured memory_bytes: 158768
#include <iostream>
#include <algorithm>

using namespace std;

int main(void) 
{
    long long n, h, w;
    cin >> n >> h >> w;

    long long sum = 0;
    for (int i = 0; i < n; i++)
    {
        long long a, b;
        cin >> a >> b;
        if (h <= a && w <= b) sum++;
    }   

    cout << sum << endl;

    return 0;
}

Measured memory_bytes: 256
package main

import "fmt"

func main() {
	var h, w, n, sr, sc int
	fmt.Scan(&h, &w, &n, &sr, &sc)
	var s, t string
	fmt.Scan(&s, &t)

	L, R := 1, w
	for i := 0; i < n; i++ {
		switch t[n-i-1] {
		case 'L':
			R = min(w, R+1)
		case 'R':
			L = max(1, L-1)
		}

		switch s[n-i-1] {
		case 'L':
			L++
		case 'R':
			R--
		}

		if L > R {
			fmt.Println("NO")
			return
		}
	}

	if !(L <= sc && sc <= R) {
		fmt.Println("NO")
		return
	}

	T, B := 1, h
	for i := 0; i < n; i++ {
		switch t[n-i-1] {
		case 'U':
			B = min(h, B+1)
		case 'D':
			T = max(1, T-1)
		}

		switch s[n-i-1] {
		case 'U':
			T++
		case 'D':
			B--
		}
		if T > B {
			fmt.Println("NO")
			return
		}
	}

	if !(T <= sr && sr <= B) {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}


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

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

  int A[M];
  int sum = 0;

  for (int i = 0; i < M; ++i)
  {
    cin >> A[i];
    sum += A[i];
  }
  if (sum > N){
    cout << -1 << endl;
  }else{
    cout  << N - sum << endl;
  }





}



Measured memory_bytes: 3668
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cerr << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cerr<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 998244353;
const int inf = (1 << 30) - 1;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; //use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////

int N;
string S;
ll dp[MAX_N];

ll get(ll n) {
	int a = N / n;
	ll res = 0;
	ll c = 1;
	rep(i, 0, a) {
		ADD(res, (S[a - 1 - i] == '1') * c);
		MUL(c, 2);
	}
	int one = 1;
	rep(i, 0, N) {
		int d = (S[i] == '1');
		int e;
		if(i % (2 * a) >= a) {
			e = (S[i % a] == '0');
		}
		else e = (S[i % a] == '1');
		// debug(n, d, e);
		if(d > e) break;
		else if(d < e) {
			one = 0; break;
		}
	}
	ADD(res, one);
	// debug(n, one, res);
	return res;
}

void solve() {
	cin >> N;
	cin >> S;
	ll ans = 0;
	for(int i = N; i >= 1; i--) {
		if(N % i || i % 2 == 0) continue;
		dp[i] = get(i);
		for(int j = i * 2; j <= N; j += i) {
			if(j % 2 == 0) continue;
			ADD(dp[i], mod - dp[j]);
		}
		ADD(ans, dp[i] * (N / i) % mod * 2 % mod);
	}
	cout << ans << "\n";
}

uint32_t rd() {
	uint32_t res;
#ifdef __MINGW32__
	asm volatile("rdrand %0" :"=a"(res) ::"cc");
#else
	res = std::random_device()();
#endif
	return res;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
    cerr << fixed;
	cerr.precision(6);
	rng.seed(rd());
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
	if(!freopen("in.txt", "rt", stdin)) return 1;
#endif	
	solve();
    cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}



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

int main()
{
    int n;
    cin >> n;
    vector<int> a1(n), a2(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a1[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a2[i];
    }

    vector<int> cum1(n + 1, 0), cum2(n + 1, 0);
    for (int i = 0; i < n; i++)
    {
        cum1[i + 1] = cum1[i] + a1[i];
        cum2[i + 1] = cum2[i] + a2[i];
    }
    
    int ans = 0;
    for (int i = 1; i < n + 1; i++)
    {
        ans = max(ans, cum1[i] + cum2[n] - cum2[i - 1]);
    }
    cout << ans << endl;
}


Measured memory_bytes: 3624
import bisect
import sys

N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 0
if N == 2:
    print(str(A[1]-A[0]))
    print(str(A[1]) + " " + str(A[0]))
else:
    if A[0] <= 0 and A[-1] >= 0:
        for i in range(N):
            ans += abs(A[i])
        print(str(ans))
        boarder = bisect.bisect_left(A, 0)
        if boarder != 0 and boarder != N-1:
            print(str(A[boarder-1]) + " " + str(A[boarder]))
            x = A[boarder-1] - A[boarder]
        else:
            if boarder == N-1:
                x = A[-1]
                for i in range(N-2, -1, -1):
                    print(str(x) + " " + str(A[i]))
                    x -= A[i]
                sys.exit()
            x = 0
        for i in range(boarder+1, N-1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        print(str(A[-1]) + " " + str(x))
        x = A[-1] - x
        for i in range(boarder-2, -1, -1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
    elif A[0] <= 0:
        print(str(abs(sum(A))+A[-1]*2))
        
        print(str(A[N-1]) + " " + str(A[N-2]))
        x = A[N-1] - A[N-2]
        for i in range(N-3, -1, -1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        
    else:
        print(str(sum(A)-A[0]*2))

        print(str(A[0]) + " " + str(A[1]))
        x = A[0] - A[1]
        for i in range(2, N-1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        print(str(A[-1]) + " " + str(x))

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

int main(){
  string s; cin >> s;
  int n = s.size();
  vector<int> a(n+1,0);
  for(int i=0; i<n; i++) {
    if(s[i]=='<') a[i+1]=a[i]+1;
  }
  for(int i=n-1; i>=0; i--) {
    if(s[i]=='>') a[i]=max(a[i],a[i+1]+1);
  }
  cout << accumulate(a.begin(),a.end(),0LL) << endl;  
  return 0;
}


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

class Combination {
    /*
        :)
    */
   public :

    const ll mod = 1e9 + 7;

    ll add(ll x, ll y) {
        x += y;
        if (x >= mod) return x - mod;
        return x;
    }
    ll sub(ll x, ll y) {
        x -= y;
        if (x < 0) return x + mod;
        return x;
    }
    ll mult(ll x, ll y) {
        return (x * y) % mod;
    }
    ll bin_pow(ll x, ll p) {
        if (p == 0) return 1;
        if (p & 1) return mult(x, bin_pow(x, p - 1));
        return bin_pow(mult(x, x), p / 2);
    }
    ll rev(ll x) {
        return bin_pow(x, mod - 2);
    }
    ll fact(ll a) {
        ll S = 1;
        for (ll I = 1 ; I <= a; I++)S = mult(S, I);
        return S;
    }
    ll Combin(ll x, ll y) {
        return mult(fact(x), rev(mult(fact(sub(x, y)), fact(y))));
    } 
};
int main () {
    ios_base::sync_with_stdio(false), cin.tie(0);

    ll a;
    cin >> a ;
    if (a < 2) {
        cout << 0;
        return 0;
    }
    Combination cm;
    ll ans = cm.sub(cm.bin_pow(10, a), cm.mult(2, cm.bin_pow(9, a)));
    ans = cm.add(ans, cm.bin_pow(8, a));
    cout << ans << "\n";
}

Measured memory_bytes: 3564
#include<iostream>
#include<iomanip>
//#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cassert>
using namespace std;
typedef long long ll;
const int Qmax = 2e5;
int N, Q;
string S;
char T[Qmax], D[Qmax];

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

	int l=0, r=N-1;
	for(int i=Q-1; i>=0; i--){
		if(S[l]==T[i]&&D[i]=='L') l++;
		if(l>0&&S[l-1]==T[i]&&D[i]=='R') l--;
		if(S[r]==T[i]&&D[i]=='R') r--;
		if(r<N-1&&S[r+1]==T[i]&&D[i]=='L') r++;
	}

	// cout << r-l+1 << " " << l << " " << r << endl;
	cout << r-l+1 << endl;

	return 0;
}


Measured memory_bytes: 900
#include <string>
#include <iostream>

int main() {
  std::string X;
  std::cin >> X;
  int n = X.length();
  int ans = n;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (X[i] == 'S') ++cnt;
    else {
      if (cnt > 0) {
        --cnt;
        ans -= 2;
      }
    }
  }
  std::cout << ans << std::endl;
}




Measured memory_bytes: 640
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> 
#include <utility> 
#include <tuple> 
#include <cstdint> 
#include <cstdio> 
#include <map> 
#include <queue> 
#include <set> 
#include <stack> 
#include <deque> 
#include <unordered_map> 
#include <unordered_set> 
#include <bitset> 
#include <cctype> 
#include <cmath>
#include <iomanip>
#include <ctype.h>

using namespace std;
using ll = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

#define TR ","
#define fi first
#define se second
#define rep(i,N) for(ll i=0;i<(ll)N;++i)
#define repe(i,a,b) for(ll i=a;i<(ll)b;++i)
#define all(v) v.begin(), v.end()
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define INF 100000000

ll gcd(ll a, ll b) {
    if (a < b) swap(a, b);
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll lcm(ll x, ll y) {
    return x / gcd(x, y) * y;
}

ll waz = 76543217;

ll inv(ll x) {
    if (x == 1) { return 1; }
    if (x == 0) { return 0; }
    else {
        ll a = (waz - inv(waz % x) * (waz / x)) % waz;
        if (a >= 0) { return a; }
        else { return waz + a; }
    }
}

void printVector(const vector<int>& vec) {
    for (int value : vec) {
        cout << value << " ";
    }
    cout << endl;
}

vector<ll> p;

ll phi(ll x) {
    ll a = x;
    ll b = x;
    for (ll k = 0; k != p.size(); ++k) {
        if (a % p[k] == 0) {
            while (a % p[k] == 0) {
                a /= p[k];
            }
            b = b / p[k] * (p[k] - 1);
        }
        if (a == 1) { break; }
    }
    return b;
}

bool my_compare(pair<string, int> a, pair<string, int> b) {
    if (a.first != b.first) {
        return a.first < b.first;
    }

    if (a.second != b.second) {
        return a.second > b.second;
    }
    else {
        return true;
    }
}

int main()
{
    IO;

    ll n; cin >> n;
    vector<ll> a(n);
    rep(i, n) a[i] = i + 1;

    do {
    rep(i, a.size() - 1) {
        cout << a[i] << " ";
    }
    cout << a[a.size() - 1] << endl;
    } while (next_permutation(all(a)));
}


Measured memory_bytes: 3176
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int[] s = new int[N];
    for (int i = 0; i < N; i++) {
      s[i] = sc.nextInt();
    }
    int sum = 0;
    for (int i = 0; i < N; i++) {
      sum += s[i];
    }
    Arrays.sort(s);
    if (sum % 10 == 0) {
      for (int i = 0; i < N; i++) {
        if ((sum - s[i]) % 10 != 0) {
          System.out.println(sum - s[i]);
          return;
        }
      }
      System.out.println(0);
    } else {
      System.out.println(sum);
    }
  }
}

Measured memory_bytes: 21716
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010


int main(){
  int a,b;

  cin >> a >> b;

  if(a+b < 10){
    cout << a+b << endl;
  }else{
    cout << "error" << endl;
  }
  
  return 0;
}


Measured memory_bytes: 256
<?php
while (true) {
        fscanf(STDIN, "%d %d", $h, $w);
        if ($h === 0 && $w === 0) break;
        echo str_repeat("#",$w),PHP_EOL;
        for ($i=$h-2;$i>0;$i--)
                echo "#",str_repeat(".",$w-2),"#",PHP_EOL;
        echo str_repeat("#",$w),PHP_EOL;
        echo PHP_EOL;
}

Measured memory_bytes: 15100
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

signed main()
{
    int n;
    cin>>n;
    vector<int>a(n);
    vector<pair<int,int>>b(n);
    REP(i,n) {
        cin >> a[i];
        b[i].first=a[i];
        b[i].second=i;
    }
    sort(ALL(b));
    vector<int>c(n+1,0);
    for (int i = 0; i < n; i++)
    {
        c[b[i].first] =b[i].second;  
    }
    set<int>st;
    st.insert(-1);
    st.insert(n);
    int ans=0;
    show(c);
    for (int i = 0; i < n; i++)
    {
        st.insert(c[i+1]);
        auto itr=st.find(c[i+1]);
        if(itr== -- st.end()){
            itr--;
            auto itr3 = itr;
            itr++;
            ans+=(i+1)*(*itr-*itr3 );
         //   cerr<<ans<<endl;
            continue;
        }
        if(itr==st.begin()){
            itr++;
            auto itr3 = itr;
            itr--;
            ans+=(i+1)*(*itr3-*itr);
         //   cerr<<ans<<endl;
            continue; 
        }
        itr--;
        auto itr1=itr;
        itr++,itr++;
        auto itr2=itr;
        itr--;
       // cerr << *itr <<*itr1<<*itr2<< endl;
        ans+=(i+1)*(*itr2 - *itr)*(*itr - *itr1);
     //   cerr<<ans<<endl;

    }
    cout<<ans<<endl;
    
  

    
    return 0;
}

Measured memory_bytes: 15844
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string.h>
#include <set>
#include <algorithm>
using namespace std;
const int N = 1e5 + 9;
typedef long long LL;
LL a[N], b[N];
int n;
void solve(){
    for (int i = 0 ; i < n ; ++i) scanf("%lld" , &a[i]);
    LL ans = 0;
    LL now = 0;
    sort(a , a + n);
    int u , v;
    u = v = n / 2;
    b[u] = a[0];
    int p = 0;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[++p];
        v += 2;
        if (v < n) b[v] = a[++p];
    }
    u = v = n / 2 - 1;
    b[u] = a[n - 1];
    p = n - 1;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[--p];
        v += 2;
        if (v < n) b[v] = a[--p];
    }

    now = 0;
    for (int i = 1 ; i < n ; ++i){
        now += abs(b[i] - b[i - 1]);
        // cout << i << ' ' << now << ',';
    }
    // cout << endl ;

    ans = now;

    u = v = n / 2;
    b[u] = a[0];
    p = 0;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[++p];
        v += 2;
        if (v < n) b[v] = a[++p];
    }
    u = v = n / 2 + 1;
    b[u] = a[n - 1];
    p = n - 1;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[--p];
        v += 2;
        if (v < n) b[v] = a[--p];
    }

    now = 0;
    for (int i = 1 ; i < n ; ++i){
        now += abs(b[i] - b[i - 1]);
        // cout << i << ' ' << now << ',';
    }
    // cout << endl ;

    ans = max(ans , now);;

    // for (int i = 0 ; i < n ; ++i)
    //     cout << b[i] << ' ';
    // cout << endl;

    reverse(a , a + n);

    u = v = n / 2;
    b[u] = a[0];
    p = 0;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[++p];
        v += 2;
        if (v < n) b[v] = a[++p];
    }
    u = v = n / 2 - 1;
    b[u] = a[n - 1];
    p = n - 1;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[--p];
        v += 2;
        if (v < n) b[v] = a[--p];
    }

    now = 0;
    for (int i = 1 ; i < n ; ++i){
        now += abs(b[i] - b[i - 1]);
        //cout << i << ' ' << now << ',';
    }
    //cout << endl;
    //3 1 4 2

    ans = max(ans , now);

    u = v = n / 2;
    b[u] = a[0];
    p = 0;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[++p];
        v += 2;
        if (v < n) b[v] = a[++p];
    }
    u = v = n / 2 + 1;
    b[u] = a[n - 1];
    p = n - 1;
    for ( ; u >= 0 || v < n ; ){
        u -= 2;
        if (u >= 0) b[u] = a[--p];
        v += 2;
        if (v < n) b[v] = a[--p];
    }

    now = 0;
    for (int i = 1 ; i < n ; ++i){
        now += abs(b[i] - b[i - 1]);
        //cout << i << ' ' << now << ',';
    }
    //cout << endl;
    //3 1 4 2

    ans = max(ans , now);


    // for (int i = 0 ; i < n ; ++i)
    //    cout << b[i] << ' ';
    // cout << endl;

    cout << ans << endl;
}
int main(){
    while(~scanf("%d" , &n)) solve();
}

Measured memory_bytes: 1792
import java.util.Scanner;
public class Main {
  static final char SEARCH_DONE = '.';
  static final int[] dx = { -1, 0, 1, 0 };
  static final int[] dy = { 0, 1, 0, -1 };
  static int h, w;
  static char[][] map;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (true) {
      h = sc.nextInt();
      w = sc.nextInt();
      if (h == 0 && w == 0) {
        break;
      }
      map = new char[h][];
      for (int i = 0; i < h; i++) {
        map[i] = sc.next().toCharArray();
      }
      int count = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          char current = map[i][j];
          if (current != SEARCH_DONE) {
            depthFirstSearch(i, j, current);
            count++;
          }
        }
      }
      System.out.println(count);
    }
  }
  private static void depthFirstSearch(int y, int x, char target) {
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
        continue;
      }
      if (map[ny][nx] == target) {
        map[ny][nx] = SEARCH_DONE;
        depthFirstSearch(ny, nx, target);
      }
    }
  }
}

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

#ifdef _DEBUG
#define debug(x) cerr << "line: " << __LINE__ << ", func: " << __func__ << " ->  " << #x << " = " << x << endl
#else
#define debug(x)
#endif

#define all(s) begin(s), end(s)
#define rall(s) rbegin(s), rend(s)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = ((a)-1); i >= (b); i--)
#define pb push_back
#define sz(a) int((a).size())
#define put(a) ((cout) << (a) << (endl))
#define putf(a, n) ((cout) << (fixed) << (setprecision(n)) << (a) << (endl))
#define deg2rad(x) (((x)*PI) / (180.0))
#define rad2deg(x) (((x) * (180.0)) / PI)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i_i = pair<int, int>;
using ll_ll = pair<ll, ll>;
using d_ll = pair<double, ll>;
using ll_d = pair<ll, double>;
using d_d = pair<double, double>;
template <class T> using vec = vector<T>;

static constexpr ll LL_INF = 1LL << 60;
static constexpr int I_INF = 1 << 28;
static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288);
static constexpr double EPS = numeric_limits<double>::epsilon();

static map<type_index, const char* const> scanType = {
    {typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}};

template <class T> static void scan(vector<T>& v);
[[maybe_unused]] static void scan(vector<string>& v, bool isWord = true);
template <class T> static inline bool chmax(T& a, T b);
template <class T> static inline bool chmin(T& a, T b);
template <class T> static inline T gcd(T a, T b);
template <class T> static inline T lcm(T a, T b);
template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val);
template <class T> T mod(T a, T m);

int main(int argc, char* argv[]) {
  ll sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;

  string ans = "";
  ans += string(tx - sx, 'R') + string(ty - sy, 'U');
  ans += string(tx - sx, 'L') + string(ty - sy, 'D');

  ans += "D" + string(tx - sx + 1, 'R') + string(ty - sy + 1, 'U') + "L";
  ans += "U" + string(tx - sx + 1, 'L') + string(ty - sy + 1, 'D') + "R";

  put(ans);
  return 0;
}

template <class T> static void scan(vector<T>& v) {
  auto tFormat = scanType[typeid(T)];
  for (T& n : v) {
    scanf(tFormat, &n);
  }
}

static void scan(vector<string>& v, bool isWord) {
  if (isWord) {
    for (auto& n : v) {
      cin >> n;
    }
    return;
  }

  int i = 0, size = v.size();
  string s;
  getline(cin, s);

  if (s.size() != 0) {
    i++;
    v[0] = s;
  }

  for (; i < size; ++i) {
    getline(cin, v[i]);
  }
}

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

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

template <class T> inline T gcd(T a, T b) { return __gcd(a, b); }

template <class T> inline T lcm(T a, T b) {
  T c = min(a, b), d = max(a, b);
  return c * (d / gcd(c, d));
}

template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) {
  std::fill((T*)arr, (T*)(arr + N), val);
}

template <class T> T mod(T a, T m) { return (a % m + m) % m; }


Measured memory_bytes: 256
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N, M = LI()
S = [LI()[1:] for i in range(M)]
ps = LI()

"""
何通りあるか
10までの制限なので、軽い
すべてのスイッチの通りは、2*10=1024通り
それぞれの場合について、最大10個見ればいいのでかんたん

"""
ans = 0
def dfs(sidx, switches):
    global ans
    # onの場合とoffの場合を検証していく
    if sidx == N:
        # 全スイッチの状態を決定できたので、判定する
        #  print('swi' , switches)
        for sidx, s in enumerate(S):
            # sはスイッチのリスト
            v = sum([switches[i-1] for i in s]) % 2
            if v == ps[sidx]:
                # ついてる
                pass
            else:
                # ついてないので止める
                return
        else:
            ans += 1
    else:
        dfs(sidx+1, switches + [0])
        dfs(sidx+1, switches + [1])
dfs(0, [])
print(ans)


Measured memory_bytes: 68712
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
  int N, A;
  cin >> N >> A;
  
  if(N < 1 || N > 10000)
  {
    cout << "N(first input) must be between 1 and 10000" << endl;
  	exit(1);
  }
  
  if(A < 0 || A > 1000)
  {
    cout << "A(second input) must be between 0 and 1000." << endl;
  	exit(1);
  }
  
  if(N % 500 <= A)
  {
  	cout << "Yes" << endl;
  }
  else
  {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
def answer(arr, k):
    arr = sorted(arr, key=lambda a:a[0])
    for a, b in arr:
        if k > b:
            k -= b
        else:
            return a

n, k = map(int, input().split())
arr = []
for i in range(n):
    a, b = map(int, input().split())
    arr.append((a, b))
print(answer(arr, k))

Measured memory_bytes: 59000
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(void) {
  int N;
  int *A;
  long int fee=0, res=0;

  cin >> N;
  A = (int *) malloc(sizeof(int) * (N+1));
  A[0] = 0;
  for (int i=1;i < N+1;i++) {
    cin >> A[i];
    fee += abs(A[i] - A[i-1]);
  }
  fee += abs(A[N]);

  for (int i=1;i < N+1;i++) {
    if (i == N) {
      res = fee - abs(A[N] - A[N-1]) + abs(A[N-1]) - abs(A[N]);
    } else {
      res = fee - abs(A[i] - A[i-1]) - abs(A[i+1] - A[i]) + abs(A[i-1] - A[i+1]);
    }
    cout << res << endl;
  }

  return 0;
}


Measured memory_bytes: 1664
#include <iostream>
#define Q 1000000007

using namespace std;
typedef long long LL;
LL fact(LL n){
  return n?(n*fact(n-1))%Q:1;
}
//LL abs(LL x){ return x>0?x:-x; }
int main(void){
  LL x,y;
  LL ans;
  cin >> x >> y;
  if (abs(x-y)>1){
    ans = 0;
  } else{
    ans = fact(x)*fact(y);
    ans %= Q;
    if (x==y) (ans*=2)%=Q;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define D double
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define ff first
#define ss second
#define SZ 100005
#define eps 1e-8
#define PI (2*acos(0.0))
#define mem(a, b) memset(a, b, sizeof a)
#define FastIO ios_base::sync_with_stdio(0)
const int inf = 0x7f7f7f7f;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
//int dx[]={2,1,-1,-2,-2,-1,1,2};
//int dy[]={1,2,2,1,-1,-2,-2,-1};
//int mnth[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    //FastIO;
    int n;
    while(sf("%d", &n)==1)
    {
        int sum = 0, i;
        for(i=1; ; i++) {
            sum += i;
            if(sum>=n) break;
        }

        int a = ((i-1)*i)/2;
        int b = n - a;

        //cout<<i<<endl;
        //cout<<b<<endl;

        int open = 0, close = 0;
        string s;
        for(int j=1; j<=b; j++) {
            s += ")"; close++;
        }

        if(open<i && close<i) {
            s += "()"; open++; close++;
        }

        for(int j=close+1; j<=i; j++) s += ")";
        for(int j=open+1; j<=i; j++) s += "(";

        cout<<s<<endl;
    }
    return 0;
}

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

#define for(i, a, b) for(ll (i)=a;(i)<(b);++(i))
#define rep(i, n)    for(i, 0, n)
#define sz(x)        ((ll)(x).size())

const ll inf = 1 << 25;
const ll mod = 1e9 + 7;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
    ll N, M;
    cin >> N >> M;
    
    ll ans = 0;
    if (M >= 2) {
        if (M >= N * 2) {
            M -= 2 * N;
            ans += N;
            
            ans += M / 4;
        } else {
            ans = M / 2;
        }
    }
    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 384
object Main {

  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)

    val N = sc.nextInt()
    val W = Array.fill(N)(sc.nextInt())

    var S1 = W.head
    var S2 = W.tail.sum
    var diff = Math.abs(S1 - S2)
    var i = 1
    while (i < N - 1) {
      val v = W(i)
      S1 += v
      S2 -= v
      diff = Math.min(diff, Math.abs(S1 - S2))
      i += 1
    }
    println(diff)
  }

}


Measured memory_bytes: 25912
#include <bits/stdc++.h>
using namespace std;
 
int main() {
 int N;
 cin >> N;
 int count = 0; 
  for(int i = 1;i <= N; i++){
    for(int j = 1;j <= N; j++){
      for(int k = 1;k <= N; k++){
      //  cout << "i:" << i << "j:" << j << "k:" << k << endl;
        count++;
      }
    }
  }
  cout << count << endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <numeric>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long long ull;
typedef vector<int> vi;

int main()
{
	string o, e;
	cin >> o >> e;
	for (int i = 0; i < o.length() + e.length(); i++) {
		if (i % 2 == 0)cout << o[i/2];
		else cout << e[i / 2];
	}
	cout << endl;

	return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int X = sc.nextInt(), A = sc.nextInt(), B = sc.nextInt();
		int ans = X - A;
		while (ans >= B) {
			ans -= B;
		}
		System.out.println(ans);
		sc.close();
	}
}


Measured memory_bytes: 22100
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	
	public static void main(String[] args) {

		try {
	
			BufferedReader questionreader = new BufferedReader(new InputStreamReader(System.in));
			PrintWriter answerwriter = new PrintWriter(System.out);

			String[] params = null;
			String question = null;

			while (questionreader.ready()) {
				if ("".equals(question = questionreader.readLine())) break;
				params = question.split(" ");
				answerwriter.println(Integer.parseInt(params[0])+Integer.parseInt(params[1]));
			}
			
			answerwriter.close();
			questionreader.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}
}

Measured memory_bytes: 9816
#include<iostream>
#include<iomanip>
#include<cmath>

//関数の宣言
double cipManhattan(int n, int* xpt, int* ypt);
double cipEuclid(int n, int* xpt, int* ypt);
double cipP3(int n, int* xpt, int* ypt);
double cipChebyshev(int n, int* xpt, int* ypt);

int main(){

	int n;
	int* xpt;
	int* ypt;
	
	double manhattan;
	double euclid;
	double p3;
	double chebyshev;

	std::cin >> n;

	xpt = new int[n];
	ypt = new int[n];

	for (int i = 0; i < n; ++i){
		std::cin >> xpt[i];
	}
	for (int i = 0; i < n; ++i){
		std::cin >> ypt[i];
	}

	manhattan = cipManhattan(n, xpt, ypt);
	euclid = cipEuclid(n, xpt, ypt);
	p3 = cipP3(n, xpt, ypt);
	chebyshev = cipChebyshev(n, xpt, ypt);

	std::cout << std::fixed << std::setprecision(10) << manhattan << std::endl << euclid << std::endl << p3 << std::endl << chebyshev << std::endl;

	return 0;
}

//関数の定義
double cipManhattan(int n, int* xpt, int* ypt){
	double difference=0;
	for (int i = 0; i < n; ++i){
		difference += std::abs(xpt[i] - ypt[i]);
	}
	return difference;
}

double cipEuclid(int n, int* xpt, int* ypt){
	double difference=0;
	double ans;
	double div;
	for (int i = 0; i < n; ++i){
		difference += std::pow(std::abs(xpt[i] - ypt[i]),2.0);
	}
	div = 1.0 / 2.0;
	ans=std::pow(difference,div);
	return ans;
}

double cipP3(int n, int* xpt, int* ypt){
	double difference=0;
	for (int i = 0; i < n; ++i){
		difference += std::pow(std::abs(xpt[i] - ypt[i]), 3.0);
	}
	return std::pow(difference, 1.0 / 3.0);
}

double cipChebyshev(int n, int* xpt, int* ypt){
	int tmp;
	int max=0;
	for (int i = 0; i < n; ++i){
		tmp = std::abs(xpt[i] - ypt[i]);
		if (max < tmp){
			max = tmp;
		}
	}
	return max;
}


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

typedef long long ll;

#define pb push_back
#define MS(x,y) memset((x),(y),sizeof((x)))
#define mn 1000000

using namespace std;

int niza[100001];
int n;
int dp[100001];
int k;

int solve (int i) {
    if (dp[i] != -1) return dp[i];
    if (i >= n-1) return 0;

    int ret = INT_MAX;

    for (int ctr = i+1; ctr<=min(n-1,i+k); ctr++) {
        ret = min(ret, abs(niza[i]-niza[ctr])+solve(ctr));
    }

    return dp[i] = ret;
}

int main()
{
    #if LOCAL_DEBUG
        fstream cin("in.txt");
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>k;

    for (int i = 0; i<n; i++) cin>>niza[i];
    for (int i = 0; i<n; i++) dp[i] = -1;

    cout<<solve(0)<<endl;

    return 0;
}


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

using namespace std;

#define int long long
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}

typedef long long ll;
typedef unsigned long long ul;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, VL, greater<ll>> PQG;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18 + 1;
const ll dx[] = {1, -1, 0, 0};
const ll dy[] = {0, 0, 1, -1};

ll N;
VL A;
vector<PL> ope;

signed main() {
    cin >> N;
    REP(i, N) A.emplace_back(IN());
    ll sum = 0;
    sort(All(A));
    ll bo = lower_bound(All(A), 0) - A.begin();
    if(bo == N) bo = N - 1;
    if(bo == 0) bo = 1;
    FOR(i, bo, N - 1) {
        ope.emplace_back(A[0], A[i]);
        A[0] -= A[i];
    }
    REP(i, bo) {
        ope.emplace_back(A[N - 1], A[i]);
        A[N - 1] -= A[i];
    }
    cout << A[N - 1] << "\n";
    REP(i, ope.size()) { cout << ope[i].first << " " << ope[i].second << "\n"; }
}


Measured memory_bytes: 4336
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<x;l=m;else;r=m;end;end;l;end

function main()
	n = readline() |> parseInt
	a = readline() |> split |> parseMap |> sort
	k = 0
	for i in 1:n-1
		for j in i+1:n
			l = bsearch(a,a[j]-a[i])
			r = bsearch(a,a[j]+a[i])
			if j<l
				k += r-l-1
			elseif j<r
				k += r-j
			end
		end
	end
	println(k)
end

main()

Measured memory_bytes: 113336
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  loop(i,0,n)
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define vi vector<int>
#define vl vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>


int main(){
    int n,k;
    cin>>n>>k;
    vi p(n);
    vector<double> e(n);
    vector<long double> s;
    long double ans=0;
    rep(i,1000){
      long double dtmp=0;
      rep(j,i+1){
        dtmp += 1.0*(j+1)/(i+1);
      }
      s.push_back(dtmp);
    }

    rep(i,n){
      int tmp;
      cin>>tmp;
      p[i]=tmp;
      e[i]=s[tmp-1];
    }

    long double stmp=0;
    rep(i,k){
      stmp+=e[i];
    }
  
  ans = stmp;
    
    rep(i,n){
      
      if(i+k<n){
        stmp = stmp-e[i]+e[i+k];
        ans = max(ans,stmp);
      }
      else{
        break;
      }
    }

    cout << fixed;

    cout<<setprecision(8)<<ans<<endl;
}

Measured memory_bytes: 2688
/*
S[r] - S[l] = r - l 
S[r] - r = S[l] - l
*/

#include <cstdio>
#include <algorithm>
#include <map>

typedef long long int64;

const int MAXN = 200000 + 10;

int n, k;
int64 ans;

std::map<int, int> mp;
int a[MAXN], s[MAXN];

int main() {
	int i;
	
	scanf("%d %d", &n, &k);
	for(i=1; i<=n; i++) {
		scanf("%d", &s[i]);
		s[i] = (s[i] + s[i - 1]) % k;
	}
	
	for(i=1; i<=n; i++)
		s[i] = (k + (s[i] - i) % k) % k;
	
	mp[0] = 1;
	for(i=1; i<=n; i++) {
		if(i >= k)
			mp[s[i - k]]--;
		ans += mp[s[i]];
		mp[s[i]]++;
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 10496
main = interact $ show' . solve . read' . lines
  where
    solve = map solveRow
    solveRow [n, x] = sum [1 | a <- [1..n-2], b <- [a+1..n-1], c <- [b+1..n], a + b + c == x]
    show' = unlines . map show
    read' = map (map read . words) . init :: [String] -> [[Int]]


Measured memory_bytes: 5060
#include <iostream>
#include <string>
#include <algorithm>
#include <utility> 
#include <unordered_set>
#include <vector>
#include <list> 
#include <string>
#include <iterator> 
#include <deque>
#include <array>
#include <forward_list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <bitset>
#include <numeric>
using namespace std;


const long double PI = 3.14159265358979323846264338327950;
const double EPS = 1e-9;
const int N = 200001;

template <class T> T gcd(T a, T b) {
	if (b == 0) { return a; }
	return gcd(b, a % b);
}

template <class T> bool isPrime(T x) {
	for (T d = 2; d * d <= x; d++) { if (x % d == 0) return false; }
	return true;
}

struct dummy{
	int cnt, num;
};

dummy arr[N] = {};

bool sort_pd(dummy& L, dummy& R) {
	return L.cnt < R.cnt;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int n, k, m;
	int cnt;
	cin >> n >> k;

	cnt = 0;
	for (int i = 0; i < n; ++i) {
		cin >> m;
		++arr[m].cnt;
		arr[m].num = m;
	}

	for (int i = 1; i <= N; ++i) {
		if (arr[i].cnt) {
			++cnt;
		}
	}

	int ans = 0;
	if (cnt <= k) {
		cout << ans << '\n';
	} else {
		sort(arr, arr + N, sort_pd);
		for (int i = 1; i <= N; ++i) {
			// cout << arr[i].num << '\n';
			if (arr[i].cnt) {
				ans += arr[i].cnt;
				cnt--;

				if(cnt <= k)
				break;
			}
		}
		cout << ans << '\n';
	}

}

Measured memory_bytes: 5140
#include <iostream>
#include <algorithm>
#define llint long long

using namespace std;

llint N, C;
llint x[100005];
llint v[100005];
llint sum[100005], maxsum[100005];

int main(void)
{
	cin >> N >> C;
	for(int i = 1; i <= N; i++) cin >> x[i] >> v[i];
	
	for(int i = 1; i <= N; i++){
		sum[i] = sum[i-1] + v[i] - (x[i] - x[i-1]);
		maxsum[i] = max(maxsum[i-1], sum[i]);
	}
	llint ans = 0, now = 0;
	ans = max(ans, maxsum[N]);
	for(int i = N; i > 0; i--){
		now += v[i];
		ans = max(ans, now - 2*(C-x[i]) + maxsum[i-1]);
	}
	
	for(int i = 1; i <= N; i++) x[i] = C - x[i];
	reverse(x+1, x+N+1);
	reverse(v+1, v+N+1);
	
	for(int i = 1; i <= N; i++){
		sum[i] = sum[i-1] + v[i] - (x[i] - x[i-1]);
		maxsum[i] = max(maxsum[i-1], sum[i]);
	}
	now = 0;
	ans = max(ans, maxsum[N]);
	for(int i = N; i > 0; i--){
		now += v[i];
		ans = max(ans, now - 2*(C-x[i]) + maxsum[i-1]);
	}
	
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3328
import java.io.BufferedReader; 
import java.io.*;
import java.io.InputStreamReader; 
import java.util.Scanner; 
import java.util.StringTokenizer; 
import java.util.*;
import java.math.*;
public class Main 
{ 
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader()
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next()
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine();
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
   public static void main(String args[])
    {
        FastReader sc=new FastReader();
        PrintWriter pw=new PrintWriter(System.out);
        int t=1;//sc.nextInt();
        long ans=0,mod=1000000007;
        while(t-->0)
        {
         int n=sc.nextInt();long a[]=new long[n];
         Map<Long,Long> mp=new HashMap<>();
         for(int i=0;i<n;i++)
         mp.merge(a[i]=sc.nextLong(),1l,Long::sum);
         long to=0;
         for(long key:mp.keySet())
         {
           long v=mp.get(key);
           to+=(v*(v-1))/2;
         }
         for(long w:a)
         {
             long v=mp.get(w);
             pw.println(to- v*(v-1)/2 + (v-1)*(v-2)/2);
         }
        }
           pw.close();
    }
}
  

Measured memory_bytes: 68492
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <queue>
#include <tuple>
#include <functional>
#include <cstring>

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007 
#define irep(i,n) for(int  i = 0; i < n; i++)
#define irep2(i,a,n) for(int i = a; i <= n; i++)
#define lrep(i,n) for(long long  i = 0; i < n; i++)
#define lrep2(i,a,n) for(long long i = a; i <= n; i++)

typedef long long ll;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int> > v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll> > v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string> > v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool> > v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main(void)
{
    int n, m;
    cin >> n >> m;

    v_int p(m);
    v_string s(m);

    v_int wa_n(100001, 0);
    v_bool ac_flg(100001, false);

    irep(i, m) {
        int p;
        string s;
        cin >> p >> s;
        if(s == "WA" && ac_flg[p] == false) { wa_n[p]++; }
        if(s == "AC") { ac_flg[p] = true; } 
    }
    
    ll ac_cnt = 0; 
    ll pena_cnt = 0;
    irep2(i, 1, 100000) {
        if(ac_flg[i]) {
            ac_cnt++;
            if(wa_n[i] != 0) {
                pena_cnt += wa_n[i];
            }
        }
    }
    cout << ac_cnt << " " << pena_cnt << endl;
    return 0;
}
/** atcodr **/


Measured memory_bytes: 1792
# vim: fileencoding=utf-8


def main():

    n = int(input())
    li = []
    for i in range(n):
        x, y, h = map(int, input().split())
        li.append({"x": x, "y": y, "h": h})
    li.sort(key=lambda x: x["h"], reverse=True)

    tops = []
    for i in range(101):
        for j in range(101):
            h = li[0]["h"] + abs(li[0]["x"] - i) + abs(li[0]["y"] - j)
            tops.append({"x": i, "y": j, "h": h})

    for i in range(1, n):
        tmp = []
        for j in range(len(tops)):
            x = li[i]["x"]
            y = li[i]["y"]
            h = li[i]["h"]
            topX = tops[j]["x"]
            topY = tops[j]["y"]
            topH = tops[j]["h"]
            if h == max(topH - abs(topX - x) - abs(topY - y), 0):
                tmp.append(tops[j])
        tops = tmp
    print(f"{tops[0]['x']} {tops[0]['y']} {tops[0]['h']}")


if __name__ == "__main__":
    main()


Measured memory_bytes: 77016
n = int(input())
f = [int(input().replace(" ", ""), 2) for i in range(n)]
p = [list(map(int, input().split())) for i in range(n)]
ans = -float('inf')
for i in range(1, 2 ** 10):
  sum = 0
  for j in range(n):
    sum += p[j][bin(i & f[j]).count('1')]
  ans = max(ans, sum)
print(ans)

Measured memory_bytes: 9152