結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー Kanten4205Kanten4205
提出日時 2021-06-01 00:02:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 3,000 ms
コード長 23,423 bytes
コンパイル時間 3,168 ms
コンパイル使用メモリ 206,168 KB
実行使用メモリ 43,228 KB
最終ジャッジ日時 2023-08-21 11:06:45
合計ジャッジ時間 18,226 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 104 ms
19,568 KB
testcase_05 AC 250 ms
30,148 KB
testcase_06 AC 112 ms
15,972 KB
testcase_07 AC 150 ms
20,244 KB
testcase_08 AC 207 ms
15,892 KB
testcase_09 AC 110 ms
7,400 KB
testcase_10 AC 121 ms
17,516 KB
testcase_11 AC 138 ms
18,840 KB
testcase_12 AC 217 ms
27,716 KB
testcase_13 AC 79 ms
12,300 KB
testcase_14 AC 91 ms
18,852 KB
testcase_15 AC 34 ms
6,432 KB
testcase_16 AC 119 ms
23,792 KB
testcase_17 AC 73 ms
5,700 KB
testcase_18 AC 262 ms
23,592 KB
testcase_19 AC 132 ms
15,472 KB
testcase_20 AC 61 ms
6,072 KB
testcase_21 AC 163 ms
31,448 KB
testcase_22 AC 174 ms
26,684 KB
testcase_23 AC 184 ms
10,868 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 8 ms
4,376 KB
testcase_29 AC 9 ms
4,376 KB
testcase_30 AC 6 ms
4,376 KB
testcase_31 AC 6 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 8 ms
4,376 KB
testcase_34 AC 6 ms
4,376 KB
testcase_35 AC 14 ms
4,376 KB
testcase_36 AC 11 ms
4,380 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 7 ms
4,376 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 5 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 5 ms
4,376 KB
testcase_43 AC 8 ms
4,376 KB
testcase_44 AC 3 ms
4,376 KB
testcase_45 AC 3 ms
5,908 KB
testcase_46 AC 5 ms
4,380 KB
testcase_47 AC 8 ms
5,756 KB
testcase_48 AC 13 ms
10,048 KB
testcase_49 AC 12 ms
6,752 KB
testcase_50 AC 29 ms
9,740 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 30 ms
9,772 KB
testcase_53 AC 10 ms
6,752 KB
testcase_54 AC 3 ms
4,376 KB
testcase_55 AC 20 ms
8,780 KB
testcase_56 AC 4 ms
4,380 KB
testcase_57 AC 20 ms
7,116 KB
testcase_58 AC 12 ms
6,596 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 4 ms
4,852 KB
testcase_61 AC 6 ms
4,380 KB
testcase_62 AC 13 ms
6,180 KB
testcase_63 AC 9 ms
4,700 KB
testcase_64 AC 275 ms
32,380 KB
testcase_65 AC 5 ms
4,380 KB
testcase_66 AC 143 ms
43,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
#define logn long
#define lnog long
#define lgon long
#define itn int
typedef long long ll;
typedef pair<long long, long long> P;
const long logn INF = 1e18;
//vector
//rep : A を N 回繰り返してくっつける
template <typename T>
vector<T> rep(vector<T>& A, long long N) {
    vector<T> B;
    for (long long i = 0; i < N; i++) {
        for (long long j = 0; j < A.size(); j++) {
            B.push_back(A[j]);
        }
    }
    return B;
}
//count_arr : A の中に key がいくつ含まれているか
template <typename T>
long long count_arr(vector<T>& A, T key) {
    long long ans = 0;
    for (long long i = 0; i < A.size(); i++) {
        ans += (A.at(i) == key);
    }
    return ans;
}
//join : A の各要素を ch で結合した文字列
template <typename Q>
string join(vector<Q>& A, char ch) {
    string S;
    for (long long i = 0; i < A.size(); i++) {
        string T = to_string(A.at(i));
        long long j = 0;
        while (j < T.size()) {
            S.push_back(T[j]);
            j++;
        }
        if (i != A.size() - 1) S.push_back(ch);
    }
    return S;
}
//sorted : A をソートした配列を返す
template<typename T>
vector<T> sorted(vector<T>& A) {
    vector<T>B = A;
    sort(B.begin(), B.end());
    return B;
}
//reversed : A を反転させた配列を返す
template<typename T>
vector<T> reversed(vector<T>& A) {
    vector<T>B = A;
    reverse(B.begin(), B.end());
    return B;
}
//max_arr : A の中の最大値を返す
template<typename T>
T max_arr(vector<T>& A) {
    long long num = -10000000000000007;
    for (long long i = 0; i < A.size(); i++) {
        if (A[i] > num) num = A[i];
    }
    return num;
}
//min_arr : A の中の最小値を返す
template<typename T>
T min_arr(vector<T>& A) {
    long long num = 10000000000000007;
    for (long long i = 0; i < A.size(); i++) {
        if (A[i] < num) num = A[i];
    }
    return num;
}

