#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char* argv[]) {
    registerTestlibCmd(argc, argv);

    // 从输入文件读取价格及 n
    vector<int> cost(11);
    for (int i = 1; i <= 10; i++) {
        cost[i] = inf.readInt();
    }
    int n = inf.readInt();

    // DP 计算最小费用
    const int INF = 1e9;
    vector<int> dp(n + 1, INF);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int d = 1; d <= 10 && d <= i; d++) {
            dp[i] = min(dp[i], dp[i - d] + cost[d]);
        }
    }
    int best = dp[n];

    // 读取选手输出的所有整数（按顺序）
    vector<int> nums;
    while (!ouf.seekEof()) {
        nums.push_back(ouf.readInt());
    }

    // 格式校验：至少要有 (1段距离 + 1段价格 + 总费用) 共3个整数，且总数应为奇数
    if ((int)nums.size() < 3 || ((int)nums.size() % 2 == 0)) {
        quitf(_wa, "Invalid number of integers: %d (must be odd and ≥ 3)", (int)nums.size());
    }

    int totalDist = 0;
    int totalPrice = 0;
    int segCnt = ((int)nums.size() - 1) / 2;   // 段数

    // 逐段校验
    for (int i = 0; i < segCnt; i++) {
        int d = nums[2 * i];
        int p = nums[2 * i + 1];

        if (d < 1 || d > 10) {
            quitf(_wa, "Distance %d out of range [1, 10]", d);
        }
        if (p != cost[d]) {
            quitf(_wa, "Price for distance %d should be %d, but got %d", d, cost[d], p);
        }
        totalDist += d;
        totalPrice += p;
    }

    int totalCost = nums.back();

    // 距离总和校验
    if (totalDist != n) {
        quitf(_wa, "Sum of distances is %d, expected %d", totalDist, n);
    }

    // 总费用校验（各段之和 vs 最后的输出值）
    if (totalPrice != totalCost) {
        quitf(_wa, "Sum of segment prices (%d) does not equal final total (%d)", totalPrice, totalCost);
    }

    // 最优性校验
    if (totalCost != best) {
        quitf(_wa, "Total cost %d is not optimal, optimal is %d", totalCost, best);
    }

    quitf(_ok, "Accepted. Minimal cost = %d", totalCost);
    return 0;
}