Problem:
Score : 100 points

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

0 \leq N_1, N_2, N_3, N_4 \leq 9

N_1, N_2, N_3 and N_4 are integers.

Input

Input is given from Standard Input in the following format:

N_1 N_2 N_3 N_4

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.

Sample Input 1

1 7 9 4

Sample Output 1

YES

We can get 1974 by swapping N_2 and N_3.

Sample Input 2

1 9 7 4

Sample Output 2

YES

We already have 1974 before doing anything.

Sample Input 3

1 2 9 1

Sample Output 3

NO

Sample Input 4

4 9 0 8

Sample Output 4

NO

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<iostream>
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
#include<math.h>
#include"boost/algorithm/string.hpp"

using namespace std;

struct test_t{
	long long target;
	long long state;
	long long diff;
	int change;

	friend bool operator<(const test_t& l, const test_t& r)
	{
		return l.diff < r.diff;
	}
};

int main(){
		

	/*Q3*/
	//	test_t test[100000];

	unsigned long n;
	cin >> n;
	cin.ignore();

	vector<test_t> test;
	test.resize(n);

	int sum_target = 0, sum_state = 0;

	for (int i = 0; i < n; ++i){
		cin >> test[i].state;
		sum_state += test[i].state;
	}
	for (int i = 0; i < n; ++i){
		cin >> test[i].target;
		sum_target += test[i].target;
		test[i].diff = test[i].state - test[i].target;//クリ
	}


	//無理!
	if (sum_state < sum_target){
		cout << -1 << endl;
		return -1;
	}
	

	std::sort(test.begin(), test.end());
	
	//不足科目があるだけ加算
	int f = 0, l = n - 1;
	while( test[f].diff < 0 && f < l){

		//加算する点が十分
		if (test[f].diff < test[l].diff){
			test[l].diff += test[f].diff;
			test[f].diff = 0;
			test[f].change = test[l].change = 1;
			++f;

		}
		//加算する点が不十分
		else if (test[f].diff < test[l].diff){
			test[f].diff += test[l].diff;
			test[l].diff = 0;
			test[f].change = test[l].change = 1;
			--l;

		}
		//iko-ru
		else{
			test[f].diff = test[l].diff = 0;
			test[f].change = test[l].change = 1;
			++f;
			--l;

		}

	}

	int ret = 0;
	for (int i = 0; i < n; ++i){
		if (test[i].change == 1){
			++ret;
		}
	}

	cout << ret << endl;

	return f;


	/*Q2*/

	/*
	string sample = "keyence";
	string in;


	int pass = 0;
	bool zwei = false;

	cin >> in;

	int s = 0;//sの参照箇所


	//頭7後0
	if (in[0] == sample[0]
		&& in[1] == sample[1]
		&& in[2] == sample[2]
		&& in[3] == sample[3]
		&& in[4] == sample[4]
		&& in[5] == sample[5]
		&& in[6] == sample[6]
	){
		std::cout << "YES" << endl;
		return 0;
	}
	//頭6後1
	if (in[0] == sample[0]
		&& in[1] == sample[1]
		&& in[2] == sample[2]
		&& in[3] == sample[3]
		&& in[4] == sample[4]
		&& in[5] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}
	//頭5後2
	if (in[0] == sample[0]
		&& in[1] == sample[1]
		&& in[2] == sample[2]
		&& in[3] == sample[3]
		&& in[4] == sample[4]
		&& in[in.length() - 2] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}
	//頭4後3
	if (in[0] == sample[0]
		&& in[1] == sample[1]
		&& in[2] == sample[2]
		&& in[3] == sample[3]
		&& in[in.length() - 3] == sample[4]
		&& in[in.length() - 2] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}
	//頭3後4
	if (in[0] == sample[0]
		&& in[1] == sample[1]
		&& in[2] == sample[2]
		&& in[in.length() - 4] == sample[3]
		&& in[in.length() - 3] == sample[4]
		&& in[in.length() - 2] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}

	//頭2後5
	if (in[0] == sample[0]
		&& in[1] == sample[1]
		&& in[in.length() - 5] == sample[2]
		&& in[in.length() - 4] == sample[3]
		&& in[in.length() - 3] == sample[4]
		&& in[in.length() - 2] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}
	//頭1後6
	if (in[0] == sample[0]
		&& in[in.length() - 6] == sample[1]
		&& in[in.length() - 5] == sample[2]
		&& in[in.length() - 4] == sample[3]
		&& in[in.length() - 3] == sample[4]
		&& in[in.length() - 2] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}
	//頭0後7
	if (in[in.length() - 7] == sample[0]
		&& in[in.length() - 6] == sample[1]
		&& in[in.length() - 5] == sample[2]
		&& in[in.length() - 4] == sample[3]
		&& in[in.length() - 3] == sample[4]
		&& in[in.length() - 2] == sample[5]
		&& in[in.length() - 1] == sample[6]
		){
		std::cout << "YES" << endl;
		return 0;
	}
	/*
	for (int i = 0; i < in.length(); ++i){
		if (in[i] == sample[s]){
			++s;
			zwei = false;


		}
		else if(zwei == false){
			//同じ文字ならノーカン
			if (in[i+1] != sample[s]){
				++pass;
		
			}

			zwei = true;
			//cout << pass << endl;
		}

		if (s == 7 && i == in.length() - 1){
			std::cout << "YES" << endl;
			return 0;
		}

		//2回の時点で即アウト
		if (1 < pass){
			cout << "NO" << endl;
			return 0;
		}


	}
	*/



	//cout << "NO" << endl;


	return 0;


	/*Q1*/
	//typedef
	/*
	struct t{
		int param;
		int yet;
	};
	struct t target[4];
	target[0].yet = 0;
	target[1].yet = 0;
	target[2].yet = 0;
	target[3].yet = 0;
	target[0].param = 1;
	target[1].param = 9;
	target[2].param = 7;
	target[3].param = 4;


	int ret = 0;
	int in[4] = {0};

	
	cin >> in[0] >> in[1] >> in[2] >> in[3];

	//roop
	for (int i = 0; i < 4; ++i){

		//探索
		for (int j = 0; j < 4; ++j)
			if (target[j].param == in[i]){
				target[j].yet = 1;
			}
	}


	if (target[0].yet == 1
		&& target[1].yet == 1
		&& target[2].yet == 1
		&& target[3].yet == 1){
		cout << "YES" << endl;
	}
	else{
		cout << "NO" << endl;

	}

	*/

	/*Q2*/

	/*Q3*/

		/*Q4*/

		return 0;
}