//Data stracture
//UnionFind
struct UnionFind {
    vector<long long> par, siz;

    //初期化
    UnionFind(long long N) : par(N, -1), siz(N, 1) {}

    //根を求める
    long long root(long long x) {
        if (par[x] == -1) return x; //x自身が根である
        else return par[x] = root(par[x]);
    }

    //x と y が同じグループに属するかどうか
    bool same(long long x, long long y) {
        return root(x) == root(y);
    }

    //x を含むグループと y を含むグループを併合
    bool unite(long long x, long long y) {
        //x, y をそれぞれ根まで移動
        x = root(x); y = root(y);

        //既に x と y が同グループに属するなら false
        if (x == y) return false;

        //union by size
        if (siz[x] < siz[y]) swap(x, y);

        //y を x の子にする
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }

    //x を含むグループのサイズ
    long long size(long long x) {
        return siz[root(x)];
    }
    //連結成分の個数をカウント
    long long count() {
        long long ans = 0;
        for (long long i = 0; i < (long long)par.size(); i++) {
            ans += (root(i) == i);
        }
        return ans;
    }
};
//区間最小値を求めるセグ木
template <typename T>
struct RMQ {
    const T INF = 2147483647;
    int N; //number of leaves
    vector<T> dat; //perfect binary tree

    RMQ(int n_) : N(), dat(n_ * 4, INF) { //The number of leaves can be displayed by the formula, 2 ^ x.
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        N = x;
    }

    void update(long long i, T x) {
        i += N - 1;
        dat.at(i) = x;
        while (i > 0) {
            i = (i - 1) / 2; //parent
            dat.at(i) = min(dat.at(i * 2 + 1), dat.at(i * 2 + 2));
        }
    }

    //the minimum element of [a, b)
    T query(long long a, long long b) {
        return query_sub(a, b, 0, 0, N);
    }
    T query_sub(long long a, long long b, long long k, long long l, long long r) {
        if (r <= a || b <= l) {
            return INF;
        }
        else if (a <= l && r <= b) {
            return dat.at(k);
        }
        else {
            T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return min(vl, vr);
        }
    }
};

//関数
//input_arr : 配列 A を入力
template <typename T>
void input_arr(vector<T>& A, long long N) {
    for (long long i = 0; i < N; i++) {
        cin >> A[i];
    }
}
//output_arr : 配列 A を出力
template <typename T>
void output_arr(vector<T>& A, long long N) {
    for (long long i = 0; i < N; i++) {
        cout << A[i];
        if (i != N - 1) cout << ' ';
    }
    cout << endl;
}
//GCD : GCD
long long GCD(long long A, long long B) { return(B == 0 ? A : GCD(B, A % B)); }
//LCM : LCM
long long LCM(long long A, long long B) { return A / GCD(A, B) * B; }
//elements : Aに含まれる要素は何種類か
template <typename T>
long long elements(vector<T>& A) {
    sort(A.begin(), A.end());
    long long ans = 1;
    for (long long i = 1; i < A.size(); i++) {
        if (A[i] != A[i - 1]) ans++;
    }
    return ans;
}
//prime_factorization : 素因数分解
vector<P> prime_factorization(long long N) {
    vector<P>A;
    long long tmp = N;
    for (long long i = 2; i * i <= N; i++) {
        if (tmp % i == 0) {
            A.push_back({ i, 0 });
            while (tmp % i == 0) {
                A.back().second++;
                tmp /= i;
            }
        }
    }
    if (tmp != 1) A.push_back({ tmp, 1 });
    return A;
}
//power : M^N(mod MOD)
long long power(long long N, long long M, long long MOD) {
    long long ans = 1;
    while (M > 0) {
        if (M & 1) {
            ans = ans * N % MOD;
        }
        N = N * N % MOD;
        M >>= 1;
    }
    return ans;
}
//Euler_phi : オイラー関数
long long Euler_phi(long long N) {
    vector<P>A = prime_factorization(N);
    for (long long i = 0; i < A.size(); i++) {
        N /= A[i].first;
    }
    for (long long i = 0; i < A.size(); i++) {
        N *= (A[i].first - 1);
    }
    return N;
}
//extgcd : 拡張ユークリッド互除法(返すのは gcd(A,B),AX + BY = GCD(A,B))
long long extgcd(long long A, long long B, long long& X, long long& Y) {
    long long D = A;
    if (B != 0) {
        D = extgcd(B, A % B, Y, X);
        Y -= (A / B) * X;
    }
    else {
        X = 1; Y = 0;
    }
    return D;
}

