import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Random;

public class CF1204E {
    public static void main(String[] args) throws Exception {
        boolean local = System.getProperty("ONLINE_JUDGE") == null;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io, new Debug(local));

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        if (local) {
            io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        final Debug debug;
        int inf = (int) 1e8;

        public Task(FastIO io, Debug debug) {
            this.io = io;
            this.debug = debug;
        }

        @Override
        public void run() {
            solve();

//            debug.debug("bf", bruteForce(3, 1));
//            debug.debug("so", solve(3, 1));
//            debug.assertTrue(bruteForce(3, 1) == solve(3, 1));
//            for(int i = 0; i < 10; i++){
//                for(int j = 0; j < 10; j++){
//                    debug.debug("i", i);
//                    debug.debug("j", j);
//                    debug.assertTrue(bruteForce(i, j) == solve(i, j));
//                }
//            }
        }

        public int bruteForce(int n, int m) {
            return allPerm(n, m, 0, 0);
        }

        public int allPerm(int n, int m, int max, int val) {
            max = Math.max(max, val);
            if (n == 0 && m == 0) {
                return max;
            }
            int sum = 0;
            if (n > 0) {
                sum = mod.plus(sum, allPerm(n - 1, m, max, val + 1));
            }
            if (m > 0) {
                sum = mod.plus(sum, allPerm(n, m - 1, max, val - 1));
            }
            return sum;
        }

        NumberTheory.Modular mod = new NumberTheory.Modular(998244853);
        NumberTheory.Composite comp = new NumberTheory.Composite(4000, mod);

        public void solve() {
            int n = io.readInt();
            int m = io.readInt();
            io.cache.append(solve(n, m));
        }

        public int solve(int n, int m) {
            int[] leq = new int[n + 1];
            leq[0] = comp.composite(n + m, n);
            for (int i = 0; i <= n; i++) {
                leq[i] = pathNoGreaterThan(n, m, i);
            }

            debug.debug("leq", leq);
            for (int i = n; i > 0; i--) {
                leq[i] = mod.plus(leq[i], -leq[i - 1]);
            }
            debug.debug("leq", leq);

            int sum = 0;
            for (int i = 0; i <= n; i++) {
                sum = mod.plus(sum, mod.mul(leq[i], i));
            }
            return sum;
        }

        //all prefix not greater than k
        public int pathNoGreaterThan(int n, int m, int k) {
            return path(m, n, k);
        }

        //find how many permutation satisfy follow condition:
        // - n * 1
        // - m * -1
        // - all prefix greater than -k
        public int path(int n, int m, int k) {
            if (n - m < -k) {
                return 0;
            }
            return mod.plus(comp.composite(n + m, n), -comp.composite(n + m,
                    n + 1 + k));
        }
    }

    public static class NumberTheory {
        private static final Random RANDOM = new Random();

        /**
         * Extend gcd
         */
        public static class ExtGCD {
            private long x;
            private long y;
            private long g;

            public long getX() {
                return x;
            }

            public long getY() {
                return y;
            }

            /**
             * Get g = Gcd(a, b) and find a way to set x and y to match ax+by=g
             */
            public long extgcd(long a, long b) {
                if (a >= b) {
                    g = extgcd0(a, b);
                } else {
                    g = extgcd0(b, a);
                    long tmp = x;
                    x = y;
                    y = tmp;
                }
                return g;
            }

            private long extgcd0(long a, long b) {
                if (b == 0) {
                    x = 1;
                    y = 0;
                    return a;
                }
                long g = extgcd0(b, a % b);
                long n = x;
                long m = y;
                x = m;
                y = n - m * (a / b);
                return g;
            }
        }

        public static class Gcd {
            public long gcd(long a, long b) {
                return a >= b ? gcd0(a, b) : gcd0(b, a);
            }

            private long gcd0(long a, long b) {
                return b == 0 ? a : gcd0(b, a % b);
            }

            public int gcd(int a, int b) {
                return a >= b ? gcd0(a, b) : gcd0(b, a);
            }

            private int gcd0(int a, int b) {
                return b == 0 ? a : gcd0(b, a % b);
            }
        }

        /**
         * Mod operations
         */
        public static class Modular {
            int m;

            public Modular(int m) {
                this.m = m;
            }

            public int valueOf(int x) {
                x %= m;
                if (x < 0) {
                    x += m;
                }
                return x;
            }

            public int valueOf(long x) {
                x %= m;
                if (x < 0) {
                    x += m;
                }
                return (int) x;
            }

            public int mul(int x, int y) {
                return valueOf((long) x * y);
            }

            public int mul(long x, long y) {
                x = valueOf(x);
                y = valueOf(y);
                return valueOf(x * y);
            }

            public int plus(int x, int y) {
                return valueOf(x + y);
            }

            public int plus(long x, long y) {
                x = valueOf(x);
                y = valueOf(y);
                return valueOf(x + y);
            }

            @Override
            public String toString() {
                return "mod " + m;
            }
        }

        /**
         * Bit operations
         */
        public static class BitOperator {
            public int bitAt(int x, int i) {
                return (x >> i) & 1;
            }

            public int bitAt(long x, int i) {
                return (int) ((x >> i) & 1);
            }

            public int setBit(int x, int i, boolean v) {
                if (v) {
                    x |= 1 << i;
                } else {
                    x &= ~(1 << i);
                }
                return x;
            }

            public long setBit(long x, int i, boolean v) {
                if (v) {
                    x |= 1L << i;
                } else {
                    x &= ~(1L << i);
                }
                return x;
            }

            /**
             * Determine whether x is subset of y
             */
            public boolean subset(long x, long y) {
                return intersect(x, y) == x;
            }

            /**
             * Merge two set
             */
            public long merge(long x, long y) {
                return x | y;
            }

            public long intersect(long x, long y) {
                return x & y;
            }

            public long differ(long x, long y) {
                return x - intersect(x, y);
            }
        }

        /**
         * Power operations
         */
        public static class Power {
            final Modular modular;

            public Power(Modular modular) {
                this.modular = modular;
            }

            public int pow(int x, long n) {
                if (n == 0) {
                    return 1;
                }
                long r = pow(x, n >> 1);
                r = modular.valueOf(r * r);
                if ((n & 1) == 1) {
                    r = modular.valueOf(r * x);
                }
                return (int) r;
            }

            public int inverse(int x) {
                return pow(x, modular.m - 2);
            }

            public int pow2(int x) {
                return x * x;
            }

            public long pow2(long x) {
                return x * x;
            }

            public double pow2(double x) {
                return x * x;
            }
        }

        /**
         * Log operations
         */
        public static class Log2 {
            public int ceilLog(int x) {
                return 32 - Integer.numberOfLeadingZeros(x - 1);
            }

            public int floorLog(int x) {
                return 31 - Integer.numberOfLeadingZeros(x);
            }

            public int ceilLog(long x) {
                return 64 - Long.numberOfLeadingZeros(x - 1);
            }

            public int floorLog(long x) {
                return 63 - Long.numberOfLeadingZeros(x);
            }
        }

        /**
         * Find all inverse number
         */
        public static class InverseNumber {
            int[] inv;

