1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <cstdio>
#include <cstdint>
#include <cassert>

#include <vector>

static int64_t thickness[1000 * 1000];
static int64_t credits[2 * 1000 * 1000 + 2];

static std::vector<std::pair<int64_t, int64_t>> factorize(int64_t x) {
    std::vector<std::pair<int64_t, int64_t>> ret;
    assert(x > 0);
    if (x % 2 == 0) {
        auto p = std::pair<int64_t, int64_t>(2, 0);
        do {
            p.second++;
            x /= 2;
        } while (x % 2 == 0);
        ret.push_back(p);
    }
    for (int64_t i = 3; i * i <= x; i += 2) {
        if (x % i == 0) {
            auto p = std::pair<int64_t, int64_t>(i, 0);
            do {
                p.second++;
                x /= i;
            } while (x % i == 0);
            ret.push_back(p);
        }
    }
    if (x > 1) {
        ret.push_back({x, 1});
    }
    return ret;
}

static bool check_width(int n, int64_t width) {
    if (width > n) {
        // fprintf(stderr, "Rejecting %lld > %d right away\n", (long long int)width, n);
        return false;
    }

    // fprintf(stderr, "Checking width candidate %lld\n", (long long int)width);

    for (int i = 0; i < width; i++) {
        credits[i] = 0;
    }

    int64_t prev_thickness = 0;
    for (int i = 0; i <= n; i++) {
        const int64_t delta = thickness[i] - prev_thickness;
        const int64_t balance = credits[i] + delta;
        // fprintf(stderr, "[%d] delta: %lld, balance: %lld\n", i, (long long int)delta, (long long int)balance);
        if (balance < 0) {
            // fprintf(stderr, "  Went below zero\n");
            return false;
        }
        credits[i + width] = balance;
        prev_thickness = thickness[i];
    }

    // fprintf(stderr, "Looks ok!\n");
    return true;
}

uint64_t search(int n, int64_t sum) {
    struct search_context_t {
        int n;
        int64_t best_candidate = 1;
        std::vector<std::pair<int64_t, int64_t>> factors;

        void do_search(int prime_id, int64_t curr_product, int64_t remaining_product) {
            // auto print_padding = [&] {
            //     for (int i = 0; (size_t)i < (factors.size() - (prime_id + 1)); i++) {
            //         fputc(' ', stderr);
            //     }
            // };
            
            // print_padding();
            // fprintf(stderr, "do_search(%d, %lld, %lld)\n", prime_id, (long long int)curr_product, (long long int)remaining_product);
            if (curr_product * remaining_product <= best_candidate) {
                // print_padding();
                // fprintf(stderr, "Branch has been cut off\n");
                return;
            }

            if (prime_id == -1) {
                assert(remaining_product == 1);
                if (check_width(n, curr_product)) {
                    best_candidate = curr_product;
                }
                return;
            }

            auto [prime, power] = factors[prime_id];
            int64_t powers_of_prime[64];
            powers_of_prime[0] = 1;
            for (int i = 1; i <= power; i++) {
                powers_of_prime[i] = powers_of_prime[i-1] * prime;
            }
            remaining_product /= powers_of_prime[power];
            for (int exp = power; exp >= 0; exp--) {
                do_search(prime_id - 1, curr_product * powers_of_prime[exp], remaining_product);
            }
        }
    };

    search_context_t ctx {
        .n = n,
        .factors = factorize(sum),
    };
    // fprintf(stderr, "Factors:");
    // for (auto [factor, power] : ctx.factors) {
    //     fprintf(stderr, " %lld^%lld", (long long int)factor, (long long int)power);
    // }
    // fprintf(stderr, "\n");
    ctx.do_search(ctx.factors.size() - 1, 1, sum);
    return ctx.best_candidate;
}

int main() {
    int n;
    scanf("%d", &n);

    int64_t sum = 0;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        thickness[i] = a;
        sum += a;
    }
    thickness[n] = 0;

    printf("%lld\n", (long long int)search(n, sum));

    return 0;
}