//計算幾何
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
//Point : 点(x, y)(点の座標(x,y)、ベクトル演算(+,-,*,/)、ベクトルのノルム(norm)、大きさ(abs)、大小比較(<)、合同を保存(==))
class Point {
public:
    double x;
    double y;

    Point(double x = 0, double y = 0) : x(x), y(y) {}

    Point operator+(Point p) { return Point(x + p.x, y + p.y); }
    Point operator-(Point p) { return Point(x - p.x, y - p.y); }
    Point operator*(double a) { return Point(a * x, a * y); }
    Point operator/(double a) { return Point(x / a, y / a); }

    double abs() { return sqrt(norm()); }
    double norm() { return x * x + y * y; }

    bool operator<(const Point& p) const {
        return x != p.x ? x < p.x : y < p.y;
    }
    bool operator==(const Point& p) const {
        return fabs(x - p.x) < (1e-10) && fabs(y - p.y) < (1e-10);
    }
};
//Vector : ベクトル(配列のvectorと間違えぬように)
typedef Point Vector;
//Segment : 線分AB(点A, Bの座標を保存)
struct Segment {
    Point A;
    Point B;
};
//Line : 直線AB
typedef Segment Line;
//Circle : 円(中心点C、半径r を保存)
class Circle {
public:
    Point C;
    double r;
    Circle(Point C = Point(), double r = 0.0) : C(C), r(r) {}
};
//Polygon : 多角形(各頂点の点列で表す)
typedef vector<Point> Polygon;
//dot : ベクトルA, B の内積
double dot(Vector A, Vector B) {
    return A.x * B.x + A.y * B.y;
}
//cross : ベクトルA, B の外積
double cross(Vector A, Vector B) {
    return A.x * B.y - A.y * B.x;
}
//Projection : 射影(点pを通る、直線Sへの垂線の足の座標)
Point Projection(Segment S, Point p) {
    Vector base = S.B - S.A;
    double r = dot(p - S.A, base) / base.norm();
    return S.A + base * r;
}
//Reflection : 反射(直線Sに関して、点pと対称な点の座標)
Point Reflection(Segment S, Point p) {
    return p + (Projection(S, p) - p) * 2.0;
}
//isOrthogonal : 直交しているか? <==> ベクトルA, B の内積は0か?
bool isOrthogonal(Vector A, Vector B) {
    return equals(dot(A, B), 0.0);
}
bool isOrthogonal(Point A1, Point A2, Point B1, Point B2) {
    return isOrthogonal(A1 - A2, B1 - B2);
}
bool isOrthogonal(Segment S1, Segment S2) {
    return equals(dot(S1.B - S1.A, S2.B - S2.A), 0.0);
}
//isParallel : 平行か? <==> ベクトルA, B の外積は0か?
bool isParallel(Vector A, Vector B) {
    return equals(cross(A, B), 0.0);
}
bool isParallel(Point A1, Point A2, Point B1, Point B2) {
    return isParallel(A1 - A2, B1 - B2);
}
bool isParallel(Segment S1, Segment S2) {
    return equals(cross(S1.B - S1.A, S2.B - S2.A), 0.0);
}
//getDistance : 2点A,Bの距離
double getDistance(Point A, Point B) {
    return (A - B).abs();
}
//getDistanceLP : 直線Lと点Pの距離
double getDistanceLP(Line L, Point P) {
    return abs((cross(L.B - L.A, P - L.A))) / (L.B - L.A).abs();
}
//getDistanceSP : 線分Sと点Pの距離
double getDistanceSP(Segment S, Point P) {
    if (dot(S.B - S.A, P - S.A) < 0.0) return (P - S.A).abs();
    if (dot(S.A - S.B, P - S.B) < 0.0) return (P - S.B).abs();
    return getDistanceLP(S, P);
}
//定数定義
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
//ccw : 3点A,B,Cがこの順に反時計回りか?
int ccw(Point A, Point B, Point C) {
    Vector S = B - A, T = C - A;
    if (cross(S, T) > EPS) return COUNTER_CLOCKWISE; //反時計回り
    if (cross(S, T) < -EPS) return CLOCKWISE; //時計回り
    if (dot(S, T) < -EPS) return ONLINE_BACK; //C,A,Bの順で同一直線上にある
    if (S.norm() < T.norm()) return ONLINE_FRONT; //A,B,Cの順で同一直線上にある
    return ON_SEGMENT; //Cが線分AB上にある
}
//intersect : 線分AB, CDは交差するか?
bool intersect(Point A, Point B, Point C, Point D) {
    return (ccw(A, B, C) * ccw(A, B, D) <= 0 && ccw(C, D, A) * ccw(C, D, B) <= 0);
}
bool intersect(Segment S1, Segment S2) {
    return intersect(S1.A, S1.B, S2.A, S2.B);
}
//別のintersect : 円Cと直線Lが交差するかどうか
bool intersect(Circle C, Line L) {
    return (getDistanceLP(L, C.C) <= C.r);
}
//getDistance : 線分S1, S2 の距離もこの関数で導入
double getDistance(Segment S1, Segment S2) {
    if (intersect(S1, S2)) return 0.0;
    return min(min(getDistanceSP(S1, S2.A), getDistanceSP(S1, S2.B)), min(getDistanceSP(S2, S1.A), getDistanceSP(S2, S1.B)));
}//getCrossPoint : 線分S1, S2 の交点(線分AB, 線分CD の交点)
Point getCrossPoint(Segment S1, Segment S2) {
    Vector base = S2.B - S2.A;
    double d1 = abs(cross(base, S1.A - S2.A));
    double d2 = abs(cross(base, S1.B - S2.A));
    double T = d1 / (d1 + d2);
    return S1.A + (S1.B - S1.A) * T;
}
Point getCrossPoint(Point A, Point B, Point C, Point D) {
    Vector base = D - C;
    double d1 = abs(cross(base, A - C));
    double d2 = abs(cross(base, B - C));
    double T = d1 / (d1 + d2);
    return A + (B - A) * T;
}
//getCrossPoints : 円Cと直線Lの交点(2つ以下のため、pairで管理)
pair<Point, Point> getCrossPoints(Circle C, Line L) {
    assert(intersect(C, L));
    Vector pr = Projection(L, C.C);
    Vector e = (L.B - L.A) / (L.B - L.A).abs();
    double base = sqrt(C.r * C.r - (pr - C.C).norm());
    return make_pair(pr + e * base, pr - e * base);
}
//グラフを表す構造体
struct Graph {
    //辺を表す構造体
    //rev : 逆辺 (to, from) が G[to] の中で何番目の要素か
    //cap : 辺 (from, to) の容量
    struct Edge {
        long long rev, from, to, cap;
        Edge(long long r, long long f, long long t, long long c) :
            rev(r), from(f), to(t), cap(c) {}
    };

