Under construction

Still being built. Things may change

Trigonometric Optimization

Time limit2sMemory limit128 MB

Summary
Choose positive integers x, y, z summing to S to optimize sin/cos(x)+sin/cos(y)+sin/cos(z), rounded to 10 decimals.
Level

Medium4 of 10

Topics
Math, Brute force, Greedy
Solved
No attempts yet

Problem

Many problems that arise in practice can be stated as optimization problems: we must maximize or minimize a so-called criterion function subject to some constraints.

Consider the following trigonometric optimization problem. Maximize or minimize the criterion function

F1(x)+F2(y)+F3(z)F_1(x) + F_2(y) + F_3(z)

subject to the constraint x+y+z=Sx + y + z = S, where xx, yy, zz are the variables and SS is a given parameter. All of xx, yy, zz, SS are natural numbers (positive integers, so x,y,z≥1x, y, z \ge 1). Each of the functions F1F_1, F2F_2, F3F_3 is a trigonometric function — either sin⁡\sin or cos⁡\cos — and its argument is measured in radians.

Write a program that solves this trigonometric optimization problem.

Input

The input consists of 5 lines.

  • Line 1 describes the function F1F_1 and contains either sin or cos.
  • Line 2 describes the function F2F_2 in the same format.
  • Line 3 describes the function F3F_3 in the same format.
  • Line 4 contains either min or max. If it is min, the criterion function must be minimized; if it is max, it must be maximized.
  • Line 5 contains the parameter SS (3≤S≤1063 \le S \le 10^6).

Output

Print a single line containing the optimal value of the criterion function: its maximum when line 4 is max, or its minimum when line 4 is min, taken over all natural numbers xx, yy, zz with x+y+z=Sx + y + z = S.

Print the value rounded to exactly 10 digits after the decimal point (format %.10f).

Examples3

  1. Example 1

    Input
    sin
    cos
    sin
    max
    10
    
    Expected output
    2.7787651403
    
  2. Example 2

    Input
    sin
    sin
    sin
    max
    3
    
    Expected output
    2.5244129544
    
  3. Example 3

    Input
    cos
    cos
    cos
    min
    3
    
    Expected output
    1.6209069176