            public InverseNumber(int[] inv, int limit, Modular modular) {
                this.inv = inv;
                inv[1] = 1;
                int p = modular.m;
                for (int i = 2; i <= limit; i++) {
                    int k = p / i;
                    int r = p % i;
                    inv[i] = modular.mul(-k, inv[r]);
                }
            }

            public InverseNumber(int limit, Modular modular) {
                this(new int[limit + 1], limit, modular);
            }
        }

        /**
         * Factorial
         */
        public static class Factorial {
            int[] fact;
            int[] inv;

            public Factorial(int[] fact, int[] inv, InverseNumber in, int limit, Modular modular) {
                this.fact = fact;
                this.inv = inv;
                fact[0] = inv[0] = 1;
                for (int i = 1; i <= limit; i++) {
                    fact[i] = modular.mul(fact[i - 1], i);
                    inv[i] = modular.mul(inv[i - 1], in.inv[i]);
                }
            }

            public Factorial(int limit, Modular modular) {
                this(new int[limit + 1], new int[limit + 1], new InverseNumber(limit, modular), limit, modular);
            }
        }

        /**
         * Composition
         */
        public static class Composite {
            final Factorial factorial;
            final Modular modular;

            public Composite(Factorial factorial, Modular modular) {
                this.factorial = factorial;
                this.modular = modular;
            }

            public Composite(int limit, Modular modular) {
                this(new Factorial(limit, modular), modular);
            }

            public int composite(int m, int n) {
                if (n > m) {
                    return 0;
                }
                return modular.mul(modular.mul(factorial.fact[m], factorial.inv[n]), factorial.inv[m - n]);
            }
        }

        /**
         * Test whether a number is primes
         */
        public static class MillerRabin {
            Modular modular;
            Power power;

            /**
             * Check whether n is a prime s times
             */
            public boolean mr(int n, int s) {
                if (n == 2) {
                    return true;
                }
                if (n % 2 == 0) {
                    return false;
                }
                modular = new Modular(n);
                power = new Power(modular);
                for (int i = 0; i < s; i++) {
                    int x = RANDOM.nextInt(n - 2) + 2;
                    if (!mr0(x, n)) {
                        return false;
                    }
                }
                return true;
            }

            private boolean mr0(int x, int n) {
                int exp = n - 1;
                while (true) {
                    int y = power.pow(x, exp);
                    if (y != 1 && y != n - 1) {
                        return false;
                    }
                    if (y != 1 || exp % 2 == 1) {
                        break;
                    }
                    exp = exp / 2;
                }
                return true;
            }
        }
    }