    //隣接リスト
    vector<vector<Edge>> list;

    //N : 頂点数
    Graph(long long N = 0) : list(N) {}

    //グラフの頂点数取得
    size_t size() {
        return list.size();
    }

    //Graph インスタンスを G として、
    // G.list[v] を G[v] と書けるようにしておく
    vector<Edge>& operator [] (long long i) {
        return list[i];
    }

    //辺 e = (u, v) の逆辺 (v, u) を取得する
    Edge& redge(const Edge& e) {
        return list[e.to][e.rev];
    }

    //辺 e = (u, v) に流量 f のフローを流す
    //e = (u, v) の流量が f だけ減少する
    //この時、逆辺 (v, u) の流量を増やす
    void run_flow(Edge& e, long long f) {
        e.cap -= f;
        redge(e).cap += f;
    }

    //頂点 from から頂点 to へ容量 cap の辺を張る
    //このとき to から from へも容量 0 の辺を張っておく
    void addedge(long long from, long long to, long long cap) {
        long long fromrev = (long long)list[from].size();
        long long torev = (long long)list[to].size();
        list[from].push_back(Edge(torev, from, to, cap));
        list[to].push_back(Edge(fromrev, to, from, 0));
    }
};
struct Ford_Fulkerson {
    static const int INF = 1 << 30;
    vector<long long> seen;

    Ford_Fulkerson() {}

