One day, BThero decided to play around with arrays and came up with the following problem:You are given an array $$$a$$$, which consists of $$$n$$$ positive integers. The array is numerated $$$1$$$ through $$$n$$$. You execute the following procedure exactly once:  You create a new array $$$b$$$ which consists of $$$2n$$$ positive integers, where for each $$$1 \le i \le n$$$ the condition $$$b_{2i-1}+b_{2i} = a_i$$$ holds. For example, for the array $$$a = [6, 8, 2]$$$ you can create $$$b = [2, 4, 4, 4, 1, 1]$$$.  You merge consecutive equal numbers in $$$b$$$. For example, $$$b = [2, 4, 4, 4, 1, 1]$$$ becomes $$$b = [2, 4, 1]$$$. Find and print the minimum possible value of $$$|b|$$$ (size of $$$b$$$) which can be achieved at the end of the procedure. It can be shown that under the given constraints there is at least one way to construct $$$b$$$.

The first line of the input file contains a single integer $$$T$$$ ($$$1 \le T \le 5 \cdot 10^5$$$) denoting the number of test cases. The description of $$$T$$$ test cases follows. The first line of each test contains a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$). The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$2 \le a_i \le 10^9$$$). It is guaranteed that $$$\sum{n}$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

For each test case, print a single line containing one integer — the minimum possible value of $$$|b|$$$.

#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define N	500000
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X;

void srand_() {
	struct timeval tv;

	gettimeofday(&tv, NULL);
	X = tv.tv_sec ^ tv.tv_usec;
}

int rand_() {
	return (X *= 3) >> 1;
}

long long ss[N + 1];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = ss[ii[j]] != ss[i_] ? ss[ii[j]] - ss[i_] : ii[j] - i_;

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int search(int *ii, int n, long long s) {
	int lower = -1, upper = n;

	while (upper - lower > 1) {
		int h = (lower + upper) / 2;

		if (ss[ii[h]] >= s)
			upper = h;
		else
			lower = h;
	}
	return lower;
}

int ft1[N + 1], ft2[N + 1];

void update(int *ft, int i, int n, int x) {
	while (i < n) {
		ft[i] = min(ft[i], x);
		i |= i + 1;
	}
}

