Algorithm


C. Lucky Sum of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with the task.

Input

The single line contains an integer n (1 ≤ n ≤ 106) — the sum of digits of the required lucky number.

Output

Print on the single line the result — the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1.

Examples
input
Copy
11
output
Copy
47
input
Copy
10
output
Copy
-1



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int n, dp[int(1e6 + 1)][2];

int rec(int sum, bool f) {
  if(sum > n)
    return 1e9;
  
  if(sum == n)
    return 0;
  
  int &ret = dp[sum][f];
  if(ret != -1)
    return ret;
  ret = 0;
  
  ret = min(rec(sum + (!f ? 4 : 7), 0) + 1, rec(sum + (!f ? 4 : 7), 1) + 1);
  
  return ret;
}

void print(int sum, bool f) {
  if(sum > n)
    return;
  
  if(sum == n)
    return;
  
  if(!f)
    putchar('4');
  else
    putchar('7');
  
  int opt = min(rec(sum + (!f ? 4 : 7), 0) + 1, rec(sum + (!f ? 4 : 7), 1) + 1);
  
  if(opt == rec(sum + (!f ? 4 : 7), 0) + 1)
    print(sum + (!f ? 4 : 7), 0);
  else
    print(sum + (!f ? 4 : 7), 1);
}

int main() {
  scanf("%d", &n);
  memset(dp, -1, sizeof dp);
  int mn = min(rec(0, 0), rec(0, 1));
  if(mn >= 1e9) {
    puts("-1");
    return 0;
  } else if(mn == rec(0, 0))
    print(0, 0);
  else
    print(0, 1);
  puts("");
  
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
11

Output

x
+
cmd
47
Advertisements

Demonstration


Codeforcess Solution  Lucky Sum of Digits,C. Lucky Sum of Digits C,C++, Java, Js and Python , Lucky Sum of Digits,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+