QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#750312#4662. 二次整数规划问题Max_s_xaM100 ✓69ms5704kbC++178.9kb2024-11-15 14:02:052024-11-15 14:02:05

Judging History

This is the latest submission verdict.

  • [2024-11-15 14:02:05]
  • Judged
  • Verdict: 100
  • Time: 69ms
  • Memory: 5704kb
  • [2024-11-15 14:02:05]
  • Submitted

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>
#include <iomanip>
#include <cassert>

typedef long long ll;
typedef double lf;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 610;
const ll W = 1e6;
const int INF = 1e9;

int K, n, m, q;
ll val[6];

typedef pair <int, int> seg;
#define lb first
#define rb second

seg a[MAXN];
inline void upd(seg &u, const seg &v) { u.lb = max(u.lb, v.lb), u.rb = min(u.rb, v.rb); }
int av[MAXN];

struct Cons
{
    int u, v, w;
}c[MAXN << 2];

int fa[MAXN], sz[MAXN];
int Find(int k) { return k == fa[k] ? k : fa[k] = Find(fa[k]); }
inline void Union(int u, int v)
{
    u = Find(u), v = Find(v);
    if (u == v) return;
    fa[v] = u, upd(a[u], a[v]), sz[u] += sz[v];
}

int st[MAXN], Vcnt[6], id[MAXN];
vector <seg> conv;
int ans[MAXN][7];