    public static class FastIO {
        public final StringBuilder cache = new StringBuilder(1 << 13);
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        public FastIO(InputStream is, OutputStream os) {
            this(is, os, Charset.forName("ascii"));
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public double readDouble() {
            boolean sign = true;
            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 + next - '0';
                next = read();
            }
            if (next != '.') {
                return sign ? val : -val;
            }
            next = read();
            long radix = 1;
            long point = 0;
            while (next >= '0' && next <= '9') {
                point = point * 10 + next - '0';
                radix = radix * 10;
                next = read();
            }
            double result = val + (double) point / radix;
            return sign ? result : -result;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readLine(char[] data, int offset) {
            int originalOffset = offset;
            while (next != -1 && next != '\n') {
                data[offset++] = (char) next;
                next = read();
            }
            return offset - originalOffset;
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

        public void flush() throws IOException {
            os.write(cache.toString().getBytes(charset));
            os.flush();
            cache.setLength(0);
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Debug {
        private boolean allowDebug;

        public Debug(boolean allowDebug) {
            this.allowDebug = allowDebug;
        }

        public void assertTrue(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (!flag) {
                fail();
            }
        }

        public void fail() {
            throw new RuntimeException();
        }

        public void assertFalse(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (flag) {
                fail();
            }
        }

        private void outputName(String name) {
            System.out.print(name + " = ");
        }

        public void debug(String name, int x) {
            if (!allowDebug) {
                return;
            }

            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, long x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, double x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, int[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, long[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, double[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, Object x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, Object... x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.deepToString(x));
        }
    }
}


// import org.junit.Test;
import java.util.Scanner;

// import static org.junit.Assert.assertEquals;

public class CF_1223G {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String args[])
    {
        // test();
        long caseNum = 1;
        while (caseNum-- > 0) {
            task();
        }
    }

    private static int maxN = 500000;
    private static int[] a = new int[maxN + 10];
    private static int[] sum = new int[maxN + 10];
    private static int[] input = new int[maxN + 10];

    public static void task() {
        int n = scanner.nextInt();
        // System.out.println(n);
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        // System.out.println("input end");
        solve(n, input);
    }

    public static void clear(int n) {
        for (int i = 1; i <= maxN + 1; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
    }

    private static int maxA;

    public static long solve(int n, int[] input) {
        long maxArea = 0;
        maxA = 0;
        clear(n);
        for (int i = 0; i < n; i++) {
            a[input[i]]++;
            if (input[i] > maxA) {
                maxA = input[i];
            }
        }
        for (int i = 2; i <= maxN + 1; i++) {
            sum[i] = a[i] + sum[i - 1];
        }

        /*
        {
            int total = 0;
            int y = 60522;
            for (int step = y; step <= maxA; step += y) {
                total += q(step, min(step + y - 1, maxA) + 1) * (step / y);
                System.out.println(total);;
            }
            System.out.println("total");
            System.out.println(total);
        }
         */

        for (int y = 2; y <= maxA; y++) {
            long total = 0;
            for (int step = y; step <= maxA; step += y) {
                total += q(step, min(step + y - 1, maxA) + 1) * (long) (step / y);
            }
            int x = 1;
            int left = 2;
            int right = maxA;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (mid == 500019) {
                    y = y;
                    left = left;
                    mid = mid;
                    right = right;

                }
                if (check(y, mid, total)) {
                    if (mid > x) {
                        x = mid;
                    }
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            if (x > 1) {
                if ((long) x * (long) y > maxArea) {
                    maxArea = (long) x * (long) y;
                    /*
                    System.out.println(y + " " + x);
                    System.out.println(maxArea);
                     */
                }
            }
        }
        System.out.println(maxArea);

        return maxArea;
    }

    public static boolean check(int y, int x, long total) {
        if (y == 60522) {
 //           System.out.println("fcuk");
        }
        boolean ret = check1(y, x, total, x + x) || check2(y, x, total);
        //System.out.println(y + " " + x + " " + total + " " + ret);
        return ret;
    }

    public static boolean check1(int y, int x, long total, int need) {
        // togather
        int up = (need - 1) / y + 1;
        int down = need / y;
        int hits = 0;
        if (need < y) {
            if (q(need, y) > 0) {
                hits = 1;
            }
        }
        if (hits == 1) {
            return total >= x;
        }
        int toSub = 0;
        int remain = need % y;
        for (int step = need - remain; step + remain <= maxA; step += y) {
            if (q(step + remain, min(step + y,maxA + 1)) > 0) {
                toSub = 1;
                break;
            }
        }
        if (toSub == 1) {
            return total >= x + down;
        } else {
            if (q(need, y) > 0) {
                return total >= x + up;
            } else {
                return false;
            }
        }
    }

    private static int min(int x, int y) {
        if (x < y) {
            return x;
        }
        return y;
    }

    public static boolean check2(int y, int x, long total) {
        // separate
        int need = x;
        int up = (need - 1) / y + 1;
        int down = need / y;
        int hits = 0;
        if (need < y) {
            if (q(need, y) > 0) {
                hits = q(need, y);
            }
            if (hits >= 2) {
                hits = 2;
            }
        }
        if (hits == 2) {
            return total >= x;
        }
        if (hits == 1) {
            return check1(y, x, total, x);
        }
        if (q(need, maxA + 1) < 2){
            return false;
        }
        if (hits == 0) {
            int toSub = 0;
            int remain = need % y;
            for (int step = need - remain; step + remain <= maxA; step += y) {
                int h = q(step + remain, min(step + y,maxA + 1));
                toSub += h;
                if (toSub >= 2) {
                    toSub = 2;
                    break;
                }
            }
            if (toSub == 2) {
                return total >= x + down + down;
            }
            if (toSub == 1) {
                return total >= x + down + up;
            }
            if (toSub == 0) {
                return total >= x + up + up;
            }
        }
        // never
        return false;
    }

    public static int q(int begin, int end) {
        if (begin > end) {
            return 0;
        }
        return sum[end - 1] - sum[begin - 1];
    }

    public static void test() {
        int n = 90792;
        int[] input = new int[maxN + 10];
        input[0] = 181565;
        input[1] = 181564;
        for (int i = 2; i < n; i++) {
            input[i] = 60522;
        }
        System.out.println(solve(n, input));
    }

    /*
    @Test
    public void test() {
        int n = 1;
        int[] input = {9};
        assertEquals(4, solve(n, input));
    }

    @Test
    public void test2() {
        String s = "9\n" +
                "9 10 9 18 9 9 9 28 9";
        s = "9\n" +
                "9 10 9 19 9 9 9 28 9";
        Scanner scanner = new Scanner(s);
        int n = scanner.nextInt();
        int [] input = new int[500000 +100];
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        assertEquals(90, solve(n, input));
    }
     */
}


// import org.junit.Test;
import java.util.Scanner;

// import static org.junit.Assert.assertEquals;

public class CF_1223G {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String args[])
    {
         //test();
        long caseNum = 1;
        while (caseNum-- > 0) {
            task();
        }
    }

    private static int maxN = 500000;
    private static int[] a = new int[maxN + 10];
    private static int[] sum = new int[maxN + 10];
    private static int[] input = new int[maxN + 10];

    public static void task() {
        int n = scanner.nextInt();
        // System.out.println(n);
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        // System.out.println("input end");
        solve(n, input);
    }

    public static void clear(int n) {
        for (int i = 1; i <= maxN + 1; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
    }

    private static int maxA;

    public static long solve(int n, int[] input) {
        long maxArea = 0;
        maxA = 0;
        clear(n);
        for (int i = 0; i < n; i++) {
            a[input[i]]++;
            if (input[i] > maxA) {
                maxA = input[i];
            }
        }
        for (int i = 2; i <= maxN + 1; i++) {
            sum[i] = a[i] + sum[i - 1];
        }

        for (int y = 2; y <= maxA; y++) {
            long total = 0;
            for (int step = y; step <= maxA; step += y) {
                total += q(step, min(step + y - 1, maxA) + 1) * (long) (step / y);
            }
            int x = 1;
            int left = 2;
            int right = maxA;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (check(y, mid, total)) {
                    if (mid > x) {
                        x = mid;
                    }
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            if (x > 1) {
                if ((long) x * (long) y > maxArea) {
                    maxArea = (long) x * (long) y;
                    /*
                    System.out.println(y + " " + x);
                    System.out.println(maxArea);
                     */
                }
            }
        }
        System.out.println(maxArea);

        return maxArea;
    }

    public static boolean check(int y, int x, long total) {
        if (y == 4) {
           // System.out.println("fcuk");
        }
        boolean ret = check1(y, x, total, x + x, false) || check2(y, x, total);
        //System.out.println(y + " " + x + " " + total + " " + ret);
        return ret;
    }

    public static boolean check1(int y, int x, long total, int need, boolean ignore) {
        // together
        long up = (need - 1) / y + 1;
        long down = need / y;
        int hits = 0;
        if (!ignore) {
            if (need < y) {
                if (q(need, y) > 0) {
                    hits = 1;
                }
            }
        }
        if (hits == 1) {
            return total >= x;
        }
        int toSub = 0;
        int remain = need % y;
        for (int step = y; step + remain <= maxA; step += y) {
            if (q(step + remain, min(step + y,maxA + 1)) > 0) {
                toSub = 1;
                break;
            }
        }
        if (toSub == 1) {
            return total >= x + down;
        } else {
            if (q(need, maxA + 1) > 0) {
                return total >= x + up;
            } else {
                return false;
            }
        }
    }

    private static int min(int x, int y) {
        if (x < y) {
            return x;
        }
        return y;
    }

    public static boolean check2(int y, int x, long total) {
        // separate
        int need = x;
        int up = (need - 1) / y + 1;
        int down = need / y;
        int hits = 0;
        if (need < y) {
            if (q(need, y) > 0) {
                hits = q(need, y);
            }
            if (hits >= 2) {
                hits = 2;
            }
        }
        if (hits == 2) {
            return total >= x;
        }
        // ?
        if (hits == 1) {
            return check1(y, x, total, x, true);
        }
        if (q(need, maxA + 1) < 2){
            return false;
        }
        if (hits == 0) {
            int toSub = 0;
            int remain = need % y;
            for (int step = y; step + remain <= maxA; step += y) {
                int h = q(step + remain, min(step + y,maxA + 1));
                toSub += h;
                if (toSub >= 2) {
                    toSub = 2;
                    break;
                }
            }
            if (toSub == 2) {
                return total >= x + down + down;
            }
            if (toSub == 1) {
                return total >= x + down + up;
            }
            if (toSub == 0) {
                return total >= x + up + up;
            }
        }
        // never
        return false;
    }

    public static int q(int begin, int end) {
        if (begin > end) {
            return 0;
        }
        return sum[end - 1] - sum[begin - 1];
    }

    public static void test() {
        int n = 90792;
        int[] input = new int[maxN + 10];
        input[0] = 181565;
        input[1] = 181564;
        for (int i = 2; i < n; i++) {
            input[i] = 60522;
        }
        System.out.println(solve(n, input));
    }

    /*
    @Test
    public void test() {
        int n = 1;
        int[] input = {9};
        assertEquals(4, solve(n, input));
    }

    @Test
    public void test2() {
        String s = "9\n" +
                "9 10 9 18 9 9 9 28 9";
        s = "9\n" +
                "9 10 9 19 9 9 9 28 9";
        Scanner scanner = new Scanner(s);
        int n = scanner.nextInt();
        int [] input = new int[500000 +100];
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        assertEquals(90, solve(n, input));
    }
     */
}


// import org.junit.Test;
import java.util.Scanner;

// import static org.junit.Assert.assertEquals;

public class CF_1223G {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String args[])
    {
        // test();
        long caseNum = 1;
        while (caseNum-- > 0) {
            task();
        }
    }

    private static int maxN = 500000;
    private static int[] a = new int[maxN + 10];
    private static int[] sum = new int[maxN + 10];
    private static int[] input = new int[maxN + 10];

    public static void task() {
        int n = scanner.nextInt();
        // System.out.println(n);
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        // System.out.println("input end");
        solve(n, input);
    }

    public static void clear(int n) {
        for (int i = 1; i <= maxN; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
    }

    private static int maxA;

    public static long solve(int n, int[] input) {
        long maxArea = 0;
        maxA = 0;
        clear(n);
        for (int i = 0; i < n; i++) {
            a[input[i]]++;
            if (input[i] > maxA) {
                maxA = input[i];
            }
        }
        for (int i = 2; i <= maxN; i++) {
            sum[i] = a[i] + sum[i - 1];
        }

        /*
        {
            int total = 0;
            int y = 60522;
            for (int step = y; step <= maxA; step += y) {
                total += q(step, min(step + y - 1, maxA) + 1) * (step / y);
                System.out.println(total);;
            }
            System.out.println("total");
            System.out.println(total);
        }
         */

        for (int y = 2; y <= maxA; y++) {
            int total = 0;
            for (int step = y; step <= maxA; step += y) {
                total += q(step, min(step + y - 1, maxA) + 1) * (step / y);
            }
            int x = 1;
            int left = 2;
            int right = maxA;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (mid == 500019) {
                    y = y;
                    left = left;
                    mid = mid;
                    right = right;

                }
                if (check(y, mid, total)) {
                    if (mid > x) {
                        x = mid;
                    }
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            if (x > 1) {
                if ((long) x * (long) y > maxArea) {
                    maxArea = (long) x * (long) y;
                    /*
                    System.out.println(y + " " + x);
                    System.out.println(maxArea);
                     */
                }
            }
        }
        System.out.println(maxArea);

        return maxArea;
    }

    public static boolean check(int y, int x, int total) {
        if (y == 60522) {
 //           System.out.println("fcuk");
        }
        boolean ret = check1(y, x, total, x + x) || check2(y, x, total);
        //System.out.println(y + " " + x + " " + total + " " + ret);
        return ret;
    }

    public static boolean check1(int y, int x, int total, int need) {
        // togather
        int up = (need - 1) / y + 1;
        int down = need / y;
        int hits = 0;
        if (need < y) {
            if (q(need, y) > 0) {
                hits = 1;
            }
        }
        if (hits == 1) {
            return total >= x;
        }
        int toSub = 0;
        int remain = need % y;
        for (int step = need - remain; step + remain <= maxA; step += y) {
            if (q(step + remain, min(step + y,maxA + 1)) > 0) {
                toSub = 1;
                break;
            }
        }
        if (toSub == 1) {
            return total >= x + down;
        } else {
            if (q(need, y) > 0) {
                return total >= x + up;
            } else {
                return false;
            }
        }
    }

    private static int min(int x, int y) {
        if (x < y) {
            return x;
        }
        return y;
    }

    public static boolean check2(int y, int x, int total) {
        // separate
        int need = x;
        int up = (need - 1) / y + 1;
        int down = need / y;
        int hits = 0;
        if (need < y) {
            if (q(need, y) > 0) {
                hits = q(need, y);
            }
            if (hits >= 2) {
                hits = 2;
            }
        }
        if (hits == 2) {
            return total >= x;
        }
        if (hits == 1) {
            return check1(y, x, total, x);
        }
        if (q(need, maxA + 1) < 2){
            return false;
        }
        if (hits == 0) {
            int toSub = 0;
            int remain = need % y;
            for (int step = need - remain; step + remain <= maxA; step += y) {
                int h = q(step + remain, min(step + y,maxA + 1));
                toSub += h;
                if (toSub >= 2) {
                    toSub = 2;
                    break;
                }
            }
            if (toSub == 2) {
                return total >= x + down + down;
            }
            if (toSub == 1) {
                return total >= x + down + up;
            }
            if (toSub == 0) {
                return total >= x + up + up;
            }
        }
        // never
        return false;
    }

    public static int q(int begin, int end) {
        if (begin > end) {
            return 0;
        }
        return sum[end - 1] - sum[begin - 1];
    }

    public static void test() {
        int n = 90792;
        int[] input = new int[maxN + 10];
        input[0] = 181565;
        input[1] = 181564;
        for (int i = 2; i < n; i++) {
            input[i] = 60522;
        }
        System.out.println(solve(n, input));
    }

    /*
    @Test
    public void test() {
        int n = 1;
        int[] input = {9};
        assertEquals(4, solve(n, input));
    }

    @Test
    public void test2() {
        String s = "9\n" +
                "9 10 9 18 9 9 9 28 9";
        s = "9\n" +
                "9 10 9 19 9 9 9 28 9";
        Scanner scanner = new Scanner(s);
        int n = scanner.nextInt();
        int [] input = new int[500000 +100];
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        assertEquals(90, solve(n, input));
    }
     */
}


import java.util.Scanner;

public class Inverse {
    final int N = 32768;
    String string;
    int[] res = new int[N];
    int n, pos;

    int num() {
        if (string.charAt(pos) == 'f') {
            pos += 2;
            int tmp = expression();
            pos++;
            return res[tmp];
        }
        if (string.charAt(pos) == 'n') {
            pos++;
            return n;
        }
        int ret = 0;
        while (Character.isDigit(string.charAt(pos))) {
            ret = ret * 10 + string.charAt(pos) - '0';
            ++pos;
        }
        return ret;
    }

    int term() {
        int ret = num();
        while (string.charAt(pos) == '*' || string.charAt(pos) == '/') {
            char c = string.charAt(pos++);
            int next = num();
            if (c == '*') {
                ret = (ret * next) % N;
            } else {
                ret /= next;
            }
        }
        return ret;
    }

    int expression() {
        int ret = term();
        while (string.charAt(pos) == '+' || string.charAt(pos) == '-') {
            char c = string.charAt(pos++);
            int next = term();
            if (c == '+') {
                ret += next;
                if (ret >= N) ret -= N;
            } else {
                ret -= next;
                if (ret < 0) ret += N;
            }
        }
        return ret;
    }

    boolean logicalExpression() {
        int X = expression();
        char flag = string.charAt(pos++);
        if (flag == '=') pos++;
        int Y = expression();
        if (flag == '<' && X < Y) return true;
        if (flag == '=' && X == Y) return true;
        if (flag == '>' && X > Y) return true;
        return false;
    }

    int calculate() {
        pos = 11;
        while (string.charAt(pos) != '}') {
            if (string.charAt(pos) == 'r') {
                pos += 6;
                return expression();
            } else {
                pos += 3;
                boolean flag = logicalExpression();
                ++pos;
                if (flag == true) {
                    pos += 6;
                    return expression();
                } else {
                    while (string.charAt(pos) != ';') ++pos;
                    ++pos;
                }
            }
        }
        return 0;
    }

    public void solve() {
        Scanner inScanner = new Scanner(System.in);
        string = new String();
        int ans = inScanner.nextInt();
        while (inScanner.hasNext()) {
            string += inScanner.next();
        }
        for (int i = 0; i < N; ++i) {
            n = i;
            res[i] = calculate();
        }
        for (int i = N - 1; i >= 0; --i)
            if (res[i] == ans) {
                System.out.println(i);
                return;
            }
        System.out.println("-1");
    }

    public static void main(String[] args) {
        new Inverse().solve();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length - 1);
        for (int i = 0; i < opers.length - 1; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        //throw new RuntimeException();
        return -1;
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else st.push((a - b + MD)%MD);
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("==");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length);
        for (int i = 0; i < opers.length - 1; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        return -1;
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else st.push((a - b + MD)%MD);
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("==");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.equals("")) return;
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length - 1);
        for (int i = 0; i < opers.length - 1; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        throw new RuntimeException();
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else st.push((a - b + MD)%MD);
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("==");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author kessido
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        GTakeMetro solver = new GTakeMetro();
        solver.solve(1, in, out);
        out.close();
    }

    static class GTakeMetro {
        int n;
        int m;
        int s;
        long t;

        void step() {
            int curJump = (int) ((t--) % n);
            int sign = s < m ? 1 : -1;
            s += sign * curJump;
            s %= n;
            if (s < 0) s += n;
        }

        void doLoop() {
            for (int i = 0; i < n; i++) step();
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            n = in.NextInt();
            m = in.NextInt();
            s = in.NextInt() - 1;
            t = in.NextLong();

            while ((t % n) != 0) step();

            long[] lastSeem = new long[n];
            Arrays.fill(lastSeem, -1);
            while (t != 0) {
                doLoop();
                if (lastSeem[s] != -1) {
                    long interval = lastSeem[s] - t;
                    t %= interval;
                    break;
                } else {
                    lastSeem[s] = t;
                }
            }
            while (t != 0) step();
            out.println(s + 1);
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int NextInt() {
            return Integer.parseInt(next());
        }

        public long NextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author lewin
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        GTakeMetro solver = new GTakeMetro();
        solver.solve(1, in, out);
        out.close();
    }

    static class GTakeMetro {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int s = in.nextInt() - 1;
            long t = in.nextLong();
            while (t % n != 0) {
                if (s < m) {
                    s = (int) ((s + t) % n);
                } else {
                    s = (int) ((s - t) % n);
                    if (s < 0) s += n;
                }
                t--;
            }
            t /= n;
            int ps = -1;
            while (t-- > 0) {
                for (int i = n - 1; i >= 1; i--) {
                    if (s < m) {
                        s = (s + i) % n;
                    } else {
                        s = (s - i + n) % n;
                    }
                }
                if (s == ps) break;
                ps = s;
            }
            out.println(s + 1);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public long nextLong() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            long res = 0L;

            while (c >= 48 && c <= 57) {
                res *= 10L;
                res += (long) (c - 48);
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * (long) sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author kessido
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        GTakeMetro solver = new GTakeMetro();
        solver.solve(1, in, out);
        out.close();
    }

    static class GTakeMetro {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.NextInt();
            int m = in.NextInt();
            int s = in.NextInt() - 1;
            long t = in.NextLong();
            int p = 0;
            HashMap<Long, Integer> hm = new HashMap<>();
            boolean fl = false;
            while (t != 0) {
                int curJump = (int) (t % n);
                t--;
                p++;
                int sign = s < m ? 1 : -1;
                s += sign * curJump;
                s %= n;
                if (s < 0) s += n;
                if (!fl) {
                    long x = (((long) s) << 32) | curJump;
                    Integer res = hm.put(x, p);
                    if (res != null) {
                        int loopLegth = p - res;
                        t %= loopLegth;
                        fl = true;
                    }
                }
            }
            out.println(s + 1);
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int NextInt() {
            return Integer.parseInt(next());
        }

        public long NextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author kessido
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        GTakeMetro solver = new GTakeMetro();
        solver.solve(1, in, out);
        out.close();
    }

    static class GTakeMetro {
        int step(int n, long t, int s, int m) {
            int curJump = (int) (t % n);
            int sign = s < m ? 1 : -1;
            s += sign * curJump;
            s %= n;
            if (s < 0) s += n;
            return s;
        }

        int doLoop(int s, long t, int n, int m) {
            for (int i = 0; i < n; i++) {
                s = step(n, t--, s, m);
            }
            return s;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.NextInt();
            int m = in.NextInt();
            int s = in.NextInt() - 1;
            long t = in.NextLong();
            while (t != 0) {
                s = step(n, t--, s, m);
            }
            long[] lastSeem = new long[n];
            int round = 0;
            Arrays.fill(lastSeem, -1);
            while (t != 0) {
                s = doLoop(s, t, n, m);
                t -= n;
                if (lastSeem[s] != -1) {
                    long interval = lastSeem[s] - t;
                    t %= interval;
                    break;
                } else {
                    lastSeem[s] = round++;
                }
            }
            while (t != 0) {
                s = step(n, t--, s, m);
            }
            out.println(s + 1);
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int NextInt() {
            return Integer.parseInt(next());
        }

        public long NextLong() {
            return Long.parseLong(next());
        }

    }
}


import java.io.*;
import java.nio.Buffer;
import java.util.*;

/**
 * Created by deepshine on 2014/6/5.
 * Codeforces Round 250 Div.2 C
 */
public class Main{

	public static void main(String[] args){
		Scan scan = new Scan();
		int n = scan.nextInt();
		Double[] d = new Double[n];
		for(int i=0;i<n;i++){
			d[i] = scan.nextDouble();
		}
		Arrays.sort(d);
		double fail_chance;
		fail_chance = 1 - d[n-1];
		double result = d[n-1];
		for(int i=n-2;i>=0;i--){
			fail_chance = (1 - d[i]) * fail_chance;
			double temp = 0;
			for(int j=i;j<n;j++){
				temp += fail_chance / (1-d[j]) * d[j];
				//System.out.println("i = "+i+" chance = " + fail_chance[i] / (1-d[j]) * d[j]);
			}

			if(temp > result) result = temp;
		}

		System.out.println(result);

	}

}

class Scan implements Iterator<String>{

	BufferedReader buffer;
	StringTokenizer tok;

	Scan(){
		buffer = new BufferedReader(new InputStreamReader(System.in));
	}

	@Override
	public boolean hasNext(){
		while(tok==null || !tok.hasMoreElements()){
			try{
				tok = new StringTokenizer(buffer.readLine());
			}catch(IOException e){
				return false;
			}
		}
		return true;
	}

	@Override
	public String next(){
		if(hasNext()) return tok.nextToken();
		return null;
	}

	String nextLine(){
		if(hasNext()) return tok.nextToken("\n");
		return null;
	}

	int nextInt(){
		return Integer.parseInt(next());
	}

	double nextDouble(){
		return Double.parseDouble(next());
	}
}


import java.io.*;
import java.util.*;

public class Main{

	public static void main(String[] args){
		Scan scan = new Scan();
		int n = scan.nextInt();
		int[] stack = new int[n];
		int top = 0;
		long result = 0;

		for(int i=0;i<n;i++){
			int temp = scan.nextInt();
			while(top>1 && stack[top-2] >= stack[top-1] && temp >= stack[top-1]){
				result += Math.min(stack[top-2], temp);
				top--;
			}
			stack[top++] = temp;
		}

		for(int i=1;i<--top;i++){
			result += Math.min(stack[i-1], stack[i+1]);
		}

		System.out.println(result);

	}

}

class Scan implements Iterator<String>{

	BufferedReader buffer;
	StringTokenizer tok;

	Scan(){
		buffer = new BufferedReader(new InputStreamReader(System.in));
	}

	@Override
	public boolean hasNext(){
		while(tok==null || !tok.hasMoreElements()){
			try{
				tok = new StringTokenizer(buffer.readLine());
			}catch(Exception e){
				return false;
			}
		}
		return true;
	}

	@Override
	public String next(){
		if(hasNext()) return tok.nextToken();
		return null;
	}

	@Override
	public void remove(){
		throw new UnsupportedOperationException();
	}

	int nextInt(){
		return Integer.parseInt(next());
	}
}


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.math.BigInteger;
import java.util.Collections.*;
import java.util.function.Consumer;

import static java.lang.Math.*;
import static java.lang.Math.min;
import static java.util.Arrays.*;
import static java.math.BigInteger.*;

public class Main{

    void run(){
        Locale.setDefault(Locale.US);
        boolean my;
        try {
            my = System.getProperty("MY_LOCAL") != null;
        } catch (Exception e) {
            my = false;
        }
        try{
            err = System.err;
            if( my ){
                sc  = new FastScanner(new BufferedReader(new FileReader("input.txt")));
//                sc  = new FastScanner(new BufferedReader(new FileReader("C:\\myTest.txt")));
                out = new PrintWriter   (new FileWriter("output.txt"));
            }
            else {
                sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
                out = new PrintWriter(new OutputStreamWriter(System.out));
            }
//            out = new PrintWriter(new OutputStreamWriter(System.out));
        }catch(Exception e){
            MLE();
        }
        if( my )
            tBeg = System.currentTimeMillis();
        solve();
        if( my )
            err.println( "TIME: " + (System.currentTimeMillis() - tBeg ) / 1e3 );
        exit(0);
    }

    void exit( int val ){
        err.flush();
        out.flush();
        System.exit(val);
    }

    double tBeg;
    FastScanner sc;
    PrintWriter out;
    PrintStream err;

    class FastScanner{

        StringTokenizer st;
        BufferedReader br;

        FastScanner( BufferedReader _br ){
            br = _br;
        }

        String readLine(){
            try {
                return br.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        String next(){
            while( st==null || !st.hasMoreElements() )
                st = new StringTokenizer(readLine());
            return st.nextToken();
        }

        int nextInt(){ return Integer.parseInt(next()); }
        long nextLong(){ return Long.parseLong(next()); }
        double nextDouble(){ return Double.parseDouble(next()); }
    }

    void MLE(){
        int[][] arr = new int[1024*1024][]; for( int i = 0; i < 1024*1024; ++i ) arr[i] = new int[1024*1024];
    }

    void MLE( boolean doNotMLE ){ if( !doNotMLE ) MLE(); }

    void TLE(){
        for(;;);
    }

    public static void main(String[] args) {
        new Main().run();
//        new Thread( null, new Runnable() {
//            @Override
//            public void run() {
//                new Main().run();
//            }
//        }, "Lolka", 256_000_000L ).run();
    }

    ////////////////////////////////////////////////////////////////

    int n;
    int[] a, pr, nx;
    PriorityQueue<Integer> Q;
    boolean[] used;

    void solve(){
        n = sc.nextInt();
        a = new int[n];
        pr = new int[n];
        nx = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            pr[i] = i - 1;
            nx[i] = i + 1;
        }

        Q = new PriorityQueue<>();
        used = new boolean[n];
        for (int i = 1; i <= n-2; i++) {
            try_add(i);
        }
        long ans = 0;

        while( !Q.isEmpty() ){
            int i = Q.poll();
            nx[pr[i]] = nx[i];
            pr[nx[i]] = pr[i];
            ans += min( a[pr[i]], a[nx[i]] );
            try_add( pr[i] );
            try_add( nx[i] );
        }

        {
            int i = nx[0];
            while( nx[i] != n ){
                ans += min( a[pr[i]], a[nx[i]] );
                i = nx[i];
            }
        }

        out.println( ans );
    }

    private void try_add(int i) {
        try {
            if( used[i] )return;

            if( 1<=i && i <=n-2 && a[pr[i]] >= a[i] && a[i] <= a[nx[i]] ){
                Q.add( i );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


import java.io.*;
import java.util.*;

public class Main{

	static int[][] chart;

	public static void main(String[] args){
		Scan scan = new Scan();
		int n = scan.nextInt();
		int[] arr = new int[n+1];
		chart = new int[n+1][n+1];
		arr[0] = 0;
		for(int i=1;i<=n;i++) arr[i] = scan.nextInt();
		int result = solve(0, n, arr);
		System.out.println(result);
		//		for(int i=0;i<=n;i++){
		//			for(int j=0;j<=n;j++) System.out.print(chart[i][j]+" ");
		//			System.out.println();
		//		}
	}

	static int solve(int begin, int end, int[] arr){
		if(begin + 2 == end) return 0;
		if(chart[begin][end] != 0) return chart[begin][end];
		int result = 0;
		for(int i = begin+1;i<end;i++){
			int temp = 0;
			temp += solve(begin, i, arr);
			temp += solve(i, end, arr);
			if(begin+1 != i) temp += Math.min(arr[begin], arr[i]);
			if(i+1 != end) temp += Math.min(arr[i], arr[end]);
			if(temp > result) result = temp;
		}
		return chart[begin][end] = result;
	}

}

class Scan implements Iterator<String>{

	BufferedReader buffer;
	StringTokenizer tok;

	Scan(){
		buffer = new BufferedReader(new InputStreamReader(System.in));
	}

	@Override
	public boolean hasNext(){
		while(tok==null || !tok.hasMoreElements()){
			try{
				tok = new StringTokenizer(buffer.readLine());
			}catch(Exception e){
				return false;
			}
		}
		return true;
	}

	@Override
	public String next(){
		if(hasNext()) return tok.nextToken();
		return null;
	}

	@Override
	public void remove(){
		throw new UnsupportedOperationException();
	}

	int nextInt(){
		return Integer.parseInt(next());
	}
}


import java.io.*;
import java.util.*;
import java.math.*;

public class main {
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static PrintWriter pw = new PrintWriter(System.out);
    public static String line;
    public static StringTokenizer st, st2;
    public static ArrayList<ArrayList<Integer>> adjList;
    public static int[] dx = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
    public static int[] dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
    public static int INF = 0x3f3f3f3f;
    public static int MOD = 1000000007;
    public static long LINF = 0x3f3f3f3f3f3f3f3fL;

    public static void main(String[] args) throws Exception {
        int[] A = new int[11];
        for (int i = 0; i < 11; i++) {
            A[i] = Integer.parseInt(br.readLine());
        }

        for (int i = 10; i >= 0; i--) {
            double a = Math.pow(Math.abs(A[i]), 0.5);
            double b = Math.pow(A[i], 3) * 5;
            double res = a + b;

            if (res > 400) {
                System.out.printf("f(%d) = MAGNA NIMIS!\n", A[i]);
            } else {
                System.out.printf("f(%d) = %.2f\n", A[i], res);
            }
        }

        pw.close();
        br.close();
    }
}

class Pair implements Comparable<Pair>{
    public int a, b;

    Pair(int _a, int _b){
        a = _a;
        b = _b;
    }

    public String toString(){
        return a + " " + b;
    }

    public int compareTo(Pair p){
        if (a != p.a) return a < p.a ? -1 : 1;
        if (b != p.b) return b < p.b ? -1 : 1;
        return 0;
    }
}


import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
import static java.lang.System.exit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.StringTokenizer;

public class G {

	static void solve() throws Exception {
		int a[] = new int[11];
		for (int i = 0; i < 11; i++) {
			a[i] = scanInt();
		}
		for (int i = 10; i >= 0; i--) {
			double r = sqrt(abs(a[i])) + pow(a[i], 3) * 5;
			if (r > 400) {
				out.print("f(" + a[i] + ") = MAGNA NIMIS!\n");
			} else {
				out.printf(Locale.US, "f(%d) = %.2f\n", a[i], r);
			}
		}
	}

	static int scanInt() throws IOException {
		return parseInt(scanString());
	}

	static long scanLong() throws IOException {
		return parseLong(scanString());
	}

	static String scanString() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer tok;

	public static void main(String[] args) {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			in.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			exit(1);
		}
	}
}


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigInteger;
import java.net.PortUnreachableException;
import java.util.*;

// written by luchy0120

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

        new Main().run();
    }

    static int groups = 0;
    static int[] fa;
    static int[] sz;

    static void init(int n) {
        groups = n;
        fa = new int[n];
        for (int i = 1; i < n; ++i) {
            fa[i] = i;
        }
        sz = new int[n];
        Arrays.fill(sz, 1);
    }

    static int root(int p) {
        while (p != fa[p]) {
            fa[p] = fa[fa[p]];
            p = fa[p];
        }
        return p;
    }

    static void combine(int p, int q) {
        int i = root(p);
        int j = root(q);
        if (i == j) {
            return;
        }
        if (sz[i] < sz[j]) {
            fa[i] = j;
            sz[j] += sz[i];
        } else {
            fa[j] = i;
            sz[i] += sz[j];
        }
        groups--;
    }

    int child[];
    int color[],dfn[],low[],stack[],cnt[];
    boolean vis[];
    boolean iscut[];
    int deep,top,n,m,sum,ans;
    List<Integer> g[];
    int root = 0;
    //  无向图的强连通分量
    void tarjanNonDirect(int u1) {

        int stk[] = new int[10001];
        int fatk[] = new int[10001];
        int p = 0;
        stk[p] = u1;
        fatk[p] = -1;
        p= 1;
        ot: while(p>0) {
            int u  = stk[p-1];
            int fa = fatk[p-1];

            if(dfn[u]==0) {
                low[u] = dfn[u] = ++deep;
            }
            // stack[top++] = u;

            for (; h[u] != -1; h[u] = ne[h[u]]) {
                int v = to[h[u]];
                if (dfn[v] == 0) {
                    //tarjanNonDirect(v);

                    fatk[p] = u;
                    stk[p++] = v;
                    continue ot;
                } else {
                    low[u] = Math.min(low[u], dfn[v]);
                    // 没有特判是否直接指向父亲
                    // 回边，使用dfn【v】更新low【u】,因为可能是指向父亲，而父亲的low可能比较小
                }
            }
            --p;

            if (fa != -1) {
                low[fa] = Math.min(low[fa], low[u]);
                if (low[u] >= dfn[fa]) {
                    if (fa != root || ++child[fa] > 1) {     // 不是root，直接记为cut，是root，判断是否有两个儿子
                        iscut[fa] = true;
                    }
                    ++dcc_cnt;
                    // 一个割点可能会被多个点双共享
                    //  int z = -1;
                    //    do {
                    //        z = stack[--top];
                    //      dcc[dcc_cnt].add(z);
                    //    } while(z != v);
                    //   dcc[dcc_cnt].add(u);
                }

            }

        }
    }
    int gd = 0;
    int id =-1;
    int scc_cnt = 0;
    int sccno[];
    // 有向图的强连通分量
    void tarjan(int u) {
        int stk[] = new int[10001];
        //   int tptk[] = new int[10001];

        //  int fatk[] = new int[10001];
        int p  =1; stk[0] = u;//fatk[0] =-1;
        ot:while(p>0) {
            u  = stk[p-1];
            //  int fa  = fatk[p-1];
            if (dfn[u] == 0) {
                low[u] = dfn[u] = ++deep;
                stack[top++] = u;

            }

            for (; hc[u] != -1; hc[u] = ne[hc[u]]) {
                int v = to[hc[u]];
                if (dfn[v] == 0) {
                    stk[p++] = v;
                    continue ot;
                    //  low[u] = Math.min(low[u], low[v]);
                } else if (sccno[v] == 0) {
                    // dfn>0 but sccno==0, means it's in current stack
                    low[u] = Math.min(low[u], low[v]);
                }
            }

            p--;
            if (dfn[u] == low[u]) {
                sccno[u] = ++scc_cnt;
                if(u==1){
                    id = scc_cnt;
                }

                while (stack[top - 1] != u) {
                    sccno[stack[top - 1]] = scc_cnt;
                    if(stack[top - 1]==1){
                        id = scc_cnt;
                    }
                    --top;
                }

                --top;
            }
            if(p>=1){
                int fa = stk[p-1];
                low[fa] = Math.min(low[fa], low[u]);
            }
        }
    }
    int rr =0;
//    void solve() {
//
//    }

//    int get_room(int i,int j){
//        return i/3*3 + j/3;
//    }

//    int a[][] = new int[9][9];
//    int space = 0;
//
//    boolean vis_row[][] = new boolean[9][10];
//    boolean vis_col[][] = new boolean[9][10];
//    boolean vis_room[][] = new boolean[9][10];
//    int val[][][] =new int[9][9][];
//    int prepare[][];
//
//    void dfs(int rt){
//
//    }

    int h[],to[],ne[];int ct =0;
    void add(int u,int v){
        // u--;v--;
        to[ct] = v;
        ne[ct] = h[u];
        h[u] = ct++;

    }

    int dcc_cnt = 0;
    int time = 0;

    private static double f(double x) {
        return Math.pow(Math.abs(x), 0.5) + (5*(Math.pow(x, 3)));
    }

    long summ[];
    int v[];
    int hc[];
    void solve() {
//        File f = new File("C:\\Users\\Luqi\\Desktop\\1.png");
//        BufferedImage bi = null;
//        try {
//            bi = ImageIO.read(f);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        int width = bi.getWidth();
//        int height = bi.getHeight();
List<Integer> r = new ArrayList<>();
        while(true){
            int x  =ni();
            if(lenbuf==-1) break;
            r.add(x);
        }

        for(int i=r.size()-1;i>=0;--i){
            int x = r.get(i);
            double y = f(x);
            if( y < 400.0) {
                System.out.printf("f(%d) = %.2f\n", x, y);
            } else {
                System.out.printf("f(%d) = %s\n", x, "MAGNA NIMIS!");
            }
        }

      //  println(width);
//      //  println(height);
//        int i  =ni();
//        int j  =ni();

//        for(int i=0;i<height;++i){
//            for(int j=0;j<width;++j){
//               int me =  bi.getRGB(i,j);
//               print(me==-1?0:1);
//            }
//            println();
//        }

//        int minx = bi.getMinX();
//        int miny = bi.getMinY();
//        System.out.println("width=" + width + ",height=" + height + ".");
//        System.out.println("minx=" + minx + ",miniy=" + miny + ".");
//        int b[][][] = new int[64][64][3];
//
//        for (int i = minx; i < width; i++) {
//            for (int j = miny; j < height; j++) {
//                int pixel = bi.getRGB(i, j);
//              //  print(pixel);
//               // b[j][i] = pixel;
//
//                b[j][i][0] = (pixel & 0xff0000) >> 16;
//                b[j][i][1] = (pixel & 0xff00) >> 8;
//                b[j][i][2] = (pixel & 0xff);
//
//            }
//            println();
//        }
//
//        for(int i=0;i<64;++i){
//            print("\"");
//            for(int j=0;j<64;++j){
//                print(b[i][j][0]<=50?"*":" ");
//            }
//            println("\",");
//        }
//        //println(b[0][0]);

//
//
//
//
//
//            }
//        }
    //    print("No");

//        int n = ni();
//        int m  = n*(n+1)/2;
//        int k = ni();
//        int p = 0;
//
//        double x[] = new double[n];
//        double y[] = new double[n];
//
//        for(int i=0;i<n;++i){
//            x[i] = nd();
//            y[i] = nd();
//        }
//
//        List<Item> li = new ArrayList<>();
//        for(int i=0;i<n;++i){
//            for(int j=i+1;j<n;++j){
//               double r =  (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
//               li.add(new Item(r,i,j));
//            }
//        }
//        Collections.sort(li,(xx,yy)->Double.compare(xx.s,yy.s));
//
//        init(n + 1);
//
//        int s = 0;
//        int c = n;
//        for (int i = 0; i < li.size(); ++i) {
//            int p1 = li.get(i).a;
//            int p2 = li.get(i).b;
//            if (root(p1) != root(p2)) {
//                combine(p1, p2);
//                c--;
//                if(c==k-1){
//                    print(roundS(Math.sqrt(li.get(i).s),2));
//                    break;
//                }
//            }
//        }

    }
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//    }

    static long mul(long a, long b, long p)
    {
        long res=0,base=a;
        while(b>0)
        {
            if((b&1L)>0)
                res=(res+base)%p;
            base=(base+base)%p;
            b>>=1;
        }
        return res;
    }

    static long mod_pow(long k,long n,long p){
        long res = 1L;
        long temp = (k%p+p)%p;
        while(n!=0L){
            if((n&1L)==1L){
                res = ((res*temp)%p+p)%p;
            }
            temp = ((temp * temp)%p+p)%p;
            n = n>>1L;
        }
        return res%p;
    }
    public static String roundS(double result, int scale){
        String fmt = String.format("%%.%df", scale);
        return String.format(fmt, result);
    }

//    void solve() {
//
//        for(int i=0;i<9;++i) {
//            for (int j = 0; j < 9; ++j) {
//                int v = ni();
//                a[i][j] = v;
//                if(v>0) {
//                    vis_row[i][v] = true;
//                    vis_col[j][v] = true;
//                    vis_room[get_room(i, j)][v] = true;
//                }else{
//                    space++;
//                }
//            }
//        }
//
//
//        prepare = new int[space][2];
//
//        int p = 0;
//
//        for(int i=0;i<9;++i) {
//            for (int j = 0; j < 9; ++j) {
//                if(a[i][j]==0){
//                    prepare[p][0] = i;
//                    prepare[p][1]= j;p++;
//                    List<Integer> temp =new ArrayList<>();
//                    for(int k=1;k<=9;++k){
//                        if(!vis_col[j][k]&&!vis_row[i][k]&&!vis_room[get_room(i,j)][k]){
//                            temp.add(k);
//                        }
//                    }
//                    int sz = temp.size();
//                    val[i][j] = new int[sz];
//                    for(int k=0;k<sz;++k){
//                        val[i][j][k] = temp.get(k);
//                    }
//                }
//            }
//        }
//        Arrays.sort(prepare,(x,y)->{
//           return Integer.compare(val[x[0]][x[1]].length,val[y[0]][y[1]].length);
//        });
//        dfs(0);
//
//
//
//
//
//
//
//
//
//
//    }

    InputStream is;
    PrintWriter out;

    void run() throws Exception {
        //is = new FileInputStream(new File("C:\\Users\\Luqi\\Downloads\\P3387_9.in"));
        is = System.in;
        out = new PrintWriter(System.out);

        solve();
        out.flush();
    }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private char ncc() {
        int b = readByte();
        return (char) b;
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private String nline() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!isSpaceChar(b) || b == ' ') {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[][] nm(int n, int m) {
        char[][] a = new char[n][];
        for (int i = 0; i < n; i++) a[i] = ns(m);
        return a;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private long[] nal(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) a[i] = nl();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) {
        }
        ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while (true) {
            if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0');
            else return minus ? -num : num;
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) {
        }
        ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while (true) {
            if (b >= '0' && b <= '9') num = num * 10 + (b - '0');
            else return minus ? -num : num;
            b = readByte();
        }
    }

    void print(Object obj) {
        out.print(obj);
    }

    void println(Object obj) {
        out.println(obj);
    }

    void println() {
        out.println();
    }

    void printArray(int a[],int from){
        int l = a.length;
        for(int i=from;i<l;++i){
            print(a[i]);
            if(i!=l-1){
                print(" ");
            }
        }
        println();
    }

    void printArray(long a[],int from){
        int l = a.length;
        for(int i=from;i<l;++i){
            print(a[i]);
            if(i!=l-1){
                print(" ");
            }
        }
        println();
    }
}


import java.util.*;
public class g {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		Stack<Integer> s=new Stack<>();
		for(int i=0;i<11;i++)s.push(in.nextInt());
		while(!s.isEmpty()) {
			int on=s.pop();
			double a = Math.sqrt(Math.abs(on));
			double b = on*on*on*5;
			double r=a + b;
			if (r > 400)System.out.printf("f(%d) = MAGNA NIMIS!\n", on);
			else System.out.printf("f(%d) = %.2f\n", on,r);
		}
	}

}