    //残余グラフ上で s-t パスを見つける(DFS)
    //返り値は s-t パス上の容量の最小値(見つからなかったら 0)
    //f: s から v へ到達した過程の各辺の容量の最小値
    long long fodfs(Graph& G, long long v, long long t, long long f) {
        //終端 t に到達したらリターン
        if (v == t) return f;

        //DFS
        seen[v] = true;
        for (auto& e : G[v]) {
            if (seen[e.to]) continue;

            //容量 0 の辺は実際には存在しない
            if (e.cap == 0) continue;

            //s-t パスを探す
            //見つかったら flow はパス上の最小容量
            //見つからなかったら f = 0
            long long flow = fodfs(G, e.to, t, min(f, e.cap));

            //s-t パスが見つからなかったら次辺を試す
            if (flow == 0) continue;

            //辺 e に容量 flow のフローを流す
            G.run_flow(e, flow);

            //s-t パスを見つけたらパス上最小容量を返す
            return flow;
        }
        //s-t パスが見つからなかったことを示す
        return 0;
    }

    //グラフ G の s-t 間の最大流量を求める
    //ただしリターン時に G は残余グラフになる
    long long solve(Graph& G, long long s, long long t) {
        long long res = 0;

        //残余グラフに s-t パスがなくなるまで反復
        while (true) {
            seen.assign((long long)G.size(), 0);
            long long flow = fodfs(G, s, t, INF);

            //s-t パスが見つからなかったら終了
            if (flow == 0) return res;

            //答えを加算
            res += flow;
        }

        //no reach
        return 0;
    }
};
bool isprime(long long N) {
    if (N < 2) return false;
    for (long long i = 2; i * i <= N; i++) {
        if (N % i == 0) return false;
    }
    return true;
}
struct EDGE {
    long long to;
    long long cost;
};
void dijkstra(const vector<vector<EDGE>>& G, int s, vector<long long>& dis, vector<long long>& prev) {
    int N = G.size();
    dis.resize(N, INF);
    prev.resize(N, -1); // 初期化
    priority_queue<P, vector<P>, greater<P>> pq;
    dis[s] = 0;
    pq.emplace(dis[s], s);
    while (!pq.empty()) {
        P p = pq.top();
        pq.pop();
        int v = p.second;
        if (dis[v] < p.first) {
            continue;
        }
        for (auto& e : G[v]) {
            if (dis[e.to] > dis[v] + e.cost) {
                dis[e.to] = dis[v] + e.cost;
                prev[e.to] = v; // 頂点 v を通って e.to にたどり着いた
                pq.emplace(dis[e.to], e.to);
            }
        }
    }
}
//find : A 内に key のある位置(ないなら -1 )
long long find(vector<long long>& A, long long key) {
    if (A.empty()) return -1;
    long long left = 0, right = A.size() - 1;
    while (left <= right) {
        long long mid = (left + right) / 2;
        if (A[mid] == key) return mid;
        else if (A[mid] < key) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}
void BFS(vector<vector<long long>>& G, long long s, vector<long long>& dist) {
    queue<long long>Q;
    dist[s] = 0; Q.push(s);
    while (!Q.empty()) {
        long long v = Q.front(); Q.pop();
        for (auto nv : G[v]) {
            if (dist[nv] == INF) {
                dist[nv] = dist[v] + 1;
                Q.push(nv);
            }
        }
    }
}
vector<int> string_to_bigint(string S) {
    int N = S.size(); // N = (文字列 S の長さ)
    vector<int> digit(N);
    for (int i = 0; i < N; ++i) {
        digit[i] = S[N - i - 1] - '0'; // 10^i の位の数
    }
    return digit;
}
string bigint_to_string(vector<int> digit) {
    int N = digit.size(); // N = (配列 digit の長さ)
    string str = "";
    for (int i = N - 1; i >= 0; --i) {
        str += digit[i] + '0';
    }
    return str;
}
vector<int> carry_and_fix(vector<int> digit) {
    int N = digit.size();
    for (int i = 0; i < N - 1; ++i) {
        // 繰り上がり処理 (K は繰り上がりの回数)
        if (digit[i] >= 10) {
            int K = digit[i] / 10;
            digit[i] -= K * 10;
            digit[i + 1] += K;
        }
        // 繰り下がり処理 (K は繰り下がりの回数)
        if (digit[i] < 0) {
            int K = (-digit[i] - 1) / 10 + 1;
            digit[i] += K * 10;
            digit[i + 1] -= K;
        }
    }
    // 一番上の桁が 10 以上なら、桁数を増やすことを繰り返す
    while (digit.back() >= 10) {
        int K = digit.back() / 10;
        digit.back() -= K * 10;
        digit.push_back(K);
    }
    // 1 桁の「0」以外なら、一番上の桁の 0 (リーディング・ゼロ) を消す
    while (digit.size() >= 2 && digit.back() == 0) {
        digit.pop_back();
    }
    return digit;
}
vector<int> multiplication(vector<int> digit_a, vector<int> digit_b) {
    int NA = digit_a.size(); // A の桁数
    int NB = digit_b.size(); // B の桁数
    vector<int> res(NA + NB - 1);
    for (int i = 0; i < NA; ++i) {
        for (int j = 0; j < NB; ++j) {
            res[i + j] += digit_a[i] * digit_b[j];
            // 答えの i+j の位に digit_a[i] * digit_b[j] を足す
        }
    }
    return carry_and_fix(res);
}
struct triple {
    long long a; long long b; long long c;
};
const long long MOD = (long long)2e18;
const int MAX_C = 61;
long long Com[MAX_C][MAX_C];
void calc_com() {
    memset(Com, 0, sizeof(Com));
    Com[0][0] = 1;
    for (int i = 1; i < MAX_C; ++i) {
        Com[i][0] = 1;
        for (int j = 1; j < MAX_C; ++j) {
            Com[i][j] = (Com[i - 1][j - 1] + Com[i - 1][j]) % MOD;
        }
    }
}
vector<long long> get_path(const vector<long long>& prev, long long t, const vector<long long>& dist) {
    vector<long long> path;
    for (int cur = t; cur != -1; cur = prev[cur]) {
        path.push_back(cur);
    }
    reverse(path.begin(), path.end()); // 逆順なのでひっくり返す
    return path;
}
long long calc = 0;
vector<long long> BFS_num(vector<vector<long long>>& G, long long s, vector<long long>& dist, vector<long long>& st, long long M) {
    vector<long long>D(M, 0);
    queue<long long>Q;
    long long ans = 0;
    dist[s] = 0; D[st[s]] = 1;  Q.push(s);
    while (!Q.empty()) {
        long long v = Q.front(); Q.pop();
        for (auto nv : G[v]) {
            if (dist[nv] == -1) {
                calc++;
                dist[nv] = dist[v] + 1;
                if (dist[nv] + st[s] == st[nv]) D[st[nv]]++;
                Q.push(nv);
            }
        }
    }
    return D;
}
int main() {
    ll N, S, T, K; cin >> N >> S >> T >> K;
    vector<ll> X(N + 1);
    for (int i = 1; i <= N; i++) cin >> X[i];
    ll M; cin >> M;
    vector<vector<P>>E(N + 1);
    ll A, B, Y;
    for (int i = 0; i < M; i++) {
        cin >> A >> B >> Y;
        E[A].push_back({ B, Y });
    }
    vector<vector<ll>> DP(N + 1, vector<ll>(K + 1, INF));
    DP[S][1] = X[S];
    vector<vector<P>>Prev(N + 1, vector<P>(K + 1, { 0, 0 }));
    priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>>Q;
    Q.push(make_pair(0LL, make_pair(S, 1)));

    pair<ll, P> R;
    ll d, x, k, y, m;
    while (!Q.empty()) {
        R = Q.top();
        Q.pop();

        d = R.first;
        x = R.second.first;
        k = R.second.second;

        if (DP[x][k] < d) continue;
        if (x == T && k == K) break;
        long long L = min(K, k + 1);
        for (P a : E[x]) {
            y = a.first, m = a.second;
            if (DP[x][k] + m + X[y] < DP[y][L]) {
                DP[y][L] = DP[x][k] + m + X[y];
                Prev[y][L] = { x, k };
                Q.push({ DP[y][L],{y,L} });
            }
        }
    }
    if (DP[T][K] >= INF) {
        cout << "Impossible" << endl;
        return 0;
    }

    x = T;
    k = K;

    vector<ll>Z;
    while (k > 0) {
        Z.push_back(x);
        P ans = Prev[x][k];
        x = ans.first; k = ans.second;
    }
    cout << "Possible" << endl << DP[T][K] << endl << Z.size() << endl;
    for (long long i = Z.size() - 1; i >= 0; i--) {
        if (i != Z.size() - 1) cout << ' ';
        cout << Z[i];
    }
    cout << endl;
}
0