const int MAXE = 4010, MAXV = 1210;
int s, t;
struct Edge
{
    int v, nxt, c, f;
}e[MAXE << 1];
int head[MAXV], tot;
inline void AddEdge(int u, int v, int w)
{
    e[++tot] = Edge{v, head[u], w, 0}, head[u] = tot;
    e[++tot] = Edge{u, head[v], 0, 0}, head[v] = tot;
}
int lvl[MAXV], curh[MAXV];
inline bool BFS()
{
    memset(lvl, -1, sizeof(lvl));
    queue <int> q;
    lvl[s] = 0, q.push(s);
    while (!q.empty())
    {
        int u = q.front(); q.pop();
        for (int i = head[u]; ~i; i = e[i].nxt)
        {
            int v = e[i].v;
            if (lvl[v] == -1 && e[i].c > e[i].f)
            {
                lvl[v] = lvl[u] + 1;
                if (v == t) return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
int _DFS(int u, int cap)
{
    if (u == t || !cap) return cap;
    int flow = 0, sum;
    for (int &i = curh[u]; ~i; i = e[i].nxt)
    {
        int v = e[i].v;
        if (lvl[v] == lvl[u] + 1 && e[i].c > e[i].f)
        {
            sum = _DFS(v, min(cap, e[i].c - e[i].f));
            if (!sum) { lvl[v] = -1; continue; }
            flow += sum, cap -= sum;
            e[i].f += sum, e[i ^ 1].f -= sum;
            if (!cap) break;
        }
    }
    return flow;
}
inline int Dinic()
{
    int flow = 0;
    while (BFS())
    {
        for (int i = 1; i <= t; i++) curh[i] = head[i];
        flow += _DFS(s, INF);
    }
    return flow;
}
int vis[MAXN];
void DFS(int u)
{
    vis[u] = 1;
    for (int i = head[u]; ~i; i = e[i].nxt)
    {
        int v = e[i].v;
        if (!vis[v] && e[i].c > e[i].f) DFS(v);
    }
}

int X, Y;
inline void Solve(int x1, int y1, int x2, int y2)
{
    if (x1 == x2 && y1 == y2) return;
    int u = y1 - y2, v = x2 - x1, pos = 0;
    for (int i = 1; i <= n; i++)
        if (a[st[i]].lb == 2) e[pos].c = u * sz[st[i]], pos += 2;
    for (int i = 1; i <= n; i++)
        if (a[st[i]].rb == 4) e[pos].c = v * sz[st[i]], pos += 2;
    for (int i = 0; i <= tot; i++) e[i].f = 0;
    Dinic();
    for (int i = 1; i <= t; i++) vis[i] = 0;
    DFS(s);
    int x = X, y = Y;
    for (int i = 0; i <= tot; i++)
        if (e[i].c == e[i].f && vis[e[i ^ 1].v] && !vis[e[i].v])
        {
            if (e[i ^ 1].v == s) x -= sz[st[e[i].v]];
            if (e[i].v == t) y -= sz[st[e[i ^ 1].v - n]];
        }
    if ((x != x1 || y != y1) && (x != x2 || y != y2))
    {
        conv.emplace_back(x, y);
        if (x1 <= x && y1 >= y) Solve(x1, y1, x, y);
        if (x <= x2 && y >= y2) Solve(x, y, x2, y2);
    }
}

int main()
{
    // freopen("sample_C/ex_qip8.in", "r", stdin);
    // freopen("C.in", "r", stdin);
    // freopen("C.out", "w", stdout);
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    int Case, T;
    Read(Case), Read(T);
    while (T--)
    {
        Read(K), Read(n), Read(m), Read(q);
        for (int i = 1; i <= n; i++) Read(a[i].lb), Read(a[i].rb);
        for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1;
        for (int i = 1; i <= m; i++)
        {
            Read(c[i].u), Read(c[i].v), Read(c[i].w);
            if (c[i].w == 0) Union(c[i].u, c[i].v);
        }
        int top = 0;
        for (int i = 1; i <= n; i++)
            if (fa[i] == i) st[++top] = i;
        n = top;
        for (int i = 1; i <= m; i++) c[i].u = Find(c[i].u), c[i].v = Find(c[i].v);
        memset(Vcnt, 0, sizeof(Vcnt)), memset(val, 0, sizeof(val));
        for (int i = 1; i <= n; i++) av[st[i]] = 0;
        for (int i = 1; i <= n; i++)
            if (a[st[i]].rb != a[st[i]].lb && a[st[i]].rb != 1 && a[st[i]].lb != 5) upd(a[st[i]], seg(2, 4));
        int pre = 0;
        do
        {
            pre = n, top = 0;
            for (int i = 1; i <= n; i++)
                if (a[st[i]].lb == a[st[i]].rb) Vcnt[a[st[i]].lb] += sz[st[i]], av[st[i]] = a[st[i]].lb;
                else st[++top] = st[i];
            n = top;
            for (int i = 1; i <= m; i++)
            {
                if (c[i].w == 0) continue;
                if (av[c[i].u]) upd(a[c[i].v], seg(av[c[i].u] - c[i].w, av[c[i].u] + c[i].w));
                if (av[c[i].v]) upd(a[c[i].u], seg(av[c[i].v] - c[i].w, av[c[i].v] + c[i].w));
            }
        }
        while (n != pre);

        for (int i = 1; i <= n; i++) id[st[i]] = i;
        memset(head, -1, sizeof(head)), tot = -1;
        X = 0, Y = 0;
        s = n * 2 + 1, t = s + 1;
        for (int i = 1; i <= n; i++)
            if (a[st[i]].lb == 2) AddEdge(s, i, 0), X += sz[st[i]];
        for (int i = 1; i <= n; i++)
            if (a[st[i]].rb == 4) AddEdge(i + n, t, 0), Y += sz[st[i]];
        for (int i = 1; i <= m; i++)
            if (!av[c[i].u] && !av[c[i].v] && c[i].w == 1)
                AddEdge(id[c[i].u], id[c[i].v] + n, INF), AddEdge(id[c[i].v], id[c[i].u] + n, INF);
        for (int i = 1; i <= n; i++) AddEdge(i, i + n, INF);
        conv.clear(), conv.emplace_back(X, 0), conv.emplace_back(0, Y);
        Solve(0, Y, X, 0);

        int sum = 0;
        for (int i = 1; i <= n; i++) sum += sz[st[i]];
        for (int i = 0; i < conv.size(); i++)
        {
            for (int j = 1; j <= 5; j++) ans[i][j] = Vcnt[j];
            ans[i][2] += conv[i].lb, ans[i][3] += sum - conv[i].lb - conv[i].rb, ans[i][4] += conv[i].rb;
            ans[i][6] = 0;
            for (int j = 1; j <= 5; j++) ans[i][6] += ans[i][j] * ans[i][j];
            for (int j = 1; j < 5; j++) ans[i][6] += ans[i][j] * ans[i][j + 1] * 2;
        }
        int _ = conv.size();
        for (int i = 1; i <= 5; i++) ans[_][i] = Vcnt[i];
        ans[_][3] += sum, ans[_][6] = 0;
        for (int i = 1; i <= 5; i++) ans[_][6] += ans[_][i] * ans[_][i];
        for (int i = 1; i < 5; i++) ans[_][6] += ans[_][i] * ans[_][i + 1] * 2;

        while (q--)
        {
            for (int i = 2; i < K; i++) Read(val[i]);
            ll mx = 0, cur = 0;
            for (int i = 0; i <= conv.size(); i++)
            {
                cur = ans[i][6] * W;
                for (int j = 2; j <= 4; j++) cur += ans[i][j] * val[j];
                mx = max(mx, cur);
            }
            cout << mx << '\n';
        }
    }
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 4
Accepted
time: 0ms
memory: 3900kb

input:

1 10
3 9 0 115
1 3
1 3
1 1
2 2
2 3
2 2
3 3
1 2
2 3
658203418123
6905620
622765945476
252875716229
2996822
8657162
18477223
830371309382
7361676
19157395
476213860310
145256
465830672653
570192000514
19624112
627396394982
247771396526
14196803
9325384
431820605418
625748891934
8219776
572139911916
25...

output:

4607502926861
127339340
4359440618332
1770209013603
99977754
139600134
208340561
5812678165674
130531732
213101765
3333576022170
80016792
3260893708571
3991423003598
216368784
4391853764874
1734478775682
178377621
144277688
3022823237926
4380321243538
136538432
4005058383412
1791843781758
4801409806...

result:

ok 200 numbers

Test #2:

score: 6
Accepted
time: 20ms
memory: 4712kb

input:

2 600
3 600 596 157061
1 2
1 2
1 1
1 2
2 3
3 3
2 3
1 2
1 1
2 2
1 2
1 3
1 3
3 3
1 3
1 2
1 3
2 2
2 2
1 1
1 1
2 3
1 2
1 2
2 2
1 1
2 3
2 3
2 3
1 2
2 3
1 3
1 2
1 3
1 2
1 2
1 1
1 2
1 3
1 2
1 3
1 3
1 2
1 2
1 1
2 3
1 2
2 3
1 3
1 3
1 3
2 3
1 1
1 3
3 3
2 3
2 2
1 1
1 3
2 3
2 3
1 2
1 1
2 3
2 2
1 3
1 3
2 3
2 2
1...

output:

357297193241
164861912841578
355443266996
436251520919042
359017128828836
168655137304175
30058706198420
82741026078026
336656296298258
188609622449948
392615357377049
351706079357
66989601391688
351251184653
28329778879046
15715555030016
84778669719620
458359958133503
216874786403294
355556711600
2...

result:

ok 300000 numbers

Test #3:

score: 4
Accepted
time: 1ms
memory: 5672kb

input:

3 10
4 8 20 108
1 3
1 3
4 4
2 3
1 3
4 4
3 4
4 4
6 1 2
1 2 3
1 8 3
3 5 3
6 3 3
6 7 3
1 3 3
4 5 1
6 2 3
3 2 3
4 1 3
8 1 1
4 3 2
1 2 0
1 4 1
7 6 3
5 7 1
3 1 1
7 7 3
6 5 3
401193240968 995208538459
353171538452 1968621
456070818347 731730207166
246878450904 1124586
1616052 1433040
874305670186 700986875...

output:

4976106692295
706400982767
3658715035830
493812275566
71165200
3851623966254
1712981554611
4680214558540
3756347800480
68770915
1805949583822
4264753967820
4343656944695
23262510920
2509677872350
1490957084650
1345739922570
4965012949965
4628363528495
312292969856
1377323436800
2485176127355
1321643...

result:

ok 200 numbers

Test #4:

score: 6
Accepted
time: 34ms
memory: 4740kb

input:

4 600
4 600 1294 157227
2 3
2 4
3 4
2 2
1 1
3 3
1 3
1 4
1 4
2 2
3 3
1 2
2 3
3 3
1 4
2 4
1 4
4 4
3 3
2 3
2 2
1 3
2 4
1 3
2 3
1 4
3 4
4 4
3 3
1 2
4 4
3 4
1 3
1 2
2 4
3 3
1 4
2 3
2 3
1 1
3 4
3 3
1 2
1 2
3 3
1 2
2 2
2 4
2 3
3 4
1 3
1 4
1 2
1 4
2 4
2 4
1 3
2 2
1 4
1 3
1 3
2 3
1 3
3 4
1 4
2 3
1 1
1 2
1 3
...

output:

326330874986634
266023872353957
308099862643100
255774576036952
293270434851554
210303324031298
344358231690686
419354488333622
147132960949905
211939860140544
329354998523166
275924958616566
133243396486754
252765431548503
85209484242580
57028056033142
338842309366822
251310971332184
10045781295551...

result:

ok 300000 numbers

Test #5:

score: 5
Accepted
time: 0ms
memory: 3616kb

input:

5 10
5 9 17 242
1 1
2 2
1 4
4 4
1 3
3 5
1 3
1 3
3 5
4 2 2
1 1 3
7 4 3
7 8 2
4 3 1
2 9 3
6 2 1
6 3 2
8 6 1
3 5 2
3 8 1
1 1 1
7 8 4
9 2 3
4 7 1
2 6 3
9 5 1
271928445423 798305139120 796785695833
722910066727 229906468430 810783986581
919517620309 866805930321 859380035316
176315418286 453811728241 628...

output:

5858609975976
4074948900190
7085221617527
3876818744295
4507271032123
2054158710691
4333392103083
2451799633603
4026871106497
3782760832589
4274244788793
6611329128388
3722519724501
3485998917225
6702892039882
3814332936486
6310623579669
5477335448090
6756892536749
4925436707444
4093958151936
354344...

result:

ok 300 numbers

Test #6:

score: 4
Accepted
time: 1ms
memory: 3844kb

input:

6 15
5 15 11 402
2 5
2 5
2 4
2 3
2 4
1 4
4 4
2 4
2 4
2 5
1 1
2 5
1 1
5 5
1 5
1 14 2
15 12 0
12 9 1
1 6 0
12 10 1
1 10 1
2 5 0
9 11 1
6 5 0
4 7 1
12 3 0
910152097519 175243574197 154352789350
426002200739 389601768514 887712385039
303341543782 261172159164 196762016508
618569610914 591630135479 30705...

output:

6491646245449
8198548970068
3322835075020
6976785194597
6997278081397
9759905189156
10668370731824
9543951559191
4962246323512
6896083141018
7191797067417
6097350493430
6768286155956
6455151087183
2758233043168
5198123440630
4155027849858
8208487080454
6199056371326
7004825542541
7001361616614
12397...

result:

ok 500 numbers

Test #7:

score: 4
Accepted
time: 1ms
memory: 3728kb

input:

7 25
5 25 21 595
3 3
2 4
2 5
1 5
1 4
1 4
1 2
2 4
1 1
4 4
2 4
5 5
2 4
1 3
2 5
2 4
2 5
1 4
2 4
1 5
2 4
2 4
2 4
2 5
1 1
15 16 0
18 2 1
13 16 0
5 17 0
18 23 0
13 10 1
17 2 1
16 23 0
19 12 2
6 21 1
14 21 1
8 21 0
24 10 1
5 7 1
19 7 1
11 17 0
3 17 0
20 14 0
14 22 0
4 6 0
6 24 0
474744872990 522331829125 3...

output:

11270997111904
7656988988471
6935814058732
19456706150941
17014520576984
15605454140985
9510393650483
9376065338251
11264421703227
13649797017496
16961418089272
12670941963680
10168627276737
11529237359655
11041511414513
20561858863404
15154993547066
19534066001566
19114579166771
12193233872597
1671...

result:

ok 750 numbers

Test #8:

score: 6
Accepted
time: 1ms
memory: 3732kb

input:

8 50
5 49 50 774
2 5
2 4
1 3
1 5
1 4
2 5
1 4
2 5
2 4
2 4
3 4
4 4
2 5
1 5
2 4
2 5
1 4
2 5
2 4
2 4
2 5
2 4
1 5
2 4
1 5
1 5
1 5
5 5
2 5
3 5
2 4
1 4
2 5
2 4
1 1
2 4
2 4
1 4
2 5
1 4
2 4
1 4
2 4
2 5
1 5
1 5
2 3
1 1
1 4
38 35 2
25 14 0
31 39 1
36 20 0
5 31 1
34 45 0
44 4 1
37 6 0
46 39 1
13 39 1
18 39 1
34...

output:

16411855359820
30862438095555
35358148741245
24243018950274
13437400941943
40856902713918
34811749852591
26463659881497
36613648932316
38079649947694
29634500414283
30333666053770
17980594056385
34202105236092
24022893381858
32108332820386
22025131074467
29959962411915
15909995288809
32556544115411
...

result:

ok 1000 numbers

Test #9:

score: 6
Accepted
time: 1ms
memory: 3680kb

input:

9 80
5 80 86 1152
2 4
2 5
1 4
1 5
2 4
4 5
4 4
1 5
2 4
1 4
2 5
2 5
2 4
2 3
1 4
2 5
1 4
1 5
1 5
1 4
2 4
1 3
2 5
1 5
1 4
1 4
1 4
1 4
2 4
2 5
1 5
2 5
1 5
1 4
1 2
2 4
2 4
1 4
1 4
2 5
1 4
1 4
2 4
1 5
2 5
3 4
2 4
2 4
1 5
2 5
1 5
2 4
2 5
2 4
2 5
2 5
2 5
1 1
1 5
2 5
2 5
5 5
2 4
2 2
3 4
1 4
1 5
2 4
2 4
1 4
1 ...

output:

43173336173321
48379179671216
61732155022844
52475055738777
60424658579039
45788700631742
43656652426991
39954033905650
21565724979888
50071822153180
53569308867112
48967008468227
48513070292990
57479392675689
73226577611663
54978429802800
43966072669984
26054060892577
45070681723753
50418256793182
...

result:

ok 1500 numbers

Test #10:

score: 5
Accepted
time: 2ms
memory: 3768kb

input:

10 120
5 120 130 1532
1 5
1 5
2 5
2 5
2 4
1 5
2 4
2 4
2 3
3 4
2 4
2 4
1 5
2 4
2 4
1 5
2 4
1 5
2 4
2 5
1 5
1 4
1 1
2 4
2 5
1 5
2 5
2 5
1 5
1 5
1 5
3 4
1 4
2 5
2 4
2 4
2 5
1 5
1 4
2 5
2 5
1 5
2 5
2 5
2 5
2 4
3 5
2 4
2 5
3 5
1 4
1 5
2 5
2 5
3 5
2 4
2 4
1 5
2 5
2 4
2 5
1 4
1 4
1 5
1 5
2 5
2 4
1 5
2 5
1 ...

output:

63602867168600
101731196482014
92305249049498
73775533697530
110522467058055
92008233794400
48928399002602
88564264676794
96744974112036
71633177160737
70507065710491
103313034017851
17071162562969
84270703570104
57631578957348
79151795566799
77619165477540
77620444072322
95247655746342
693106356066...

result:

ok 2000 numbers

Test #11:

score: 3
Accepted
time: 0ms
memory: 3988kb

input:

11 200
5 200 0 6331
1 1
1 2
1 3
1 1
3 3
1 2
1 1
4 5
4 4
1 5
2 2
1 2
5 5
5 5
3 4
2 5
2 3
1 4
2 3
2 3
2 3
2 4
1 2
1 5
1 4
1 3
2 3
1 2
3 3
3 4
3 5
2 5
1 4
1 3
2 5
3 5
3 5
1 5
1 2
1 1
2 4
1 2
1 2
2 5
1 2
1 2
2 5
4 4
2 5
1 4
3 4
2 4
2 4
1 4
3 4
2 5
3 4
2 3
2 5
1 4
5 5
2 4
3 4
1 1
4 4
1 4
4 5
3 4
2 4
2 4
...

output:

119342637257251
128388173707220
111084996328359
157252088885495
175756234861472
143899687055552
116303911085186
132692078362401
139773506437735
96146670759328
152118969964169
128730360620264
141157148907793
144718305928451
124191758001235
122728868138622
147230654034856
148509060105740
3759212537773...

result:

ok 8000 numbers

Test #12:

score: 4
Accepted
time: 6ms
memory: 4944kb

input:

12 400
5 399 0 24008
5 5
3 5
4 5
5 5
3 5
4 5
3 4
4 5
4 5
1 3
3 5
2 5
1 4
1 2
1 5
2 5
2 5
3 5
1 3
5 5
3 5
2 4
1 2
3 3
2 4
3 3
1 2
4 4
5 5
1 5
2 5
1 2
3 4
2 5
2 2
1 4
1 1
2 2
1 3
2 4
3 3
1 4
1 1
1 3
3 5
5 5
3 4
1 5
2 4
1 4
3 4
3 5
2 5
1 4
1 4
2 3
2 5
1 5
1 2
1 2
2 3
3 4
2 5
1 5
2 5
2 2
1 3
3 4
4 4
3 5...

output:

273398697481417
275581543494465
112639763389498
208591921347822
274577497610718
171159870741590
220217416399128
197872703369255
118832257760004
315482946084500
319178062757912
326412510236264
261165602990663
121829237782274
255203148475584
148802450822224
268667153706950
244111407530824
263155939708...

result:

ok 30000 numbers

Test #13:

score: 5
Accepted
time: 25ms
memory: 4648kb

input:

13 600
5 599 0 161496
1 5
1 2
2 4
1 1
1 3
1 3
5 5
2 4
2 2
1 5
1 2
3 3
1 3
4 4
1 2
1 5
2 5
5 5
2 3
2 5
4 5
3 3
2 5
4 4
4 4
1 2
3 3
3 4
1 1
3 5
4 5
4 4
2 2
1 5
3 5
1 2
2 2
3 5
1 1
2 5
4 4
4 4
1 1
1 4
3 3
2 5
1 3
3 3
3 5
1 5
1 3
1 2
2 5
3 5
3 4
5 5
1 2
1 5
2 4
3 5
1 4
4 4
2 3
2 4
1 1
1 5
4 4
3 4
1 2
2 ...

output:

335749178644070
495434475852526
342993330210422
392748473892963
299682715380817
392612411623920
459962505190307
180543144504562
299105373318074
422840698814929
212879459848175
365771452476895
256332484121325
521542258132687
436358315944947
109466575109592
70509758413125
208555381020119
2579759521083...

result:

ok 200000 numbers

Test #14:

score: 3
Accepted
time: 2ms
memory: 5704kb

input:

14 200
5 199 10 6319
1 3
1 1
4 5
2 5
1 2
3 3
1 4
1 5
2 5
2 5
2 5
1 4
2 2
3 3
5 5
3 3
2 4
4 5
2 2
3 3
2 5
3 5
4 4
1 5
3 5
1 2
1 4
2 4
1 3
1 3
3 5
3 4
1 5
1 2
1 3
4 4
2 4
1 3
4 5
1 4
2 5
1 3
1 2
2 4
2 3
2 3
2 4
3 5
2 5
1 5
3 5
1 5
1 5
1 3
3 4
2 3
4 5
1 3
4 5
2 2
3 4
2 5
2 2
2 5
2 5
1 3
2 4
4 5
3 4
3 4...

output:

133979833559952
163474467236312
149482468595017
159109645546715
142848831773033
114538412895062
92382303249118
142094731168341
146006313836820
138898377533718
90366203850164
110399618141096
93414151473382
104666227533559
144480204652038
120453498522911
46893485262916
136282254223986
130276085181282
...

result:

ok 8000 numbers

Test #15:

score: 4
Accepted
time: 6ms
memory: 4636kb

input:

15 400
5 399 10 23992
4 5
1 2
3 5
4 5
3 4
2 5
2 4
1 2
2 5
1 1
4 5
2 2
4 5
4 5
1 4
3 5
1 3
2 3
1 4
2 5
2 3
1 2
1 3
2 3
1 2
2 3
1 2
2 5
4 4
2 4
3 5
3 4
3 5
5 5
1 5
3 5
3 4
3 5
3 5
2 5
3 3
1 3
1 1
1 5
1 5
1 1
2 5
1 4
1 4
1 4
3 3
1 3
4 5
1 5
2 5
4 4
3 4
1 5
1 3
2 5
2 4
1 4
2 4
5 5
4 5
3 5
3 3
2 5
1 1
2 ...

output:

281666614191992
254694150419229
254836768267886
179835544743370
201712613846799
193621409653849
288127493447937
218076686570948
270820114482413
257144906290122
231669788416951
291639238399134
307597327780721
229236438296358
289980448246619
273629412285779
271505517570023
297464673341855
167314151511...

result:

ok 30000 numbers

Test #16:

score: 4
Accepted
time: 25ms
memory: 4716kb

input:

16 600
5 600 10 161360
2 4
4 5
3 4
3 3
2 3
1 2
2 3
3 4
1 2
3 4
1 3
1 3
1 1
2 2
2 5
2 4
4 4
3 5
2 5
3 4
1 4
3 3
1 5
2 4
2 3
4 5
1 1
4 5
4 5
2 5
2 3
2 5
1 4
3 3
2 4
1 1
3 4
1 3
3 5
3 4
3 5
4 5
2 2
1 3
2 3
1 5
2 2
1 3
2 5
4 5
2 4
2 2
4 5
1 2
3 4
4 5
1 2
2 4
3 5
2 2
2 5
2 3
1 5
3 4
1 2
2 5
4 4
4 4
1 2
2...

output:

181278926200419
386453914757203
386319439875542
493317511242192
487083543495960
402817134226612
83961490405313
277137634721699
226964737963217
344548710346617
349989846610085
528618991799189
460710926276896
378705613479919
432484524569292
470691495927259
235451741825977
405583690014834
3336355728953...

result:

ok 200000 numbers

Test #17:

score: 4
Accepted
time: 8ms
memory: 4692kb

input:

17 120
5 119 355 81330
3 5
3 5
3 5
1 5
1 3
3 3
1 2
2 5
2 2
3 4
1 5
3 5
4 5
3 3
4 5
3 4
3 5
2 5
1 4
2 3
4 4
2 5
1 3
1 4
1 5
4 5
5 5
3 4
2 5
2 4
4 5
1 3
3 4
2 5
4 5
1 4
1 2
3 3
1 2
1 5
1 1
2 2
4 4
2 3
3 3
3 4
5 5
1 2
1 2
3 5
3 5
4 5
1 5
2 5
1 4
2 4
2 2
3 3
3 5
1 4
1 1
3 3
3 5
1 4
5 5
3 5
5 5
4 5
2 4
4...

output:

66198382514252
59622159077969
60827005109865
46118195591083
66771374223710
48831804665381
79608131752009
31968284755062
94127991928758
45944596201478
32119458879636
70443862946816
74193874490723
70832357442367
61755956068141
57232920654146
18449977428589
66922135462574
59076425039328
74141179956533
...

result:

ok 100000 numbers

Test #18:

score: 5
Accepted
time: 16ms
memory: 4628kb

input:

18 150
5 149 393 162466
1 3
5 5
1 2
2 5
2 5
3 5
4 5
1 3
2 4
2 3
3 4
2 3
2 3
2 4
1 2
5 5
2 4
2 4
2 5
4 5
3 5
2 4
4 5
1 2
4 5
2 3
1 4
3 5
1 3
1 2
3 4
1 5
2 4
3 3
2 4
1 5
2 4
1 5
2 3
5 5
1 4
2 4
2 3
1 5
1 1
4 5
3 3
3 3
1 3
4 5
4 5
2 5
4 4
2 2
1 4
3 4
2 2
1 5
1 3
1 5
2 3
1 3
2 5
3 5
3 4
2 3
2 4
4 5
3 5
...

output:

90462993219411
92196171930641
105558329776600
109829303449099
78209529286435
105944215391721
93991905015011
88209421536733
86612481454766
88209979384243
81754437866348
54795015659628
71273911152566
62949966302086
54902672601916
76583096660954
89991650399327
98772141539034
70371810015192
276635801659...

result:

ok 200000 numbers

Test #19:

score: 5
Accepted
time: 29ms
memory: 4888kb

input:

19 180
5 180 289 243700
2 3
1 5
2 3
1 4
4 5
2 4
1 1
1 5
2 5
3 5
4 5
1 2
2 4
4 5
1 5
2 3
2 4
2 4
1 2
2 4
3 3
3 5
2 5
2 2
2 3
1 2
5 5
2 5
1 3
2 5
3 5
4 5
1 5
3 4
3 5
1 3
3 4
1 4
2 5
2 3
3 5
3 4
2 5
1 4
2 4
3 3
1 2
5 5
3 5
3 5
4 5
1 2
1 3
1 4
2 2
2 2
1 4
2 4
1 4
2 4
1 1
2 2
1 3
1 4
2 2
3 4
2 4
1 5
1 3
...

output:

82357639672559
109278227945149
47909684545677
106819187228408
125097491886803
96807857495463
60437316712917
62518834560561
127161460194292
73561810777818
85468039484735
56463515385768
90838174309695
126999937276284
91926606344616
81621959364724
71164295011060
107726310839415
62755972690698
135795857...

result:

ok 300000 numbers

Test #20:

score: 5
Accepted
time: 10ms
memory: 4596kb

input:

20 300
5 300 340 40300
1 4
2 4
1 5
2 5
1 4
2 4
2 5
1 4
1 4
1 4
2 4
1 4
2 4
2 5
4 4
1 4
2 4
2 4
2 4
2 5
1 4
2 4
1 4
2 5
2 4
2 5
2 4
2 4
1 5
2 4
1 5
2 5
1 5
1 5
2 4
2 4
2 5
1 5
1 5
1 4
1 4
1 4
1 4
1 5
1 5
2 4
2 4
2 5
2 4
2 5
1 5
2 4
1 4
1 4
1 4
2 4
1 4
3 5
2 3
1 4
2 4
1 5
1 4
2 5
2 4
1 5
1 4
2 5
2 5
2...

output:

218130940674637
140726103406404
222657546947880
104930864471334
175682167130420
261194346998352
210878096255632
287022367774339
183080954101912
224846557729055
217076909443111
238241480685678
253853170527494
185754102697537
250938400107558
232108311171164
219049442495196
205601212531212
182076296082...

result:

ok 50000 numbers

Test #21:

score: 4
Accepted
time: 25ms
memory: 4712kb

input:

21 450
5 450 498 80658
2 5
2 4
2 5
2 4
1 5
2 5
1 5
2 4
2 4
4 4
1 5
1 5
2 4
2 5
1 4
2 4
1 5
2 4
1 4
1 5
1 4
2 5
2 4
1 4
2 4
1 4
2 4
2 4
1 5
2 5
1 4
1 4
2 4
2 5
1 5
1 4
2 4
1 4
2 5
2 5
2 5
2 5
2 4
1 1
2 5
1 4
2 5
1 5
2 5
1 5
2 5
2 4
2 5
1 4
2 5
1 3
1 4
2 4
2 5
2 5
2 4
2 4
1 4
2 4
1 5
1 4
1 4
1 3
1 5
2...

output:

348537382085321
343294985626014
373983520542463
340960976831875
398780686844023
424638057048250
208785160972457
98663085451259
343459816079694
305202335549029
332141994036632
379397559681111
49084384728407
360703253246668
187128021523960
350269908280291
406350955644555
305184300973585
27218854103732...

result:

ok 100000 numbers

Test #22:

score: 4
Accepted
time: 69ms
memory: 4756kb

input:

22 600
5 600 690 242382
1 4
2 4
1 4
1 4
1 5
1 4
1 5
2 5
1 5
2 5
1 5
1 4
1 4
1 5
1 5
2 4
1 4
1 5
2 5
2 4
1 4
2 3
2 5
1 4
2 4
2 4
2 5
2 4
1 5
2 5
2 4
2 4
1 4
1 5
2 5
2 5
3 4
3 5
2 4
2 5
1 3
2 4
1 5
1 5
1 5
2 5
2 5
2 5
1 5
1 4
1 5
1 5
3 5
2 4
1 5
1 4
1 4
2 5
1 4
2 4
1 4
1 4
1 5
2 4
1 5
2 5
1 5
1 4
1 4
...

output:

464440385956615
411871996194468
492245570890866
290012140920905
407044073875942
413729220334542
480514246642687
369451460117159
275338979512217
525165307508844
374042621417122
244184176142816
343846931342235
562186934927726
573816960849201
494203884165926
495537198495828
548944831050759
346630760325...

result:

ok 300000 numbers

Extra Test:

score: 0
Extra Test Passed