int query(int *ft, int i) {
	int x = INF;

	while (i >= 0) {
		x = min(x, ft[i]);
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	int t;

	srand_();
	scanf("%d", &t);
	while (t--) {
		static int aa[N + 1], qu[2][N + 1], head[2], cnt[2], ii[N], ll[N + 1], ll_[N + 1], rr_[N + 1], pp[N + 1], dp[N + 1], dq[N + 1];
		int n, h, h_, i, j;
		long long sum;

		scanf("%d", &n);
		for (i = 1; i <= n; i++)
			scanf("%d", &aa[i]), aa[i] -= 2;
		for (i = 1; i <= n; i++)
			ss[i] = aa[i] - (i == 0 ? 0 : ss[i - 1]);
		head[0] = cnt[0] = head[1] = cnt[1] = 0;
		for (i = 0, j = 0; j <= n; j++) {
			while (cnt[j % 2] && ss[qu[j % 2][head[j % 2] + cnt[j % 2] - 1]] >= ss[j])
				cnt[j % 2]--;
			qu[j % 2][head[j % 2] + cnt[j % 2]++] = j;
			while (i < j && ss[qu[0][head[0]]] + ss[qu[1][head[1]]] < 0) {
				if (qu[i % 2][head[i % 2]] == i)
					head[i % 2]++, cnt[i % 2]--;
				i++;
			}
			ll[j] = i;
			if (j > 0 && aa[j] % 2 == 0) {
				h = search(qu[j % 2] + head[j % 2], cnt[j % 2], ss[j] - aa[j] / 2);
				ll_[j] = h == -1 ? i : qu[j % 2][head[j % 2] + h] + 1;
				h = search(qu[j % 2 ^ 1] + head[j % 2 ^ 1], cnt[j % 2 ^ 1], aa[j] / 2 - ss[j]);
				ll_[j] = max(ll_[j], h == -1 ? i : qu[j % 2 ^ 1][head[j % 2 ^ 1] + h] + 1);
			}
		}
		cnt[0] = cnt[1] = 0;
		for (i = n; i > 0; i--) {
			while (cnt[i % 2] && ss[qu[i % 2][cnt[i % 2] - 1]] >= ss[i])
				cnt[i % 2]--;
			qu[i % 2][cnt[i % 2]++] = i;
			if (i > 0 && aa[i] % 2 == 0) {
				h = search(qu[i % 2], cnt[i % 2], ss[i] - aa[i] / 2);
				rr_[i] = h == -1 ? n : qu[i % 2][h] - 1;
				h = search(qu[i % 2 ^ 1], cnt[i % 2 ^ 1], aa[i] / 2 - ss[i]);
				rr_[i] = min(rr_[i], h == -1 ? n : qu[i % 2 ^ 1][h] - 1);
			}
		}
		sum = 0;
		for (i = 1; i <= n; i++) {
			ss[i] = sum + (i % 2 == 0 ? aa[i] / 2 : -aa[i] / 2);
			sum += i % 2 == 0 ? aa[i] : -aa[i];
			ii[i - 1] = i;
		}
		sort(ii, 0, n);
		for (h = 0; h < n; h = h_) {
			long long s = ss[ii[h]];

			h_ = h, i = 0;
			while (h_ < n && ss[ii[h_]] == s) {
				if (aa[ii[h_]] % 2 == 0)
					pp[ii[h_]] = i, i = ii[h_];
				else
					pp[ii[h_]] = 0;
				h_++;
			}
		}
		memset(ft1, 0x3f, (n + 1) * sizeof *ft1);
		memset(ft2, 0x3f, (n + 1) * sizeof *ft2);
		update(ft1, n - 0, n + 1, 0);
		for (i = 1; i <= n; i++) {
			dp[i] = min(query(ft2, n - i), query(ft1, n - ll[i]) + 1);
			if (aa[i] % 2 == 0) {
				dq[i] = query(ft1, n - ll_[i]);
				if (pp[i] && ll_[i] <= pp[i])
					dq[i] = min(dq[i], dq[pp[i]] - 1);
			} else
				dq[i] = INF;
			update(ft1, n - i, n + 1, dp[i]);
			if (dq[i] != INF) {
				update(ft1, n - i, n + 1, dq[i]);
				update(ft2, n - rr_[i], n + 1, dq[i]);
			}
		}
		printf("%d\n", n + min(dp[n], dq[n]));
	}
	return 0;
}

#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define N	500000
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X;

void srand_() {
	struct timeval tv;

	gettimeofday(&tv, NULL);
	X = tv.tv_sec ^ tv.tv_usec;
}

int rand_() {
	return (X *= 3) >> 1;
}

long long ss[N + 1];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (ss[ii[j]] == ss[i_])
				j++;
			else if (ss[ii[j]] < ss[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int search(int *ii, int n, long long s) {
	int lower = -1, upper = n;

	while (upper - lower > 1) {
		int h = (lower + upper) / 2;

		if (ss[ii[h]] >= s)
			upper = h;
		else
			lower = h;
	}
	return lower;
}

int ft1[N + 1], ft2[N + 1];

void update(int *ft, int i, int n, int x) {
	while (i < n) {
		ft[i] = min(ft[i], x);
		i |= i + 1;
	}
}

int query(int *ft, int i) {
	int x = INF;

	while (i >= 0) {
		x = min(x, ft[i]);
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	int t;

	srand_();
	scanf("%d", &t);
	while (t--) {
		static int aa[N + 1], qu[2][N + 1], head[2], cnt[2], ii[N], ll[N + 1], ll_[N + 1], rr_[N + 1], pp[N + 1], dp[N + 1], dq[N + 1];
		int n, h, h_, i, j;
		long long sum;

		scanf("%d", &n);
		for (i = 1; i <= n; i++)
			scanf("%d", &aa[i]), aa[i] -= 2;
		for (i = 1; i <= n; i++)
			ss[i] = aa[i] - (i == 0 ? 0 : ss[i - 1]);
		head[0] = cnt[0] = head[1] = cnt[1] = 0;
		for (i = 0, j = 0; j <= n; j++) {
			while (cnt[j % 2] && ss[qu[j % 2][head[j % 2] + cnt[j % 2] - 1]] >= ss[j])
				cnt[j % 2]--;
			qu[j % 2][head[j % 2] + cnt[j % 2]++] = j;
			while (ss[qu[0][head[0]]] + ss[qu[1][head[1]]] < 0) {
				if (qu[i % 2][head[i % 2]] == i)
					head[i % 2]++, cnt[i % 2]--;
				i++;
			}
			ll[j] = i;
			if (j > 0 && aa[j] % 2 == 0) {
				h = search(qu[j % 2] + head[j % 2], cnt[j % 2], ss[j] - aa[j] / 2);
				ll_[j] = h == -1 ? 0 : qu[j % 2][head[j % 2] + h] + 1;
				h = search(qu[j % 2 ^ 1] + head[j % 2 ^ 1], cnt[j % 2 ^ 1], aa[j] / 2 - ss[j]);
				ll_[j] = max(ll_[j], h == -1 ? 0 : qu[j % 2 ^ 1][head[j % 2 ^ 1] + h] + 1);
			}
		}
		cnt[0] = cnt[1] = 0;
		for (i = n; i > 0; i--) {
			while (cnt[i % 2] && ss[qu[i % 2][cnt[i % 2] - 1]] >= ss[i])
				cnt[i % 2]--;
			qu[i % 2][cnt[i % 2]++] = i;
			if (i > 0 && aa[i] % 2 == 0) {
				h = search(qu[i % 2], cnt[i % 2], ss[i] - aa[i] / 2);
				rr_[i] = h == -1 ? n : qu[i % 2][h] - 1;
				h = search(qu[i % 2 ^ 1], cnt[i % 2 ^ 1], aa[i] / 2 - ss[i]);
				rr_[i] = min(rr_[i], h == -1 ? n : qu[i % 2 ^ 1][h] - 1);
			}
		}
		sum = 0;
		for (i = 1; i <= n; i++) {
			ss[i] = sum + (i % 2 == 0 ? aa[i] / 2 : -aa[i] / 2);
			sum += i % 2 == 0 ? aa[i] : -aa[i];
			ii[i - 1] = i;
		}
		sort(ii, 0, n);
		for (h = 0; h < n; h = h_) {
			long long s = ss[ii[h]];

			h_ = h, i = 0;
			while (h_ < n && ss[ii[h_]] == s) {
				if (aa[ii[h_]] % 2 == 0)
					pp[ii[h_]] = i, i = ii[h_];
				else
					pp[ii[h_]] = 0;
				h_++;
			}
		}
		memset(ft1, 0x3f, (n + 1) * sizeof *ft1);
		memset(ft2, 0x3f, (n + 1) * sizeof *ft2);
		update(ft1, n - 0, n + 1, 0);
		for (i = 1; i <= n; i++) {
			dp[i] = min(query(ft2, n - i), query(ft1, n - ll[i]) + 1);
			if (aa[i] % 2 == 0) {
				dq[i] = query(ft1, n - ll_[i]);
				if (pp[i] && ll_[i] <= pp[i])
					dq[i] = min(dq[i], dq[pp[i]] - 1);
			} else
				dq[i] = INF;
			update(ft1, n - i, n + 1, dp[i]);
			if (dq[i] != INF) {
				update(ft1, n - i, n + 1, dq[i]);
				update(ft2, n - rr_[i], n + 1, dq[i]);
			}
		}
		printf("%d\n", n + min(dp[n], dq[n]));
	}
	return 0;
}

#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define N	500000
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X;

void srand_() {
	struct timeval tv;

	gettimeofday(&tv, NULL);
	X = tv.tv_sec ^ tv.tv_usec;
}

int rand_() {
	return (X *= 3) >> 1;
}

long long ss[N + 1];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = ss[ii[j]] != ss[i_] ? ss[ii[j]] - ss[i_] : ii[j] - i_;

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int search(int *ii, int n, long long s) {
	int lower = -1, upper = n;

	while (upper - lower > 1) {
		int h = (lower + upper) / 2;

		if (ss[ii[h]] >= s)
			upper = h;
		else
			lower = h;
	}
	return lower;
}

int ft1[N + 1], ft2[N + 1];

void update(int *ft, int i, int n, int x) {
	while (i < n) {
		ft[i] = min(ft[i], x);
		i |= i + 1;
	}
}

int query(int *ft, int i) {
	int x = INF;

	while (i >= 0) {
		x = min(x, ft[i]);
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	int t;

	srand_();
	scanf("%d", &t);
	while (t--) {
		static int aa[N + 1], qu[2][N + 1], head[2], cnt[2], ii[N], ll[N + 1], ll_[N + 1], rr_[N + 1], pp[N + 1], dp[N + 1], dq[N + 1];
		int n, h, h_, i, j;
		long long sum;

		scanf("%d", &n);
		for (i = 1; i <= n; i++)
			scanf("%d", &aa[i]), aa[i] -= 2;
		for (i = 1; i <= n; i++)
			ss[i] = aa[i] - (i == 0 ? 0 : ss[i - 1]);
		head[0] = cnt[0] = head[1] = cnt[1] = 0;
		for (i = 0, j = 0; j <= n; j++) {
			while (cnt[j % 2] && ss[qu[j % 2][head[j % 2] + cnt[j % 2] - 1]] >= ss[j])
				cnt[j % 2]--;
			qu[j % 2][head[j % 2] + cnt[j % 2]++] = j;
			while (ss[qu[0][head[0]]] + ss[qu[1][head[1]]] < 0) {
				if (qu[i % 2][head[i % 2]] == i)
					head[i % 2]++, cnt[i % 2]--;
				i++;
			}
			ll[j] = i;
			if (j > 0 && aa[j] % 2 == 0) {
				h = search(qu[j % 2] + head[j % 2], cnt[j % 2], ss[j] - aa[j] / 2);
				ll_[j] = h == -1 ? 0 : qu[j % 2][head[j % 2] + h] + 1;
				h = search(qu[j % 2 ^ 1] + head[j % 2 ^ 1], cnt[j % 2 ^ 1], aa[j] / 2 - ss[j]);
				ll_[j] = max(ll_[j], h == -1 ? 0 : qu[j % 2 ^ 1][head[j % 2 ^ 1] + h] + 1);
			}
		}
		cnt[0] = cnt[1] = 0;
		for (i = n; i > 0; i--) {
			while (cnt[i % 2] && ss[qu[i % 2][cnt[i % 2] - 1]] >= ss[i])
				cnt[i % 2]--;
			qu[i % 2][cnt[i % 2]++] = i;
			if (i > 0 && aa[i] % 2 == 0) {
				h = search(qu[i % 2], cnt[i % 2], ss[i] - aa[i] / 2);
				rr_[i] = h == -1 ? n : qu[i % 2][h] - 1;
				h = search(qu[i % 2 ^ 1], cnt[i % 2 ^ 1], aa[i] / 2 - ss[i]);
				rr_[i] = min(rr_[i], h == -1 ? n : qu[i % 2 ^ 1][h] - 1);
			}
		}
		sum = 0;
		for (i = 1; i <= n; i++) {
			ss[i] = sum + (i % 2 == 0 ? aa[i] / 2 : -aa[i] / 2);
			sum += i % 2 == 0 ? aa[i] : -aa[i];
			ii[i - 1] = i;
		}
		sort(ii, 0, n);
		for (h = 0; h < n; h = h_) {
			long long s = ss[ii[h]];

			h_ = h, i = 0;
			while (h_ < n && ss[ii[h_]] == s) {
				if (aa[ii[h_]] % 2 == 0)
					pp[ii[h_]] = i, i = ii[h_];
				else
					pp[ii[h_]] = 0;
				h_++;
			}
		}
		memset(ft1, 0x3f, (n + 1) * sizeof *ft1);
		memset(ft2, 0x3f, (n + 1) * sizeof *ft2);
		update(ft1, n - 0, n + 1, 0);
		for (i = 1; i <= n; i++) {
			dp[i] = min(query(ft2, n - i), query(ft1, n - ll[i]) + 1);
			if (aa[i] % 2 == 0) {
				dq[i] = query(ft1, n - ll_[i]);
				if (pp[i] && ll_[i] <= pp[i])
					dq[i] = min(dq[i], dq[pp[i]] - 1);
			} else
				dq[i] = INF;
			update(ft1, n - i, n + 1, dp[i]);
			if (dq[i] != INF) {
				update(ft1, n - i, n + 1, dq[i]);
				update(ft2, n - rr_[i], n + 1, dq[i]);
			}
		}
		printf("%d\n", n + min(dp[n], dq[n]));
	}
	return 0;
}


In the spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side.One example of a grid path is (0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → ( - 1, 1). Note that squares in this sequence might be repeated, i.e. path has self intersections.Movement within a grid path is restricted to adjacent squares within the sequence. That is, from the i-th square, one can only move to the (i - 1)-th or (i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is some j-th square of the path that coincides with the i-th square, only moves to (i - 1)-th and (i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares.To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence (0, 0) → (0, 1) → (0, 0) cannot occur in a valid grid path.One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble will try and move east as well. By try, we mean if moving east is a valid move, then the marble will move east.Moving north increases the second coordinate by 1, while moving south decreases it by 1. Similarly, moving east increases first coordinate by 1, while moving west decreases it.Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths.

The first line of the input contains a single integer n (2 ≤ n ≤ 1 000 000) — the length of the paths. The second line of the input contains a string consisting of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the first grid path. The characters can be thought of as the sequence of moves needed to traverse the grid path. For example, the example path in the problem statement can be expressed by the string "NNESWW". The third line of the input contains a string of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the second grid path.

Print "YES" (without quotes) if it is possible for both marbles to be at the end position at the same time. Print "NO" (without quotes) otherwise. In both cases, the answer is case-insensitive.

NoteIn the first sample, the first grid path is the one described in the statement. Moreover, the following sequence of moves will get both marbles to the end: NNESWWSWSW.In the second sample, no sequence of moves can get both marbles to the end.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

bool reversed(char c, char t)
{
    return (c == 'N' && t == 'S') || (c == 'S' && t == 'N') || (c == 'W' && t == 'E') || (c == 'E' && t == 'W');
}

char reverse(char c)
{
    if (c == 'S')
        return 'N';
    if (c == 'W')
        return 'E';
    if (c == 'N')
        return 'S';
    if (c == 'E')
        return 'W';
    return '\0';
}

int main()
{
    int n;
    assert(scanf("%d", &n) == 1);
    n--;
    char* s = malloc(sizeof(char) * (n + 1));
    char* t = malloc(sizeof(char) * (n + 1));
    assert(scanf("%s", s));
    assert(scanf("%s", t));
    if (reversed(s[n - 1], t[n - 1]))
        printf("NO\n");
        else
    {
        int i = 0;
        int j = 0;
        int ilast = 0;
        int op = 0;
        while (j < n || i < n)
        {
            op += n - i;
            for (; i < n; i++)
            {
                if (j < n && t[j] == s[i])
                    j++;
                else if (j > 0 && reverse(t[j - 1]) == s[i])
                    j--;
            }
            if (j == n && i == n)
            {
                printf("YES\n");
                return 0;
            }
            op += n - j;
            for (; j < n; j++)
            {
                if (i < n && t[j] == s[i])
                    i++;
                else if (i > 0 && reverse(s[i - 1]) == t[j])
                    i--;
            }
            if (op > 4e8)
            {
                printf("YES\n");
                return 0;
            }
            if (ilast == i)
                break;
            ilast = i;
        }
        if (j == n && i == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

bool reversed(char c, char t)
{
    return (c == 'N' && t == 'S') || (c == 'S' && t == 'N') || (c == 'W' && t == 'E') || (c == 'E' && t == 'W');
}

char reverse(char c)
{
    if (c == 'S')
        return 'N';
    if (c == 'W')
        return 'E';
    if (c == 'N')
        return 'S';
    if (c == 'E')
        return 'W';
    return '\0';
}

int main()
{
    int n;
    assert(scanf("%d", &n) == 1);
    n--;
    char* s = malloc(sizeof(char) * (n + 1));
    char* t = malloc(sizeof(char) * (n + 1));
    assert(scanf("%s", s));
    assert(scanf("%s", t));
    if (reversed(s[n - 1], t[n - 1]))
        printf("NO\n");
        else
    {
        int i = 0;
        int j = 0;
        int ilast = 0;
        int op = 0;
        while (j < n || i < n)
        {
            op += n - i;
            for (; i < n; i++)
            {
                if (j < n && t[j] == s[i])
                    j++;
                else if (j > 0 && reverse(t[j - 1]) == s[i])
                    j--;
            }
            if (j == n && i == n)
            {
                printf("YES\n");
                return 0;
            }
            op += n - j;
            for (; j < n; j++)
            {
                if (i < n && t[j] == s[i])
                    i++;
                else if (i > 0 && reverse(s[i - 1]) == t[j])
                    i--;
            }
            if (op > 4e8)
                break;
            if (ilast == i)
                break;
            ilast = i;
        }
        if (j == n && i == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

bool reversed(char c, char t)
{
    return (c == 'N' && t == 'S') || (c == 'S' && t == 'N') || (c == 'W' && t == 'E') || (c == 'E' && t == 'W');
}

char reverse(char c)
{
    if (c == 'S')
        return 'N';
    if (c == 'W')
        return 'E';
    if (c == 'N')
        return 'S';
    if (c == 'E')
        return 'W';
    return '\0';
}

int main()
{
    int n;
    assert(scanf("%d", &n) == 1);
    n--;
    char* s = malloc(sizeof(char) * (n + 1));
    char* t = malloc(sizeof(char) * (n + 1));
    assert(scanf("%s", s));
    assert(scanf("%s", t));
    if (reversed(s[n - 1], t[n - 1]))
        printf("NO\n");
        else
    {
        int i = 0;
        int j = 0;
        int ilast = 0;
        int op = 0;
        while (j < n || i < n)
        {
            op += n - i;
            for (; i < n; i++)
            {
                if (j < n && t[j] == s[i])
                    j++;
                else if (j > 0 && reverse(t[j - 1]) == s[i])
                    j--;
            }
            if (j == n && i == n)
            {
                printf("YES\n");
                return 0;
            }
            op += n - j;
            for (; j < n; j++)
            {
                if (i < n && t[j] == s[i])
                    i++;
                else if (i > 0 && reverse(s[i - 1]) == t[j])
                    i--;
            }
            if (op > 1e8)
                break;
            if (ilast == i)
                break;
            ilast = i;
        }
        if (j == n && i == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

bool reversed(char c, char t)
{
    return (c == 'N' && t == 'S') || (c == 'S' && t == 'N') || (c == 'W' && t == 'E') || (c == 'E' && t == 'W');
}

char reverse(char c)
{
    if (c == 'S')
        return 'N';
    if (c == 'W')
        return 'E';
    if (c == 'N')
        return 'S';
    if (c == 'E')
        return 'W';
    return '\0';
}

int main()
{
    int n;
    assert(scanf("%d", &n) == 1);
    n--;
    char* s = malloc(sizeof(char) * (n + 1));
    char* t = malloc(sizeof(char) * (n + 1));
    assert(scanf("%s", s));
    assert(scanf("%s", t));
    if (reversed(s[n - 1], t[n - 1]))
        printf("NO\n");
        else
    {
        int i = 0;
        int j = 0;
        int ilast = 0;
        int jlast = 0;
        int op;
        while (j < n || i < n)
        {
            for (; i < n; i++)
            {
                if (j < n && t[j] == s[i])
                    j++;
                else if (j > 0 && reverse(t[j - 1]) == s[i])
                    j--;
            }
            if (j == n && i == n)
            {
                printf("YES\n");
                return 0;
            }
            if (j == jlast)
                break;
            jlast = j;
            for (; j < n; j++)
            {
                if (i < n && t[j] == s[i])
                    i++;
                else if (i > 0 && reverse(s[i - 1]) == t[j])
                    i--;
            }
            op += 2 * n;
            //if (op >= 1e8)
            //    break;
            if (ilast == i)
                break;
            ilast = i;
        }
        if (j == n && i == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

bool reversed(char c, char t)
{
    return (c == 'N' && t == 'S') || (c == 'S' && t == 'N') || (c == 'W' && t == 'E') || (c == 'E' && t == 'W');
}

char reverse(char c)
{
    if (c == 'S')
        return 'N';
    if (c == 'W')
        return 'E';
    if (c == 'N')
        return 'S';
    if (c == 'E')
        return 'W';
    return '\0';
}

int main()
{
    int n;
    assert(scanf("%d", &n) == 1);
    n--;
    char* s = malloc(sizeof(char) * (n + 1));
    char* t = malloc(sizeof(char) * (n + 1));
    assert(scanf("%s", s));
    assert(scanf("%s", t));
    if (reversed(s[n - 1], t[n - 1]))
        printf("NO\n");
        else
    {
        int i = 0;
        int j = 0;
        int ilast = 0;
        int op;
        while (j < n || i < n)
        {
            for (; i < n; i++)
            {
                if (j < n && t[j] == s[i])
                    j++;
                else if (j > 0 && reverse(t[j - 1]) == s[i])
                    j--;
            }
            if (j == n && i == n)
            {
                printf("YES\n");
                return 0;
            }
            for (; j < n; j++)
            {
                if (i < n && t[j] == s[i])
                    i++;
                else if (i > 0 && reverse(s[i - 1]) == t[j])
                    i--;
            }
            op += 2 * n;
            if (op >= 1e8)
                break;
            if (ilast == i)
                break;
            ilast = i;
        }
        if (j == n && i == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of n playing cards. Let's describe the house they want to make:   The house consists of some non-zero number of floors.  Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card.  Each floor besides for the lowest one should contain less rooms than the floor below. Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more. While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of n cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make using exactly n cards.

The single line contains integer n (1 ≤ n ≤ 1012) — the number of cards.

Print the number of distinct heights that the houses made of exactly n cards can have.

NoteIn the first sample you can build only these two houses (remember, you must use all the cards):  Thus, 13 cards are enough only for two floor houses, so the answer is 1.The six cards in the second sample are not enough to build any house.

#include <stdio.h>

int main(void)
{
    long long n, v = 0;
    scanf("%I64d", &n);
    long long fmin = 3 - n % 3;
    for (long long f = fmin; n >= f * (3 * f + 1) / 2; f += 3)
        v++;
    printf("%I64d", v);
}

/* practice with Dukkha */
#include <stdio.h>

int main() {
	long long n, a, b;
	int ans;

	scanf("%lld", &n);
	ans = 0;
	for (a = 1; (b = a * (a + 1) / 2 * 3 - a) <= n; a++)
		if ((n - b) % 3 == 0)
			ans++;
	printf("%d\n", ans);
	return 0;
}

#include<stdio.h>
int main()
{
	long long int s=0,n,t,k=0,c=0;
	scanf("%I64d",&n);
	while(s<=n)
	{
		k++;
		s=3*k*(k+1)/2-k;
	}
	t=k;
	t=t-1;
	while(t)
	{
		if((n+t)%3==0)
		{
			c++;
		}
		t--;
	}
	printf("%ld\n",c);
	return 0;
}

#include<stdio.h>
int main()
{
	long long n,i;
	while(scanf("%lld",&n)!=EOF)
	{
		long long m,t,j,c,d=0;
		int s=0;
		for(i=1;i*i<=n;i++)
		{
			c=(n-2*i);
			t=c/3;
			d+=i-1;
			if(c%3==0&&t>=d)
			{
				s++;
			}
		}

			printf("%d\n",s);
	}
}

#include<stdio.h>
int main() {
	long long int i,j,n;
	while(scanf("%lld",&n)!=EOF){
		i=0;j=2;
		for(;n-j>=0;j+=3) {
			n-=j;
			if(n%3==0){
				i++;
			}
		}
		printf("%lld\n",i);
	}
	return 0;
}

#include<stdio.h>
#include<math.h>
typedef long long ll;
int main()
{
	ll n;
	scanf("%lld",&n);
	ll maxi=sqrt(n);
	int is=0;
	for(ll i=maxi;i>=0;i--)
	{
		if(3*(i+i*i)%2==0)
		{
			if(3*(i+i*i)/2+2*(i+1)<=n)
			{
				if((n-(3*(i+i*i)/2+2*(i+1)))%3==0)
				{
					is++;
				}
			}
		}
	}
	printf("%d",is);
}

#include<stdio.h>
#include<math.h>
typedef long long ll;
int main()
{
	ll n;
	scanf("%lld",&n);
	ll maxi=sqrt(n);
	int is=0;
	for(ll i=maxi;i>=1;i--)
	{
		if((3*i*i+i)/2<=n&&(n-(3*i*i+i)/2)%3==0)
		{
			is++;
		}

	}
	printf("%d",is);
}

#include <stdio.h>
#include <math.h>

#define ll long long

int main ()
{
    ll n, x, i;
    ll ans, yu, cha, mid;
    while (scanf("%I64d", &n) != EOF)
    {

        ans = 0;
        mid = (-1 + sqrt (1 + 24 * n)) / 6;
        if(3 * mid * mid + mid - 2 * n > 0){
            mid--;
        }
        while(mid)
        {

            if((n + mid) % 3 == 0){
                ans++;
            }
            mid--;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

#include <stdio.h>
#define LL long long
int main()
{
    LL n;
    int ans=0;
    scanf("%I64d",&n);
    int rem=n%3;
    LL q=n/3;
    LL start;
    if(rem==0)
    {
        start=3;
    }
    else if(rem==1)
    {
        start=2;
    }
    else
    {
        start=1;
    }
    LL lim=(start*(start+1))/2;
    while((q+ans+1)>=lim)
    {
        ans++;
        start+=3;
        lim=(start*(start+1))/2;
    }
    printf("%d",ans);
    return 0;
}

#include<stdio.h>
int main()
{
    long long int i,n,count=0,h,rooms;
   scanf("%lld",&n);
    for(h=3-(n%3);;h=h+3)
    {
        rooms=(n+h)/3;
        if(rooms>=(h*(h+1))/2)
        count++;
        else
        break;
    }
    printf("%lld\n",count);
    return 0;
}

#include<stdio.h>
int main()
{
	long long int n;
	scanf("%lld", &n);
	long long int ans=0, h=0;
	while(1)
	{
		h++;
		long long int tem=n-(2*h);
		if(tem<0)break;
		if(tem%3==0)
		{
			long long int t = (h*(h+1))/2;
			t-=h;
			t*=3;
			if(tem>=t)ans++;
			else break;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

#include<stdio.h>
int main()
{
	long long n;
	while(scanf("%I64d",&n)!=EOF)
	{
		long long  a,b;
		a=n/3;
		b=n%3;

		long long  mi=0;if(b==2) mi=1;
		for(;;)
		{if(b%2==1) {b=b+3;a=a-1;}
			else  if(b%2==0){b=b+2*3 ; a=a-2; }

			 if(2*a>=b/2*(b/2-1))
			{
				mi++;
			}
			else
			{
				break;
			}

		}

		printf("%I64d\n",mi);
	}
}

#include <stdio.h>

typedef long long LL_t;

int main()
{
    LL_t h, N, Ans, F, E;
    scanf("%I64d", &N);
    h = 1;
    Ans = 0;
    while(1)
    {
        F = h*2 + h*(h-1)/2*3;
        if(F > N)
        {
            break;
        }
        E = N - F;
        if(E % 3 == 0)
        {
            ++Ans;
        }
        ++h;
    }
    printf("%I64d\n", Ans);
    return 0;
}

#include<stdio.h>
#define ll long long int
ll getcards(ll flo)
{
    return 3*flo*(flo+1)/2-flo;
}
//Another solution would be to calculate the number of rooms R i.e 3R-F everytime and then return the value.But it would become an O(n*sqrt(n));
int main()
{
    ll n;
    scanf("%lld",&n);

    ll floor;
    ll rem=0;
    for(floor=1;getcards(floor)<=n;floor++)
    {
        if((n+floor)%3==0)
            rem++;
    }

    printf("%lld",rem);

    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long int n,a,b;
	scanf("%lld",&n);
	b=(sqrt(1+24*n)-1)/6;
	a=3-n%3;
	printf("%lld\n",a>b?0:(b-a)/3+1);
	return 0;
}

#include <stdio.h>

int main(void)
{
	unsigned long long int ar[1000000],n;
	int i,num=0;
	ar[1]=5;
	ar[2]=7;
	for(i=3;i<1000000;i++)
		{
		ar[i]=ar[i-1]+3*i-1;
		//printf("%llu ",ar[i]);
		}
	scanf("%llu",&n);
	if(n==2)
		{
		printf("1");
		return 0;
		}
	i=1;
	while(ar[i]<=n)
		{
			if((n-ar[i])%3==0)
				num++;
			i++;
		}
	printf("%d",num);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
typedef long long int ll;
int main()
{
    ll n,ans=0;
    scanf("%lld",&n);
    ll min=2,compt=5;
    ll k=2;
    while(n>=min)
    {
        if(n%3==k) ans++;
        min+=compt;
        compt+=3;
        if(k>0)k--;
        else k=2;
    }
    printf("%lld",ans);
    return 0;
}

#include <stdio.h>

typedef long long LL_t;

int main()

{
    LL_t h, N, Ans, F, E;
    scanf("%I64d", &N);
    h = 1;
    Ans = 0;
    while(1) {
        F = h * 2 + h * (h - 1) / 2 * 3;
        if(F > N) {
            break;
        }
        E = N - F;
        if(E % 3 == 0) {
            ++Ans;
        }
        ++h;
    }
    printf("%I64d\n", Ans);
    return 0;
}

#include<stdio.h>
int main()
{
    long long int n,i,count=0;
    scanf("%I64d",&n);
    for(i=1;3*i*(i-1)/2+2*(i)<=n;i++)
    {
        if((n-2*i)%3==0)

        {
            count++;
        }
    }
    printf("%I64d\n",count);
    return(0);
}

#include<stdio.h>
typedef long long ll;
int main()
{
	ll n,i;
	int ans=0;
	scanf("%I64d",&n);
	for(i=1;i<n;i++)
	{
		if(1ll*i*(3*i+1)/2>n)
			break;
		else
		{
			if((n-1ll*i*(3*i+1)/2)%3==0)
				ans++;
		}
	}
	printf("%d",ans);
}

 #include<stdio.h>
 int main()
  {
    long long int t,n,i,count=0,rem;
    scanf("%I64d",&n);
    t=n%3;
    t=3-t;
    for(i=t;3*i*(i+1)/2<=n+i;i+=3)
    {
    count++;
    }
    printf("%I64d\n",count);
    return 0;
  }

#include<stdio.h>
int main() {
	long long int i=0,j=2,k,l,n,m;
	scanf("%lld",&n);
	for(;n-j>=0;j+=3) {n-=j;if(n%3==0)i++;}
	printf("%lld\n",i);
	return 0;
}

#include<stdio.h>

int main()
{
    long long n;
    scanf("%lld",&n);
    long long count = 0,check=2,var=2,iter = 1;
    while(check<=n)
    {
        if((n+iter)%3==0)
        {
            count++;
            //printf("%lld",iter);
        }
        var = var+3;
        check = check + var;
        //printf("%lld\n",check);
        //printf("%lld\n",iter);
        iter++;
        //printf("%lld\n",iter);
    }
    printf("%lld",count);
    return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    long long int n,i,s,j,sum=0;
    while(~scanf("%I64d",&n))
   {
   	sum=0;
    s=2;
    i=1;
    while(s<=n)
    {
    	i++;
    	s=i*(i+1)+i*(i-1)/2;
    }
    for(j=1;j<i;j++)
    {
    	if((n+j)%3==0)
    	sum++;
    }
    printf("%I64d\n",sum);
   }
}

#include<stdio.h>
int main()
{
	long long int n,count=0,t,i,q,sum;
	scanf("%lld",&n);
	t=n/5;
	for(i=1;i<t+2;i+=0)
	{
		if((n+i)%3==0)
		{
			sum=((i)*(i+1))/2;
			q=(n+i)/3;
			if(q>=sum)
				count++;
			else
				break;
			i+=3;
		}
		else
			i++;
	}
	printf("%lld\n",count);
	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long k=0,n;
	while(scanf("%lld",&n)!=EOF)
	{
		k=0;
		for(long long i=sqrt(n);i>=1;i--)
		{
			if((n-(3*i*i+i)/2)%3==0&&(3*i*i+i)/2<=n)
			k++;
		}
		printf("%lld\n",k);
	}
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
	long long p,n,ans;
	while(scanf("%I64d",&n)==1)
	{
		if((n<=1)||(n==4))
		{
			printf("0\n");
		}
		else
		{
			p=(long long)((sqrt(24*n+1)-1)/6);
			if(n%3==0)
			{
				ans=p/3;
				printf("%I64d\n",ans);
			}
			else if(n%3==1)
			{
				ans=(p-2)/3+1;
				printf("%I64d\n",ans);
			}
			else
			{
				ans=(p-1)/3+1;
				printf("%I64d\n",ans);
			}
		}
	}
	return 0;
}

#include <stdio.h>
long long x(int n)
{
long long   i, sum=0,s;
  for (i=1;i<=n;i++)
     sum+=i;
s=sum*3+n*2;
return s;
}
int main()
{
  long long n,i,s=0,k;
  scanf("%I64d",&k);
  for (i=1;i<1000000;i++)
  {
    if ((k+i)%3==0&&(3*i*i+i)/2<=k) s++;
  }
  printf("%I64d\n",s);
}

#include<stdio.h>
int main(){
    long long a,b=0,c,d,i,j,level,x,cards,n,t;
    scanf("%lld",&n);
    for(level=1000000;level>0;level--){
        x=(level*(level+1))/2-level;
        cards=(3*x+2*level);
        if(cards<=n){
            b=level;
            break;
        }
    }
//  printf("%lld\n",b);
    if(n%3==2){
        a=(b+2)/3;
    }else if(n%3==1){
        a=(b+1)/3;
    }else{
        a=b/3;
    }
    printf("%lld\n",a);
    return 0;
}

#include <stdio.h>

int main(void)
{
	long long int n;
	long long int b;
	int i;
	int c;

	scanf("%I64d", &n);

	c = 0;
	for(i = 1, b = 2; b <= n; i ++, b += 3 * (long long)i - 1)
	{
		if((n - b) % 3 == 0)
		{
			//printf("%d\n", i);
			c ++;
		}
	}

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

	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    long long n,h,k,s;
    scanf("%I64d",&n);
    h=0;
    if((n+1)%3==0)
        h++;
    s=7;
    k=5;
    for(s=7;s<=n;s=s+k)
    {
        if((n-s)%3==0)
            h++;
        k=k+3;
    }
    printf("%I64d\n",h);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long int n;
	scanf("%lld",&n);
	long long int m=(-1+sqrt(1+24*n))/6;
	int i=(3+(3-n)%3)%3;
	int j=m%3;
	int k=0;
//	printf("%d %d",i,j);
	if(i<=j&&i!=0)
		k=1;
//	printf("%lld",m/3);
	printf("%lld",m/3+k);
	return 0;
}

#include<stdio.h>
#include<math.h>

void solve(long long int x)
{
    long long int n=(long long int)((sqrt(24*x + 1) - 1)/6.0);
    long long int ans=0,rem=x-(3*n*n+n)/2;
   // printf("%lld\n",rem);
    if(n>0)
    {
        if(rem%3==0)    ans=1;
        while(n>1)
        {
            if((rem+2)%3==0)
            {
                ans++;
            }
            rem+=2;
            n--;
        }
    }
    printf("%lld",ans);
}
int main()
{
    long long int n;
    scanf("%lld",&n);
    solve(n);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long i,A,cnt=0,cnt4;
    scanf("%I64d",&i);
    if(i%3==0){
        A=15;
        cnt4=3;
    }
    else if(i%3==2){
        A=2;
        cnt4=1;
    }
    else if(i%3==1){
        A=7;
        cnt4=2;
    }
    while(A<=i){
        cnt++;
        A=A+3*cnt4+2;
        cnt4++;
        A=A+3*cnt4+2;
        cnt4++;
        A=A+3*cnt4+2;
        cnt4++;
    }
    printf("%I64d",cnt);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n,i,j,count=0;
    scanf("%lld",&n);

    //j=sqrt(n);
    j = (sqrt(1+24*n)-1)/6;
    for(i=1;i<=j;i++)
    {
        if((n+i)%3==0)
            count++;
    }

    printf("%lld\n",count);

    return 0;
}

#include<stdio.h>
int main(){
	long long int last,sum=2,i,j,k,l,n,m,rem;
	scanf("%lld",&n);
	m = 1;
	rem = 5;
	while(sum<n){
		m++;
		last = sum;
		sum = sum + rem;
		rem = rem + 3;
	}
//	printf("%lld %lld %lld\n",m,sum,last);
	if(sum==n){
		if(m%3==0)
			printf("%lld\n",m/3);
		else if(m%3==1)
			printf("%lld\n",m/3+1);
		else
			printf("%lld\n",m/3+1);
	}
	else{
		m--;
		if((n-last)%3==0){
			if(m%3==0)
				printf("%lld\n",m/3);
			else if(m%3==1)
				printf("%lld\n",(m/3)+1);
			else
				printf("%lld\n",(m/3)+1);
		}
		else if((n-last)%3==1){
			if(m%3==0)
				printf("%lld\n",(m)/3);
			else if(m%3==1){
				if(m>1)
					printf("%lld\n",m/3);
				else
					printf("0\n");}
			else
				printf("%lld\n",m/3+1);
		}
		else{
			if(m%3==0)
				printf("%lld\n",m/3);
			else if(m%3==1)
				printf("%lld\n",(m-1)/3);
			else
				printf("%lld\n",(m-2)/3);
		}
	}
	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long int n,a,b;
	scanf("%I64d",&n);
	b=(sqrt(1+24*n)-1)/6;
	a=3-n%3;
	printf("%I64d\n",a>b?0:(b-a)/3+1);
	return 0;
}

#include<stdio.h>
int main()
{
    long long int n,i,ans=0;
    scanf("%lld",&n);
    for(i=1;(3*i*(i+1)/2-i)<=n;i++)
    {
        if((n+i)%3==0)
        ans++;
    }
    printf("%d\n",ans);
    return 0;
}

#include<stdio.h>
#include<string.h>
#define MAX 1000000//1-1000000�ۼ�Ϊ1e12
long long most[1000010];
int main(void)
{
	long long ans=0,n;
	scanf("%lld",&n);
	memset(most,0,sizeof(most));
	for(int i=1;i<=MAX;i++)
	{
		most[i]=3*i-1+most[i-1];
	}
	for(int i=1;i<=MAX;i++)
    {
		if(n>=most[i]&&(n-most[i])%3==0)
		{
			ans++;
		}
		if(n<most[i])
		    break;
	}
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
int main()
{
	int n,t;
	double k;
	scanf("%d",&n);
	for(k=1;k<n;k++)
	{
		if(n<k/2*k*3+k/2)
		{
			break;
		}
	}
	t=k;
	int c=0;
	t=t-1;
	while(t)
	{
		if((n+t)%3==0)
		{
			c++;
		}
		t--;
	}
	printf("%d",c);
	return 0;
}

#include<stdio.h>
int main()
{
	long n,t;
	double k;
	scanf("%ld",&n);
	for(k=1;k<n;k++)
	{
		if(n<k/2*k*3+k/2)
		{
			break;
		}
	}
	t=k;
	long c=0;
	t=t-1;
	while(t)
	{
		if((n+t)%3==0)
		{
			c++;
		}
		t--;
	}
	printf("%ld",c);
	return 0;
}

#include<stdio.h>
int main()
{
	long long n,i;
	while(scanf("%lld",&n)!=EOF)
	{
		int s=0,m,t,j,c,d=0;
		for(i=1;i*i<=n;i++)
		{
			c=(n-2*i);
			t=c/3;
			d+=i-1;
			if(c%3==0&&t>=d)
			{
				s++;
			}
		}

			printf("%d\n",s);
	}
}

#include<stdio.h>
int fact(int x);
int main()
{
	long long n,i;
	while(scanf("%lld",&n)!=EOF)
	{
		int s=0,m,t,j,c,d;
		for(i=1;i<=10;i++)
		{
			c=(n-2*i);
			t=c/3;
			if(c%3==0&&t>=fact(i))
			{
				s++;
			}
		}

			printf("%d\n",s);
	}
}
int fact(int x)
{
	int i,s=0;
	for(i=1;i<x;i++)
	{
		s+=i;
	}
	return s;
}

#include <stdio.h>
#define ll long long
ll arr[3000005];
int main()
{

    int i;
    arr[0]=0;
    arr[1]=2;

    ll a=2;
    for(i=2;i<3000005;i++)
    {
        a+=((i-1)*3+2);
        arr[i]=a;
    }

    ll n;
    scanf("%lld",&n);
    ll ans=0;
    for(i=1;i<300005;i++)
    {
        int flag=0;
        if(n>=arr[i])
        {
            ll diff=n-arr[i];

            if(diff%3==0)
            {
                //printf("i=%d\n",i);
                ans+=1;
            }
        }
    }

    printf("%lld\n",ans);

}

#include<stdio.h>
#include<math.h>
typedef long long ll;
int main()
{
	ll n;
	scanf("%lld",&n);
	ll maxi=sqrt(n);
	int is=0;
	for(ll i=maxi;i>=1;i--)
	{
		if(3*(i+i*i)%2==0)
		{
			if(3*(i+i*i)/2+2*(i+1)<=n)
			{
				if((n-(3*(i+i*i)/2+2*(i+1)))%3==0)
				{
					is++;
				}
			}
		}
	}
	printf("%d",is);
}

#include <stdio.h>
#define LL long long
int main()
{
    LL n;
    int ans=0;
    scanf("%I64d",&n);
    int rem=n%3;
    LL q=n/3;
    LL start;
    if(rem==0)
    {
        start=3;
    }
    else if(rem==1)
    {
        start=2;
    }
    else
    {
        start=4;
        q++;
    }
    LL lim=(start*(start+1))/2;
    while((q+ans+1)>=lim)
    {
        ans++;
        start+=3;
        lim=(start*(start+1))/2;
    }
    printf("%d",ans);
    return 0;
}

#include<stdio.h>
int main()
{
  long long int n,i,t[3]={0},j,s;
   scanf("%I64d",&n);
   for(i=2,s=0,j=0;s<n;i+=3)
	{s+=i;
         t[s%3]++;
        }
    printf("%I64d\n",t[n%3]);

return 0;
}

#include<stdio.h>
int main()
{
  long long int n;
   scanf("%I64d",&n);
   if(n==1 ||n==3 || n==4 || n==6 || n==9 || n==12)
	printf("0\n");
   else
	printf("1\n");
return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	int n,a,b;
	scanf("%d",&n);
	b=(sqrt(1+24*n)-1)/6;
	a=3-n%3;
	printf("%d\n",a>b?0:(b-a)/3+1);
	return 0;
}

#include <stdio.h>

int main(void)
{
	unsigned long long int ar[1000000],n;
	int i,num=0;
	ar[1]=5;
	ar[2]=7;
	for(i=3;i<1000000;i++)
		{
		ar[i]=ar[i-1]+3*i-1;
		//printf("%llu ",ar[i]);
		}
	scanf("%llu",&n);
	i=1;
	while(ar[i]<=n)
		{
			if((n-ar[i])%3==0)
				num++;
			i++;
		}
	printf("%d",num);
	return 0;
}

#include<stdio.h>
int main()
{
	long long n;
	scanf("%lld",&n);
	long long l=1,r=n/2+1,mid,ser,u;
	if (n==2) {printf("1\n");return 0;}
	while (l+1<r)
	{
		mid=l+(r-l)/2;
		ser=(n-mid*2)/3;
		//printf("%lld %lld\n",l,r);
		if ( mid%2==0 ) u=mid/2*(mid-1);else u=(mid-1)/2*mid;
		if (mid*2+ser*3==n&&ser>=u) l=mid;
		else r=mid;
	}
	if ((l==1)&&!(mid*2+ser*3==n&&ser>=u)) {printf("0\n");return 0;}
	else {l=l/3+1;printf("%lld\n",l);return 0;}
}

#include<stdlib.h>
#include<stdio.h>
typedef long long ll;
ll n,ans=0;
int main()
{
	scanf("%LLd",&n);
	ll right=1000000000,left=0,mid;
	while(left<=right)
	{
		mid=(left+right)/2;
		if(2*n-(3*mid-1)*(3*mid+1)<6*mid+4)
			right=mid-1;
		else
		{
			ans=mid;
			left=mid+1;
		}
	}
	printf("%LLd",ans);
}

#include<stdlib.h>
#include<stdio.h>
typedef long long ll;
ll n,ans=0;
int main()
{
	scanf("%LLd",&n);
	ll right=10000000000,left=0,mid;
	while(left<=right)
	{
		mid=(left+right)/2;
			if(2*n-3*(mid+1)*mid<(mid+1)*4)
			{
				 right=mid-1;
			}
		    else
		     {
                   ans=mid;
				   left=mid+1;
			 }
	}
	if((n-(3*(ans+1)*ans/2))%2!=0&&ans>0)
		ans--;
	printf("%LLd",ans);
}

#include<stdlib.h>
#include<stdio.h>
typedef long long ll;
ll n,ans=0;
int main()
{
	scanf("%LLd",&n);
	ll i,ans=0,x;
	for(i=1;i<n;i++)
	{
		x=n-(3*i+1)*i/2;
		if(x<0)
		  break;
		else{if(x%3==0) ans++;}
	}
	printf("%LLd",ans);
}

#include<stdlib.h>
#include<stdio.h>
typedef long long ll;
ll n,ans=0;
int main()
{
	scanf("%LLd",&n);
	ll right=1000000000,left=0,mid;
	while(left<=right)
	{
		mid=(left+right)/2;
		if(2*n-(3*mid+1)*mid<6*mid+4)
			right=mid-1;
		else
		{
			ans=mid;
			left=mid+1;
		}
	}
	printf("%LLd",ans);
}

#include<stdio.h>
int main() {
	long long int i=0,j=2,k,l,n,m;
	scanf("%lld",&n);
	for(;n-j>=0;j+=3,i++) n -= j;
	printf("%lld\n",n%3?0:i);
	return 0;
}

#include<stdio.h>
int main() {
	long long int i=0,j=2,k,l,n,m;
	scanf("%lld",&n);
	for(;n-j>=0;j+=3,i++) n -= j;
	printf("%lld\n",n%3?0:i-1);
	return 0;
}

#include<stdio.h>

int main()
{
    long long n;
    scanf("%lld",&n);
    long long count = 0,check=2,var=2,iter = 1;
    while(check<n)
    {
        if(n>check && ((n+iter)%3==0))
            count++;
        var = var+iter*3;
        check = check + var;
        iter++;
    }
    printf("%lld",count);
    return 0;
}

#include<stdio.h>

int main()
{
    long long n;
    scanf("%lld",&n);
    long long count = 0,check=2,var=2,iter = 1;
    while(check<n)
    {
        if(n>check && ((n+iter)%3==0))
            count++;
        var = var+3;
        check = check + var;
        iter++;
    }
    printf("%lld",count);
    return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    long long int n,i,s,j,sum=0;
    scanf("%I64d",&n);
    s=2;
    i=1;
    while(s<n)
    {
    	i++;
    	s=i*(i+1)+i*(i-1)/2;
    }
    for(j=1;j<i;j++)
    {
    	if((n+j)%3==0)
    	sum++;
    }
    printf("%I64d",sum);
}

#include<stdio.h>
int main()
{
	long int n,count=0,t,i,q,sum;
	scanf("%ld",&n);
	t=n/5;
	for(i=1;i<t+2;i++)
	{
		if((n+i)%3==0)
		{
			sum=((i)*(i+1))/2;
			q=(n+i)/3;
			if(q>=sum)
				count++;
		}
	}
	printf("%ld\n",count);
	return 0;
}

#include<stdio.h>
#include<math.h>
int n;
int main()
{
	while (scanf("%d",&n)!=EOF)
	{int k=0;
		for(int i=sqrt(n);i>=1;i--)
		{
			if((2*n-(3*i*i+i))%3==0)
			k++;
		}
		printf("%d\n",k);
	}
}

#include <stdio.h>
long long x(int n)
{
long long   i, sum=0,s;
  for (i=1;i<=n;i++)
     sum+=i;
s=(sum+n)*3;
return s;
}
int main()
{
  long long n,i,s;
  scanf("%I64d",&n);
  for (i=1;i<10000000;i++)
  {
    if (x(i)>n)
     {s=i;
     break;}
  }
  printf("%I64d\n",s-1);
}

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
int main()
{
	long long n,a[100005],i;
	scanf("%I64d",&n);
	for (i=1;i<=100005;i++)
	  {if((i-1)*i*3/2+2*i>n)
	    break;
	  }
	printf("%I64d\n",i-2);
}

#include <stdio.h>

int main(void)
{
	int n;
	int b;
	int i;
	int c;

	scanf("%d", &n);

	c = 0;
	for(i = 1, b = 2; b <= n; i ++, b += 3 * i - 1)
	{
		if((n - b) % 3 == 0)
			c ++;
	}

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

	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	long long int n;
	scanf("%lld",&n);
	long long int m=(-1+sqrt(1+24*n))/6;
	int i=(3-n)%3;
	int j=m%3;
	int k=0;
	if(i<=j&&i!=0)
		k=1;
	printf("%lld",m/3+k);
	return 0;
}

#include<stdio.h>
#include<math.h>

void solve(long long int x)
{
    long long int n=(long long int)((sqrt(24*x + 1) - 1)/6.0);
    long long int ans=0,rem=x-(3*n*n+n)/2;
    if(n>1)
    {
        ans=1;
        while(n>1)
        {
            if((rem+2)%3==0)
            {
                ans++;
            }
            rem+=2;
            n--;
        }
    }
    printf("%lld",ans);
}
int main()
{
    long long int n;
    scanf("%lld",&n);
    solve(n);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n,i,j,count=0;
    scanf("%lld",&n);

    j=sqrt(n);
    for(i=1;i<=j;i++)
    {
        if((n+i)%3==0)
            count++;
    }

    printf("%lld\n",count);

    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n,i,j,count=0;
    scanf("%lld",&n);

    //j=sqrt(n);
    j = (1 + sqrt(1+24*n))/6;
    for(i=1;i<=j;i++)
    {
        if((n+i)%3==0)
            count++;
    }

    printf("%lld\n",count);

    return 0;
}

#include<stdio.h>
int main(){
	long long int i,sum,j,k,l,n,m,rem;
	scanf("%lld",&n);
	sum=2;
	m = 1;
	while(sum<n){
		m++;
		sum = (m*(3*m+1))/2;
	}
	if(sum==n)
		rem = sum;
	else
		rem = ((m-1)*(3*m-2))/2;
//	printf("%lld %lld %lld\n",m,sum,rem);
	if((n-rem)%3==0)
		if(m%3==0)
			printf("%lld\n",m/3);
		else
			printf("%lld\n",m/3+1);
	else if((n-rem)%3==2)
		if((m-2)%3==0)
			printf("%lld\n",(m-2)/3);
		else
			printf("%lld\n",(m-2)/3+1);
	else
		if((m-1)%3==0 || m-1==1)
			printf("%lld\n",(m-1)/3);
		else
			printf("%lld\n",(m-1)/3+1);
	return 0;
}

#include<stdio.h>
int main(){
	int i,j,k,l,n,m,a[10];
	for(i=0;i<10;i++)
		a[i]=0;
	for(i=0;i<6;i++){
		scanf("%d",&n);
		a[n]++;
	}
	int flag=0;
	for(i=1;i<10;i++){
		if(a[i]>=4){
			if(a[i]==4){
				for(j=1;j<10;j++)
					if(a[j]==2){
						flag=1;
						printf("Elephant\n");
					}
				if(flag==0)
					printf("Bear\n");
			}
			else if(a[i]==6)
				printf("Elephant\n");
			else
				printf("Bear\n");
			flag=1;
		}
	}
	if(flag==0){
		printf("Alien\n");
	}
	return 0;
}

#include<stdio.h>
int main(){
	long long int i,sum,j,k,l,n,m,rem;
	scanf("%lld",&n);
	sum=2;
	m = 1;
	while(sum<n){
		m++;
		sum = (m*(3*m+1))/2;
	}
	if(sum==n)
		rem = sum;
	else
		rem = ((m-1)*(3*m-2))/2;
//	printf("%lld %lld %lld\n",m,sum,rem);
	if((n-rem)%3==0)
		if(m%3==0)
			printf("%lld\n",m/3);
		else
			printf("%lld\n",m/3+1);
	else if((n-rem)%3==2)
		printf("%lld\n",(m-2)/3);
	else
		printf("%lld\n",m/3);
	return 0;
}

#include<stdio.h>
int main(){
	long long int sum,rem,i,j,k,l,n,m;
	scanf("%lld",&n);
	sum=0;
	m=1;
	if(n==1)
		printf("0\n");
	else{
		while(sum<=n){
			sum = (m*(3*m+1))/2;
			m++;
		}
		printf("%lld\n",m-3);
	}
	return 0;
}

#include<stdio.h>
int main(){
	long long int i,sum,j,k,l,n,m,rem;
	scanf("%lld",&n);
	sum=2;
	m = 1;
	while(sum<n){
		m++;
		sum = (m*(3*m+1))/2;
	}
	if(sum==n)
		rem = sum;
	else
		rem = ((m-1)*(3*m-2))/2;
//	printf("%lld %lld %lld\n",m,sum,rem);
	if((n-rem)%3==0)
		printf("%lld\n",m/3);
	else if((n-rem)%3==2)
		printf("%lld\n",(m-2)/3);
	else
		printf("%lld\n",(m-1)/3);
	return 0;
}

#include<stdio.h>
int main(){
	long long int sum,rem,i,j,k,l,n,m;
	scanf("%lld",&n);
	sum=0;
	m=1;
	while(sum<=n){
		sum = (m*(3*m+1))/2;
		m++;
	}
	printf("%lld\n",m-2);
	return 0;
}

#include<stdio.h>
int main(){
	long long int i,sum,j,k,l,n,m,rem;
	scanf("%lld",&n);
	sum=2;
	m = 1;
	while(sum<n){
		m++;
		sum = (m*(3*m+1))/2;
	}
	if(sum==n)
		rem = sum;
	else
		rem = ((m-1)*(3*m-2))/2;
//	printf("%lld %lld %lld\n",m,sum,rem);
	if((n-rem)%3==0)
		if(m%3==0)
			printf("%lld\n",m/3);
		else
			printf("%lld\n",m/3+1);
	else if((n-rem)%3==2)
		if((m-2)%3==0)
			printf("%lld\n",(m-2)/3);
		else
			printf("%lld\n",(m-2)/3+1);
	else
		if((m-1)%3==0)
			printf("%lld\n",(m-1)/3);
		else
			printf("%lld\n",(m-1)/3+1);
	return 0;
}

#include<stdio.h>
int main(){
	long long int last,sum=2,i,j,k,l,n,m,rem;
	scanf("%lld",&n);
	m = 1;
	rem = 5;
	while(sum<n){
		m++;
		last = sum;
		sum = sum + rem;
		rem = rem + 3;
	}
//	printf("%lld %lld %lld\n",m,sum,last);
	if(sum==n){
		if(m%3==0)
			printf("%lld\n",m/3);
		else if(m%3==1)
			printf("%lld\n",m/3+1);
		else
			printf("%lld\n",m/3+1);
	}
	else{
		m--;
		if((n-last)%3==0){
			if(m%3==0)
				printf("%lld\n",m/3);
			else if(m%3==1)
				printf("%lld\n",(m/3)+1);
			else
				printf("%lld\n",(m/3)+1);
		}
		else if((n-last)%3==1){
			if(m%3==0)
				printf("%lld\n",(m)/3);
			else if(m%3==1)
				if(m>1)
					printf("%lld\n",m/3+1);
				else
					printf("0\n");
			else
				printf("%lld\n",m/3+1);
		}
		else{
			if(m%3==0)
				printf("%lld\n",m/3);
			else if(m%3==1)
				printf("%lld\n",(m-1)/3);
			else
				printf("%lld\n",(m-2)/3);
		}
	}
	return 0;
}

#include<stdio.h>
long long add(long long x);
int main(void)
{
	long long m;
	scanf("%lld",&m);
	int min=0,max=4000000;
	int mid;
	while(min<=max)
	{
		mid=(min+max)/2;
		if(3*add(mid)+2*mid>m)
		     max=mid-1;
        else
             min=mid+1;
	}
	int count=0;
	for(int i=1;i<max+5;i++)
	{
		if((m-i*2)%3==0&&(m-i*2)/3>add(i-1))
		    count++;
	}
	printf("%d\n",count);
	return 0;
}

long long add(long long x)
{
	long long ans=0;
	for(int i=1;i<=x;i++)
	    ans=ans+x;
	    return ans;
}

#include<stdio.h>
long long add(long long x);
int main(void)
{
	long long m;
	scanf("%lld",&m);
	int min=0,max=4000000;
	int mid;
	while(min<=max)
	{
		mid=(min+max)/2;
		if(3*add(mid)+2*mid>m)
		     max=mid-1;
        else
             min=mid+1;
	}
	int count=0;
	for(int i=1;i<max+5;i++)
	{
		if((m-i*2)%3==0&&(m-i*2)/3>=add(i-1))
		    count++;
	}
	printf("%d\n",count);
	return 0;
}

long long add(long long x)
{
	long long ans=0;
	for(int i=1;i<=x;i++)
	    ans=ans+x;
	    return ans;
}

#include<stdio.h>
#include<math.h>
typedef long long ll;
int main()
{
	ll n;
	scanf("%lld",&n);
	ll maxi=sqrt(n);
	int is=0;
	for(ll i=maxi;i>=1;i--)
	{
		if(3*(i+i*i)%2==0)
		{
			if(3*(i+i*i)/2+2*(i+1)<=n)
			{
				if((n-(3*(i+i*i)/2+2*(i+1)))%3==0)
				{
					printf("%lld",i);
					is=1;
					break;
				}
			}
		}
	}
	if(is==0)
	{
		printf("0");
	}
}


As you know, lemmings like jumping. For the next spectacular group jump n lemmings gathered near a high rock with k comfortable ledges on it. The first ledge is situated at the height of h meters, the second one is at the height of 2h meters, and so on (the i-th ledge is at the height of i·h meters). The lemmings are going to jump at sunset, and there's not much time left.Each lemming is characterized by its climbing speed of vi meters per minute and its weight mi. This means that the i-th lemming can climb to the j-th ledge in  minutes.To make the jump beautiful, heavier lemmings should jump from higher ledges: if a lemming of weight mi jumps from ledge i, and a lemming of weight mj jumps from ledge j (for i < j), then the inequation mi ≤ mj should be fulfilled.Since there are n lemmings and only k ledges (k ≤ n), the k lemmings that will take part in the jump need to be chosen. The chosen lemmings should be distributed on the ledges from 1 to k, one lemming per ledge. The lemmings are to be arranged in the order of non-decreasing weight with the increasing height of the ledge. In addition, each lemming should have enough time to get to his ledge, that is, the time of his climb should not exceed t minutes. The lemmings climb to their ledges all at the same time and they do not interfere with each other.Find the way to arrange the lemmings' jump so that time t is minimized.

The first line contains space-separated integers n, k and h (1 ≤ k ≤ n ≤ 105, 1 ≤ h ≤ 104) — the total number of lemmings, the number of ledges and the distance between adjacent ledges. The second line contains n space-separated integers m1, m2, ..., mn (1 ≤ mi ≤ 109), where mi is the weight of i-th lemming. The third line contains n space-separated integers v1, v2, ..., vn (1 ≤ vi ≤ 109), where vi is the speed of i-th lemming.

Print k different numbers from 1 to n — the numbers of the lemmings who go to ledges at heights h, 2h, ..., kh, correspondingly, if the jump is organized in an optimal way. If there are multiple ways to select the lemmings, pick any of them.

NoteLet's consider the first sample case. The fifth lemming (speed 10) gets to the ledge at height 2 in  minutes; the second lemming (speed 2) gets to the ledge at height 4 in 2 minutes; the fourth lemming (speed 2) gets to the ledge at height 6 in 3 minutes. All lemmings manage to occupy their positions in 3 minutes.

#include <stdio.h>
#include <stdlib.h>

typedef struct {
     int m;
     int v;
     int i;
} lemmings;

int cmp(const void *a, const void *b)
{
     if (((lemmings *)a)->m != ((lemmings *)b)->m) {
	  return ((lemmings *)a)->m - ((lemmings *)b)->m;
     } else {
	  return ((lemmings *)a)->v - ((lemmings *)b)->v;
     }
}

int main()
{
     int n, k, h, i, j;
     double l, r, m;
     lemmings a[100000];
     int b[100000], c[100000];

     scanf("%d %d %d", &n, &k, &h);

     for (i = 0; i < n; i++) scanf("%d", &a[i].m);
     for (i = 0; i < n; i++) scanf("%d", &a[i].v);

     for (i = 0; i < n; i++) a[i].i = i + 1;

     qsort(a, n, sizeof(lemmings), cmp);

     l = 0, r = 1e18, m = (l + r) / 2;

     for (i = 0; i < 500; i++) {
	  int p = 0;

	  for (j = 0; j < n && p < k; j++) {
	       if ((double)(p + 1) * h / a[j].v <= m) {
		    b[p++] = a[j].i;
	       }
	  }

	  if (p >= k) {
	       r = m;
	       m = (l + r) / 2;

	       for (j = 0; j < k; j++) c[j] = b[j];
	  } else {
	       l = m;
	       m = (l + r) / 2;
	  }
     }

     for (i = 0; i < k; i++) {
	  if (i > 0) putchar(' ');

	  printf("%d", c[i]);
     }

     puts("");

     return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct {
     int m;
     int v;
     int i;
} lemmings;

int cmp(const void *a, const void *b)
{
     if (((lemmings *)a)->m != ((lemmings *)b)->m) {
	  return ((lemmings *)a)->m - ((lemmings *)b)->m;
     } else {
	  return ((lemmings *)a)->v - ((lemmings *)b)->v;
     }
}

int main()
{
     int n, k, h, c, i, j;
     double l, r, m;
     lemmings a[100000];

     scanf("%d %d %d", &n, &k, &h);

     for (i = 0; i < n; i++) scanf("%d", &a[i].m);
     for (i = 0; i < n; i++) scanf("%d", &a[i].v);

     for (i = 0; i < n; i++) a[i].i = i + 1;

     qsort(a, n, sizeof(lemmings), cmp);

     l = 0, r = 1e9, m = (l + r) / 2;

     for (i = 0; i < 500; i++) {
	  c = 0;

	  for (j = 0; j < n; j++) {
	       if ((double)(c + 1) * h / a[j].v <= m) c++;
	  }

	  if (c >= k) {
	       r = m;
	       m = (l + r) / 2;
	  } else {
	       l = m;
	       m = (l + r) / 2;
	  }
     }

     for (i = 0, c = 0; i < n && c < k; i++) {
	  if ((double)(c + 1) * h / a[i].v <= r) {
	       if (c > 0) putchar(' ');

	       printf("%d", a[i].i);

	       c++;
	  }
     }

     puts("");

     return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct {
     int m;
     int v;
     int i;
} lemmings;

int cmp(const void *a, const void *b)
{
     if (((lemmings *)a)->m != ((lemmings *)b)->m) {
	  return ((lemmings *)a)->m - ((lemmings *)b)->m;
     } else {
	  return ((lemmings *)a)->v - ((lemmings *)b)->v;
     }
}

int main()
{
     int n, k, h, c, i, j;
     double l, r, m;
     lemmings a[100000];

     scanf("%d %d %d", &n, &k, &h);

     for (i = 0; i < n; i++) scanf("%d", &a[i].m);
     for (i = 0; i < n; i++) scanf("%d", &a[i].v);

     for (i = 0; i < n; i++) a[i].i = i + 1;

     qsort(a, n, sizeof(lemmings), cmp);

     l = 0, r = 1e18, m = (l + r) / 2;

     for (i = 0; i < 100; i++) {
	  c = 0;

	  for (j = 0; j < n; j++) {
	       if ((double)(c + 1) * h / a[j].v <= m) c++;
	  }

	  if (c >= k) {
	       r = m;
	       m = (l + r) / 2;
	  } else {
	       l = m;
	       m = (l + r) / 2;
	  }
     }

     for (i = 0, c = 0; i < n && c < k; i++) {
	  if ((double)(c + 1) * h / a[i].v <= r) {
	       if (c > 0) putchar(' ');

	       printf("%d", a[i].i);

	       c++;
	  }
     }

     puts("");

     return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct {
     int m;
     int v;
     int i;
} lemmings;

int cmp(const void *a, const void *b)
{
     if (((lemmings *)a)->m != ((lemmings *)b)->m) {
	  return ((lemmings *)a)->m - ((lemmings *)b)->m;
     } else {
	  return ((lemmings *)a)->v - ((lemmings *)b)->v;
     }
}

int main()
{
     int n, k, h, i, j;
     double l, r, m;
     lemmings a[100000];
     int b[100000], c[100000];

     scanf("%d %d %d", &n, &k, &h);

     for (i = 0; i < n; i++) scanf("%d", &a[i].m);
     for (i = 0; i < n; i++) scanf("%d", &a[i].v);

     for (i = 0; i < n; i++) a[i].i = i + 1;

     qsort(a, n, sizeof(lemmings), cmp);

     l = 0, r = 1e18, m = (l + r) / 2;

     for (i = 0; i < 100; i++) {
	  int p = 0;

	  for (j = 0; j < n && p < k; j++) {
	       if ((double)(p + 1) * h / a[j].v <= m) {
		    b[p++] = a[j].i;
	       }
	  }

	  if (p >= k) {
	       r = m;
	       m = (l + r) / 2;

	       for (j = 0; j < k; j++) c[j] = b[j];
	  } else {
	       l = m;
	       m = (l + r) / 2;
	  }
     }

     for (i = 0; i < k; i++) {
	  if (i > 0) putchar(' ');

	  printf("%d", c[i]);
     }

     puts("");

     return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct {
     int m;
     int v;
     int i;
} lemmings;

int cmp(const void *a, const void *b)
{
     if (((lemmings *)a)->m != ((lemmings *)b)->m) {
	  return ((lemmings *)a)->m - ((lemmings *)b)->m;
     } else {
	  return ((lemmings *)a)->v - ((lemmings *)b)->v;
     }
}

int main()
{
     int n, k, h, c, i, j;
     double l, r, m;
     lemmings a[100000];

     scanf("%d %d %d", &n, &k, &h);

     for (i = 0; i < n; i++) scanf("%d", &a[i].m);
     for (i = 0; i < n; i++) scanf("%d", &a[i].v);

     for (i = 0; i < n; i++) a[i].i = i + 1;

     qsort(a, n, sizeof(lemmings), cmp);

     l = 0, r = 1e9, m = (l + r) / 2;

     for (i = 0; i < 100; i++) {
	  c = 0;

	  for (j = 0; j < n; j++) {
	       if ((double)(c + 1) * h / a[j].v <= m) c++;
	  }

	  if (c >= k) {
	       r = m;
	       m = (l + r) / 2;
	  } else {
	       l = m;
	       m = (l + r) / 2;
	  }
     }

     for (i = 0, c = 0; i < n && c < k; i++) {
	  if ((double)(c + 1) * h / a[i].v <= r) {
	       if (c > 0) putchar(' ');

	       printf("%d", a[i].i);

	       c++;
	  }
     }

     puts("");

     return 0;
}