QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#451392#8759. 小班课kilo_tobo_tarjenAC ✓848ms46624kbC++205.0kb2024-06-23 10:34:502024-06-23 10:34:51

Judging History

你现在查看的是最新测评结果

  • [2024-06-23 10:34:51]
  • 评测
  • 测评结果:AC
  • 用时:848ms
  • 内存:46624kb
  • [2024-06-23 10:34:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const char el = '\n';
typedef long long ll;
namespace costflow {

typedef long long flow_t;
typedef long long cost_t;

const flow_t inf_flow = 1e18;
const cost_t inf_cost = 1e18;

struct CostEdge {
  int from, to;
  cost_t cost;
  flow_t cap, low = 0, flow = 0;
};

int num_node(const std::vector<CostEdge> &edges) {
  int n = 0;
  for (const auto &e : edges) n = std::max({n, e.from, e.to});
  return n;
}
std::pair<flow_t, cost_t> get_flow(const std::vector<CostEdge> &edges, int s) {
  flow_t flow = 0;
  cost_t cost = 0;
  for (const auto &e : edges) {
    if (e.from == s) flow += e.flow;
    cost += e.flow * e.cost;
  }
  return {flow, cost};
}

struct CostFlow {
  struct Edge {
    int from, to;
    cost_t cost;
    flow_t cap;
  };
  int n;
  std::vector<std::vector<int>> eid;
  std::vector<Edge> edge;
  void build(const std::vector<CostEdge> &edges) {
    n = num_node(edges);
    eid.assign(n + 1, {});
    edge.clear();
    int num_edges = 0;
    for (const auto &e : edges) {
      eid[e.from].push_back(num_edges++);
      edge.push_back({e.from, e.to, e.cost, e.cap - e.flow});
      eid[e.to].push_back(num_edges++);
      edge.push_back({e.to, e.from, -e.cost, e.flow});
    }
  }
  std::vector<cost_t> dis;
  std::vector<int> pre;
  bool spfa(int s, int t) {
    if (s > n || t > n) return false;
    dis.assign(n + 1, inf_cost);
    pre.assign(n + 1, 0);
    std::vector<bool> inque(n + 1);
    std::queue<int> que;
    dis[s] = 0;
    que.push(s);
    inque[s] = true;
    while (que.size()) {
      int u = que.front();
      // cerr << 'u' << ' ' << u << endl;
      que.pop();
      inque[u] = false;
      for (auto i : eid[u]) {
        const auto &e = edge[i];
        if (e.cap && dis[e.to] > dis[u] + e.cost) {
          dis[e.to] = dis[u] + e.cost;
          pre[e.to] = i;
          if (!inque[e.to]) {
            que.push(e.to);
            inque[e.to] = true;
          }
        }
      }
    }
    return dis[t] < inf_cost;
  }
  std::pair<flow_t, cost_t> maxflow(int s, int t) {
    flow_t flow = 0;
    cost_t cost = 0;
    while (spfa(s, t)) {
      flow_t detf = inf_flow;
      cost_t detc = 0;
      for (int u = t, i = pre[u]; u != s; u = edge[i].from, i = pre[u]) {
        detf = std::min(detf, edge[i].cap);
        detc += edge[i].cost;
      }
      for (int u = t, i = pre[u]; u != s; u = edge[i].from, i = pre[u]) {
        edge[i].cap -= detf;
        edge[i ^ 1].cap += detf;
      }
      flow += detf;
      cost += detf * detc;
    }
    return {flow, cost};
  }
  std::vector<CostEdge> to_edge() {
    std::vector<CostEdge> edges;
    for (int i = 0; i < edge.size(); i += 2)
      edges.push_back({
          .from = edge[i].from,
          .to = edge[i].to,
          .cost = edge[i].cost,
          .cap = edge[i].cap + edge[i ^ 1].cap,
          .flow = edge[i ^ 1].cap,
      });
    return edges;
  }
};

}  // namespace costflow
using namespace costflow;

struct graph {
  vector<vector<int>> e;
  graph(int n) : e(n + 1) {}
  void adde(int u, int v) { e[u].push_back(v); }
  optional<vector<int>> toposort() {
    int n = e.size() - 1;
    vector<int> d(n + 1);
    queue<int> que;
    for (int u = 1; u <= n; u++)
      for (auto v : e[u]) d[v]++;
    for (int i = 1; i <= n; i++)
      if (!d[i]) que.push(i);
    vector<int> res;
    while (!que.empty()) {
      auto u = que.front();
      res.push_back(u);
      que.pop();
      for (auto v : e[u]) {
        if (!--d[v]) que.push(v);
      }
    }
    if (res.size() != n) return nullopt;
    return res;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(15);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, m;
    cin >> n >> m;
    vector<CostEdge> edge;
    auto id1 = [&](int i) { return i; };
    auto id2 = [&](int i) { return i + n; };
    int s = n + m + 1, t = n + m + 2;
    for (int i = 1; i <= m; i++) {
      int b;
      cin >> b;
      edge.push_back({.from = id2(i), .to = t, .cost = 0, .cap = b});
    }
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      vector<int> a(k);
      for (auto &v : a) cin >> v;
      edge.push_back({.from = s, .to = id1(i), .cost = 0, .cap = 1});
      for (int j = 0; j < a.size(); j++)
        edge.push_back({.from = id1(i), .to = id2(a[j]), .cost = j, .cap = 1});
    }
    CostFlow cf;
    cf.build(edge);
    auto [f, c] = cf.maxflow(s, t);
    edge = cf.to_edge();
    cout << f << el;
    graph g(n + m);
    vector<int> cost(n + 1);
    for (auto e : edge)
      if (e.from <= n && e.flow) g.adde(e.to, e.from), cost[e.from] = e.cost;
    for (auto e : edge)
      if (e.from <= n && !e.flow && e.cost < cost[e.from]) g.adde(e.from, e.to);
    auto res = *g.toposort();
    reverse(res.begin(), res.end());
    for (auto v : res)
      if (v <= n) cout << v << ' ';
    cout << el;
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

3
5 5
1 1 1 1 1
4 1 3 2 4
1 5
4 3 4 2 1
2 3 5
1 1
5 3
1 2 2
2 1 2
2 1 2
2 1 3
2 1 3
2 1 3
5 5
1 1 1 1 1
2 1 2
2 5 4
2 3 2
2 4 3
2 5 1

output:

5
4 5 2 3 1 
5
5 4 3 2 1 
5
2 5 4 3 1 

result:

ok Correct!

Test #2:

score: 0
Accepted
time: 1ms
memory: 3856kb

input:

250
2 1
2
1 1
1 1
1 1
1
0
2 2
1 1
1 1
2 2 1
2 2
0 2
2 1 2
1 2
1 1
1
1 1
1 2
1 0
0
1 2
1 0
0
2 1
2
1 1
0
1 2
1 0
0
2 1
2
1 1
1 1
1 1
1
1 1
1 2
1 0
1 2
2 2
2 0
1 1
1 2
1 1
1
0
1 1
1
0
1 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
0
5 5
1 2 1 1 0
1 ...

output:

3
3 2 4 5 1 
1
1 5 4 3 2 
2
1 5 4 3 2 
3
5 3 1 4 2 
2
1 3 5 4 2 
4
5 4 2 1 3 
3
4 3 1 5 2 
2
2 3 5 4 1 
1
1 5 4 3 2 
4
1 2 4 3 5 
2
3 2 5 4 1 
2
1 4 5 3 2 
3
3 2 5 4 1 
3
4 3 1 5 2 
3
4 1 2 5 3 
3
3 1 2 5 4 
2
1 3 5 4 2 
3
5 4 1 3 2 
1
1 5 4 3 2 
3
3 5 2 4 1 
1
2 5 4 3 1 
2
3 2 5 4 1 
2
1 4 5 3 2 
2...

result:

ok Correct!

Test #6:

score: 0
Accepted
time: 2ms
memory: 3688kb

input:

10
45 47
3 0 2 0 1 1 1 0 2 0 1 0 0 3 0 0 0 4 0 1 0 0 1 2 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 2 4 1 2 1 2 3
7 1 37 21 3 13 43 22
0
10 23 46 22 40 12 19 47 27 16 42
4 29 19 45 35
10 6 26 2 43 41 7 9 16 42 44
5 39 40 34 46 14
3 34 3 38
8 10 5 38 23 19 37 9 34
0
5 31 29 15 13 35
3 40 4 28
1 7
6 29 12 9 35 2...

output:

33
37 10 39 4 31 30 11 16 8 18 15 29 19 43 44 35 25 6 3 34 40 5 14 36 42 38 24 17 13 12 7 21 1 45 41 33 32 28 27 26 23 22 20 9 2 
39
40 30 12 21 38 31 28 15 25 26 43 3 32 29 11 5 20 44 14 19 24 6 42 35 9 34 17 10 36 18 16 39 33 2 45 23 41 7 1 37 27 22 13 8 4 
36
27 29 5 33 20 31 6 21 23 47 3 43 15 3...

result:

ok Correct!

Test #7:

score: 0
Accepted
time: 47ms
memory: 5672kb

input:

1
499 497
1 2 0 2 0 1 0 0 0 2 1 2 0 3 1 2 0 0 0 1 0 1 0 2 1 0 1 0 1 1 1 2 0 1 0 1 0 2 2 3 1 1 2 1 0 0 1 0 2 3 0 1 0 0 2 0 1 2 1 0 0 1 2 0 0 2 0 2 0 1 0 1 0 0 1 0 0 1 1 1 1 1 0 0 0 1 2 3 0 0 0 4 2 2 1 2 2 0 1 0 1 0 2 0 1 0 2 0 0 1 1 1 3 2 0 2 2 2 0 1 1 1 1 1 0 1 0 1 1 1 1 1 2 0 0 1 0 2 1 2 1 2 1 0 1 ...

output:

482
283 367 237 353 391 376 153 27 222 75 342 329 69 184 161 199 321 314 410 296 433 272 426 8 346 369 268 79 401 201 378 19 429 106 404 198 351 472 446 358 101 213 291 162 397 489 478 134 470 302 459 387 253 402 398 315 499 262 86 243 326 46 170 460 67 80 457 380 50 447 242 39 348 60 413 189 155 12...

result:

ok Correct!

Test #8:

score: 0
Accepted
time: 848ms
memory: 46624kb

input:

1
498 499
0 1 1 0 1 0 1 0 0 0 0 2 0 3 1 2 4 0 1 0 1 1 0 0 0 1 1 0 0 2 2 0 1 1 1 0 4 1 1 2 1 0 0 1 2 0 1 2 1 0 1 2 0 2 1 2 2 0 2 2 0 1 0 2 0 0 3 0 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 2 1 1 0 1 0 1 0 0 0 1 1 2 0 1 0 2 1 1 2 2 0 0 0 0 2 0 2 1 0 1 0 2 0 1 3 1 1 1 0 1 3 0 1 0 1 0 0 1 3 2 3 2 1 1 0 2 ...

output:

498
60 251 319 337 479 127 219 210 391 279 165 55 141 125 83 139 449 273 189 15 134 5 180 302 47 188 450 255 356 137 281 156 296 204 261 244 405 161 230 396 233 31 278 254 285 98 290 349 340 103 3 46 166 108 58 367 40 347 185 26 437 370 225 399 105 78 44 104 80 402 416 16 236 207 474 56 215 384 268 ...

result:

ok Correct!

Test #9:

score: 0
Accepted
time: 1ms
memory: 3752kb

input:

5
99 96
2 0 0 1 1 2 1 0 1 1 1 0 0 0 1 0 1 1 2 1 1 1 1 1 0 1 2 4 0 0 0 2 2 1 1 1 1 1 0 2 0 0 0 1 1 3 0 1 0 0 1 2 1 4 1 2 1 0 1 0 0 2 0 0 0 2 3 2 1 0 1 2 2 0 1 1 0 0 1 0 0 1 2 1 3 1 3 1 3 0 3 0 0 2 2 2
2 14 58
1 55
2 53 69
0
0
1 76
2 23 38
1 41
2 74 54
0
0
2 83 91
0
0
0
1 48
0
0
1 96
2 76 52
1 17
2 51...

output:

48
47 34 6 84 88 3 39 19 56 73 82 71 25 60 12 52 62 35 83 26 53 23 2 65 40 38 9 20 22 16 24 59 72 87 67 42 78 57 30 7 99 21 45 69 92 74 46 44 98 97 96 95 94 93 91 90 89 86 85 81 80 79 77 76 75 70 68 66 64 63 61 58 55 54 51 50 49 48 43 41 37 36 33 32 31 29 28 27 18 17 15 14 13 11 10 8 5 4 1 
44
60 33...

result:

ok Correct!

Test #10:

score: 0
Accepted
time: 2ms
memory: 3724kb

input:

5
99 97
0 2 4 0 0 2 0 1 1 1 0 1 0 3 0 1 1 1 1 0 0 1 0 0 1 2 0 0 1 3 1 2 0 2 1 1 1 3 3 1 2 1 0 1 0 1 0 2 0 0 0 0 1 2 3 1 1 1 0 1 0 1 0 0 1 2 1 2 1 1 1 2 2 3 1 1 0 0 1 1 0 0 1 1 2 1 2 2 0 1 1 1 2 0 1 3 1
2 56 63
2 52 45
4 26 56 80 10
2 27 19
1 81
2 38 64
1 83
1 8
3 14 81 60
3 63 28 15
5 59 33 80 88 56...

output:

72
25 44 62 81 16 23 64 48 34 83 39 47 11 36 55 26 85 66 65 50 63 18 94 53 7 35 77 95 32 30 90 28 54 78 1 91 82 27 45 33 71 76 96 57 40 17 68 59 14 52 37 6 46 29 72 99 24 22 73 58 3 79 20 98 12 9 41 49 8 60 42 13 97 93 92 89 88 87 86 84 80 75 74 70 69 67 61 56 51 43 38 31 21 19 15 10 5 4 2 
67
56 60...

result:

ok Correct!

Test #11:

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

input:

5
99 98
4 0 1 1 3 2 0 1 4 0 1 1 2 2 1 2 0 0 1 2 1 2 0 1 1 1 2 0 2 0 0 3 0 2 0 0 1 1 1 0 1 1 1 2 0 1 1 0 1 1 1 0 0 1 0 0 2 1 2 3 3 0 0 0 0 0 1 2 1 1 0 3 0 0 0 1 2 0 0 0 0 1 0 2 2 1 2 1 0 1 0 0 1 1 2 3 3 0
5 72 78 90 7 60
6 69 37 10 41 4 59
10 61 85 79 5 7 58 3 55 1 50
6 59 24 30 26 77 21
2 29 21
10 7...

output:

85
83 12 53 55 85 66 98 39 76 54 46 65 97 63 43 16 10 25 26 79 29 58 47 89 32 1 94 24 20 71 82 50 23 74 75 30 6 59 67 44 33 31 57 37 27 96 49 84 70 87 15 3 91 78 86 4 64 34 21 73 40 69 2 90 60 5 19 68 41 38 99 35 28 51 72 93 81 42 11 77 80 62 14 95 8 92 88 61 56 52 48 45 36 22 18 17 13 9 7 
87
42 40...

result:

ok Correct!

Test #12:

score: 0
Accepted
time: 9ms
memory: 4008kb

input:

5
97 100
1 1 1 0 0 1 0 1 1 2 0 1 2 0 1 0 2 3 0 1 0 1 0 1 0 0 1 0 1 2 0 3 2 2 1 0 1 1 2 3 3 1 0 2 1 1 1 2 2 2 0 2 0 3 1 2 2 2 0 1 0 1 1 0 2 0 0 0 0 3 1 0 0 1 0 1 1 0 0 1 1 2 1 2 0 0 1 2 0 1 1 0 2 0 0 1 0 0 2 2
48 80 1 66 89 71 73 40 2 50 99 68 91 31 76 25 67 94 37 6 88 86 28 22 43 62 21 16 17 39 70 1...

output:

94
39 9 15 42 12 37 50 89 20 55 40 58 91 47 66 63 24 88 49 48 53 81 96 79 64 69 41 2 16 8 90 60 54 78 28 93 62 45 30 80 35 51 36 26 94 65 17 43 33 83 19 32 5 22 14 21 73 71 74 44 77 23 7 86 11 85 56 92 59 29 70 38 3 13 10 1 61 57 27 34 31 25 97 82 76 52 6 4 84 75 67 87 72 18 95 68 46 
94
91 35 86 88...

result:

ok Correct!

Test #13:

score: 0
Accepted
time: 1ms
memory: 3904kb

input:

5
96 96
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 72 44
0
0
1 75
0
2 48 35
1 29
1 55
2 75 32
1 95
0
2 87 85
1 69
1 6
2 48 6
1 77
1 67
0
0
1 75
2 59 ...

output:

59
59 48 17 78 12 15 4 54 24 10 62 65 27 75 80 36 79 73 60 90 69 16 91 46 1 88 13 55 32 22 49 21 56 23 8 61 41 25 51 39 87 89 81 68 85 6 9 34 74 7 70 67 86 29 47 45 31 14 44 96 95 94 93 92 84 83 82 77 76 72 71 66 64 63 58 57 53 52 50 43 42 40 38 37 35 33 30 28 26 20 19 18 11 5 3 2 
54
40 31 8 76 96 ...

result:

ok Correct!

Test #14:

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

input:

1
499 497
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

287
34 167 425 405 299 87 186 378 356 26 411 10 343 214 190 430 246 7 51 453 280 60 369 407 132 314 156 134 234 44 235 261 61 56 342 227 183 97 402 122 101 451 457 318 361 102 401 237 32 93 53 317 323 96 268 81 307 260 349 483 31 210 47 99 397 43 29 305 171 57 347 24 250 46 339 220 125 488 435 279 2...

result:

ok Correct!

Test #15:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

10
50 49
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 43
0
0
2 25 28
0
0
2 28 14
0
2 8 38
1 1
0
1 6
1 41
0
2 40 47
2 8 34
2 26 41
2 31 28
1 5
0
2 33 45
0
2 14 43
0
2 39 34
1 32
1 6
1 15
1 17
0
0
2 9 6
2 37 1
0
2 13 20
0
2 4 41
2 31 5
0
2 44 14
1...

output:

30
29 49 9 41 47 46 21 40 1 13 15 25 33 16 26 18 50 7 17 4 28 23 35 48 32 12 19 37 44 10 45 43 42 39 38 36 34 31 30 27 24 22 20 14 11 8 6 5 3 2 
27
24 14 20 29 49 34 38 5 19 45 40 16 21 9 41 17 27 33 25 10 12 44 43 6 3 50 35 48 47 46 42 39 37 36 32 31 30 28 26 23 22 18 15 13 11 8 7 4 2 1 
25
28 15 4...

result:

ok Correct!

Test #16:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

10
50 49
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 32
0
0
2 47 41
1 10
3 33 18 13
1 43
1 49
1 39
0
0
3 16 27 31
0
0
2 30 25
3 23 16 37
3 18 1 46
2 8 9
3 32 33 10
3 24 14 31
3 31 11 2
2 8 36
1 44
1 9
0
2 39 27
1 14
1 6
3 47 12 10
1 14
1 10
0
1...

output:

32
9 1 26 18 24 47 19 4 42 7 8 23 46 44 22 48 15 34 35 33 20 16 12 27 6 29 21 5 28 49 45 17 50 43 41 40 39 38 37 36 32 31 30 25 14 13 11 10 3 2 
31
18 36 26 38 21 2 23 11 34 25 46 15 42 6 28 49 14 43 41 5 3 4 39 27 48 16 32 17 29 31 1 50 47 45 44 40 37 35 33 30 24 22 20 19 13 12 10 9 8 7 
35
7 1 16 ...

result:

ok Correct!

Test #17:

score: 0
Accepted
time: 6ms
memory: 4184kb

input:

1
495 495
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

344
494 291 108 414 368 145 484 392 250 40 253 375 376 468 241 469 461 379 49 191 351 247 30 43 205 258 121 458 353 107 426 495 144 340 68 230 349 18 12 211 362 92 32 318 359 422 252 409 404 212 415 91 439 272 493 264 167 460 209 261 436 60 313 133 38 347 80 150 10 287 462 179 327 218 164 97 373 134...

result:

ok Correct!

Test #18:

score: 0
Accepted
time: 1ms
memory: 3536kb

input:

250
1 2
1 0
2 1 2
2 2
0 2
1 1
1 1
2 2
1 1
2 1 2
2 1 2
1 1
1
0
2 2
0 2
2 1 2
0
2 1
2
1 1
0
2 2
0 2
1 1
2 1 2
2 1
2
0
0
1 2
0 1
2 1 2
1 1
1
1 1
2 1
2
0
1 1
2 2
1 1
2 1 2
1 1
1 1
1
1 1
2 1
2
0
1 1
1 2
0 1
2 1 2
1 2
0 1
0
1 2
0 1
1 1
2 2
1 1
1 1
2 1 2
2 1
2
1 1
1 1
1 2
1 0
2 1 2
1 2
0 1
2 1 2
1 2
1 0
2 ...

output:

1
1 
0
2 1 
2
1 2 
0
1 
1
1 2 
1
1 2 
1
2 1 
0
2 1 
1
1 
1
1 
1
2 1 
2
2 1 
1
1 
1
2 1 
1
1 
0
1 
0
1 
2
1 2 
2
2 1 
1
1 
1
1 
1
1 
2
2 1 
0
2 1 
2
2 1 
1
1 
1
1 2 
0
1 
1
1 
1
1 
1
1 2 
2
2 1 
1
2 1 
1
1 
1
2 1 
0
1 
1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:

score: 0
Accepted
time: 2ms
memory: 3652kb

input:

10
49 49
0 0 0 0 0 1 0 1 0 0 1 0 0 1 1 0 0 1 2 0 2 2 1 1 0 0 2 3 1 2 1 1 1 2 2 0 1 0 1 0 2 2 0 0 4 1 3 3 3
5 3 10 12 16 18
5 2 16 36 21 39
9 19 23 5 31 32 12 15 34 40
0
5 5 19 21 24 30
3 6 8 16
6 15 17 26 2 14 7
1 3
2 14 15
3 16 21 23
6 3 4 17 19 21 22
1 7
7 2 11 28 10 13 31 6
0
6 13 33 43 47 14 1
4...

output:

29
9 6 34 41 16 28 11 15 45 10 38 42 39 49 7 26 18 22 2 36 46 33 3 32 5 20 30 13 17 48 47 44 43 40 37 35 31 29 27 25 24 23 21 19 14 12 8 4 1 
22
27 17 21 5 47 41 23 29 42 28 22 38 9 48 7 32 46 2 25 45 36 12 49 44 43 40 39 37 35 34 33 31 30 26 24 20 19 18 16 15 14 13 11 10 8 6 4 3 1 
28
13 43 4 28 48...

result:

ok Correct!

Test #23:

score: 0
Accepted
time: 39ms
memory: 5592kb

input:

1
498 500
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 2 1 0 1 0 0 2 0 1 0 0 1 1 0 0 0 0 0 2 0 0 1 0 1 0 0 0 0 0 2 1 2 0 1 0 0 1 1 0 1 ...

output:

464
175 172 74 290 145 17 81 12 138 374 246 94 100 462 187 306 135 208 366 49 229 400 497 330 10 43 396 262 281 476 218 162 79 165 52 9 371 61 463 446 323 18 340 251 398 198 454 276 93 381 326 319 226 21 22 238 485 231 146 372 496 5 344 296 78 109 291 184 337 383 7 464 107 312 260 106 458 13 130 6 1...

result:

ok Correct!

Test #24:

score: 0
Accepted
time: 433ms
memory: 24812kb

input:

1
497 496
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 1 0 1 0 1 0 2 1 0 0 0 0 0 0 0 1 1 0 1 0 1 2 1 2 0 1 0 0 1 2 0 1 1 1 0 0 0 0 2 1 1 1 0 0 1 ...

output:

496
222 346 477 112 433 284 448 241 143 352 463 43 276 68 39 133 450 412 452 97 269 393 496 485 495 179 476 442 5 277 344 191 427 487 225 449 270 189 455 280 482 403 250 194 129 299 19 6 10 163 400 293 253 142 260 186 58 467 486 339 72 211 57 316 289 64 141 417 334 4 324 309 492 67 252 217 261 204 1...

result:

ok Correct!

Test #25:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

5
100 100
0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 1 2 0 1 0 0 1 1 1 1 0 2 1 0 0 1 2 1 0 1 1 1 1 0 1 1 2 5 1 0 1 2 0 2 0 3 1 1 1 2 3 2 1 1 1 2 4 2 1 4 1 2 1 0 3 3 1 3 0 0 3 1 0 0 0 3 4 2
1 35
0
1 8
1 35
0
0
1 10
2 15 19
2 1 3
2 6 61
0
0
0
0
2 14 20
2 4 80
0
0
0
1 6
0
1 13
1 4
...

output:

19
47 16 37 98 10 73 48 59 39 57 43 92 54 27 79 66 22 7 3 100 99 97 96 95 94 93 91 90 89 88 87 86 85 84 83 82 81 80 78 77 76 75 74 72 71 70 69 68 67 65 64 63 62 61 60 58 56 55 53 52 51 50 49 46 45 44 42 41 40 38 36 35 34 33 32 31 30 29 28 26 25 24 23 21 20 19 18 17 15 14 13 12 11 9 8 6 5 4 2 1 
26
8...

result:

ok Correct!

Test #26:

score: 0
Accepted
time: 0ms
memory: 3920kb

input:

5
95 99
0 0 1 0 0 0 0 0 1 1 0 0 0 2 1 0 0 0 1 0 1 0 0 0 1 2 0 0 1 1 2 0 0 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 1 2 2 0 0 0 1 0 1 1 1 4 3 0 3 1 2 1 0 1 0 1 5 0 1 1 0 3 0 3 1 2 2 4 0 0 0 1 1 1 2 3 1 0 2 4 0 1 1 4 3
0
1 5
3 31 50 70
1 21
5 14 26 31 2 6
5 7 41 19 45 30
3 1 6 40
0
2 16 19
4 5 6 9 25
0
0
5 17 47...

output:

41
71 17 61 21 18 92 5 85 42 44 70 35 63 51 16 59 43 79 62 45 69 84 28 73 13 57 82 65 30 47 36 3 83 41 49 89 75 87 15 4 19 95 94 93 91 90 88 86 81 80 78 77 76 74 72 68 67 66 64 60 58 56 55 54 53 52 50 48 46 40 39 38 37 34 33 32 31 29 27 26 25 24 23 22 20 14 12 11 10 9 8 7 6 2 1 
37
6 19 70 65 96 45 ...

result:

ok Correct!

Test #27:

score: 0
Accepted
time: 2ms
memory: 3860kb

input:

5
97 97
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 1 0 0 1 1 2 0 0 1 2 1 0 0 1 2 2 1 0 2 0 1 0 0 3 0 2 3 0 2 1 1 1 1 0 1 1 2 4 1 0 2 1 0 1 2 1 2 2 2 1 2 5 2 0 1 0 3 1 3 1 0 1 2 0 0 2 4 2 2 1 2 1 3 0
0
5 14 16 33 41 9
7 12 28 16 32 45 46 71
10 15 63 65 17 66 18 81 45 8 68
6 8 35 12 54 21 63
0
0
0
...

output:

67
18 52 86 48 94 40 59 43 75 14 67 73 49 25 72 38 57 69 93 77 47 56 41 2 53 26 51 16 13 24 83 54 42 92 78 55 23 63 29 4 65 60 22 45 89 20 19 70 58 84 64 36 62 34 96 3 21 27 71 50 95 31 97 81 5 87 10 91 90 88 85 82 80 79 76 74 68 66 61 46 44 39 37 35 33 32 30 28 17 15 12 11 9 8 7 6 1 
62
67 71 78 64...

result:

ok Correct!

Test #28:

score: 0
Accepted
time: 9ms
memory: 3744kb

input:

5
95 95
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 1 0 0 0 1 1 0 1 0 1 1 1 0 0 2 1 0 0 1 1 0 0 1 0 1 0 0 3 0 2 1 1 5 0 0 0 4 3 0 2 1 0 3 2 2 1 2 1 6 1 3 4 2 1 2 2 3 0 2 0 2 1 1 2 2 1 0 2 1 0 0 1 2 3
16 2 9 23 36 52 58 31 65 66 71 72 17 75 24 44 27
28 4 2 32 38 45 21 52 61 94 55 46 79 92 1 11 65 73 95...

output:

89
16 17 37 71 83 44 27 3 9 73 60 92 18 85 54 72 94 64 25 43 29 66 7 52 95 82 23 33 32 14 36 34 68 55 30 19 10 77 20 89 65 41 80 6 84 21 28 45 91 74 31 1 40 57 22 88 63 38 67 51 2 58 42 46 39 47 69 61 49 87 81 48 76 50 26 13 59 79 4 86 78 70 35 15 11 93 75 56 53 90 62 24 12 8 5 
89
5 91 92 66 20 21 ...

result:

ok Correct!

Test #29:

score: 0
Accepted
time: 2ms
memory: 4036kb

input:

1
498 500
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 2 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 1 1 2 0 0 1 0 0 0 0 0 1 0 0 2 0 1 1 1 0 0 1 0 2 0 0 1 1 0 0 2 0 0 0 0 0 3 1 0 2 0 1 3 0 0 0 ...

output:

108
438 311 310 128 307 94 185 58 14 297 374 96 34 212 479 251 54 1 5 288 231 450 486 429 255 42 329 247 29 433 99 240 386 331 417 12 319 210 360 361 260 481 469 444 480 348 199 372 86 330 291 286 446 125 187 277 401 51 186 159 46 195 494 459 173 153 98 192 33 137 160 445 393 472 228 133 407 382 365...

result:

ok Correct!

Test #30:

score: 0
Accepted
time: 16ms
memory: 5632kb

input:

1
500 498
2 0 0 1 1 1 0 0 0 0 1 1 2 2 2 0 1 0 0 0 1 1 1 0 1 1 2 2 2 1 1 0 4 3 1 1 1 1 0 3 0 3 3 1 2 1 0 2 3 2 0 2 0 0 0 1 1 1 0 2 4 1 0 1 1 1 1 3 1 0 0 1 1 2 2 4 1 1 1 0 0 2 2 1 2 1 1 1 0 0 2 0 0 1 0 0 1 1 1 1 0 1 0 0 1 2 1 1 1 2 2 4 2 0 0 0 0 1 1 0 1 4 1 3 0 2 0 3 0 1 1 1 0 4 0 1 1 3 2 0 0 1 0 1 1 ...

output:

258
486 292 219 377 19 88 315 42 284 223 375 182 500 154 141 449 423 242 230 96 64 71 36 472 411 337 12 99 62 47 299 207 213 199 202 189 224 329 2 238 293 291 248 281 131 14 272 32 270 353 231 216 194 211 55 446 335 277 161 133 257 147 115 4 156 146 264 252 168 498 130 427 253 138 135 21 397 445 134...

result:

ok Correct!

Test #31:

score: 0
Accepted
time: 156ms
memory: 24796kb

input:

1
496 498
0 0 0 0 2 0 0 1 0 1 1 0 2 3 1 1 2 0 3 2 2 0 2 1 2 0 1 1 1 0 1 2 0 0 0 0 5 1 1 0 2 1 0 0 1 0 0 2 0 3 0 0 3 1 0 3 0 1 1 2 1 2 0 2 0 2 1 0 3 0 1 0 1 1 2 0 1 0 0 1 2 1 2 0 1 0 1 1 1 0 3 3 3 2 0 1 2 1 1 3 1 1 0 0 3 0 1 0 0 0 0 1 3 1 0 1 1 2 1 0 1 2 0 2 0 1 1 1 2 2 0 0 1 2 1 1 1 2 1 3 1 1 1 0 0 ...

output:

248
75 159 212 406 326 1 268 214 94 383 83 11 125 191 61 476 197 389 199 62 226 9 10 112 433 77 269 198 100 166 284 6 66 23 68 272 451 445 221 122 330 182 289 48 417 375 304 40 493 443 165 103 482 46 25 20 140 252 27 266 292 114 57 357 325 190 227 88 356 238 444 259 127 287 65 91 302 240 138 429 488...

result:

ok Correct!

Test #32:

score: 0
Accepted
time: 1ms
memory: 3700kb

input:

10
49 50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
0
0
1 4
0
1 7
2 8 7
2 9 8
0
1 10
1 12
2 13 12
0
0
1 16
2 16 17
0
1 19
0
1 20
1 21
1 23
2 24 23
2 25 24
1 26
1 27
1 27
1 28
0
2 31 30
1 32
0
1 33
1 34
2 35 36
1 37
2 38 37
1 38
1 40
2 41 4...

output:

35
15 49 48 46 45 44 42 41 40 39 37 36 35 34 33 31 30 28 26 25 24 23 22 21 20 18 16 12 11 10 8 7 6 4 1 47 43 38 32 29 27 19 17 14 13 9 5 3 2 
31
35 47 46 45 43 41 39 38 37 36 34 31 30 29 28 27 24 23 21 19 18 17 15 14 13 12 11 9 7 2 1 49 48 44 42 40 33 32 26 25 22 20 16 10 8 6 5 4 3 
30
44 42 41 40 3...

result:

ok Correct!

Test #33:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

5
99 99
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 2
0
0
2 5 4
1 5
1 7
0
1 9
1 10
0
2 12 11
1 12
1 14
1 14
2 15 16
1 17
0
1 19
1 19
1 21
2 21 2...

output:

60
33 66 32 12 5 99 97 96 95 94 93 91 88 86 85 84 81 77 76 75 73 71 70 69 68 67 62 60 58 57 56 54 53 51 50 45 44 42 41 40 38 36 34 31 27 26 24 23 22 20 18 16 15 13 11 9 8 6 4 1 98 92 90 89 87 83 82 80 79 78 74 72 65 64 63 61 59 55 52 49 48 47 46 43 39 37 35 30 29 28 25 21 19 17 14 10 7 3 2 
63
83 82...

result:

ok Correct!

Test #34:

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

input:

1
496 500
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

325
363 168 118 441 436 421 398 393 362 360 345 336 334 333 326 324 299 285 241 209 207 204 173 167 154 125 119 115 104 103 97 84 75 69 13 496 494 492 491 488 487 486 482 479 478 476 475 474 473 472 470 468 466 465 463 462 460 459 458 453 452 451 450 447 444 443 442 439 437 435 433 430 429 428 427 4...

result:

ok Correct!

Test #35:

score: 0
Accepted
time: 0ms
memory: 3848kb

input:

10
46 47
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
2 3 4
1 5
2 5 6
2 6 5
3 8 6 7
0
1 8
1 10
0
2 11 13
2 14 13
2 14 13
3 15 14 16
2 16 15
1 18
1 18
0
1 20
1 21
1 21
0
2 23 24
1 26
2 27 26
1 27
1 29
1 29
1 31
1 30
1 32
1 32
1 33
2 35 36
2 35 37
1...

output:

34
41 38 42 39 34 12 5 8 44 45 43 37 35 36 33 31 29 30 27 25 24 23 20 19 16 15 14 13 11 9 6 3 2 1 46 40 32 28 26 22 21 18 17 10 7 4 
36
13 44 40 27 31 21 14 12 4 46 45 42 41 37 38 36 35 34 33 29 30 28 24 22 18 17 15 16 9 10 8 7 5 3 2 1 48 47 43 39 32 26 25 23 20 19 11 6 
39
22 19 34 30 25 26 23 18 1...

result:

ok Correct!

Test #36:

score: 0
Accepted
time: 2ms
memory: 3660kb

input:

5
99 99
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 1 3 2
2 4 3
2 4 3
0
2 6 7
1 6
3 8 7 9
2 10 9
1 11
3 12 11 10
3 13 12 11
1 12
2 13 14
0
1 15
...

output:

75
9 12 2 95 92 89 86 84 79 76 52 37 20 11 10 6 3 99 98 97 96 93 91 90 87 88 85 83 81 80 77 75 74 73 71 70 68 66 65 64 63 62 60 59 58 56 55 53 50 48 47 46 45 43 40 41 39 38 36 33 32 30 27 24 23 22 19 18 16 15 13 8 7 5 1 94 82 78 72 69 67 61 57 54 51 49 44 42 35 34 31 29 28 26 25 21 17 14 4 
75
73 90...

result:

ok Correct!

Test #37:

score: 0
Accepted
time: 6ms
memory: 3996kb

input:

1
495 497
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

383
441 440 375 283 132 72 38 485 480 477 464 458 460 452 439 427 424 422 409 405 398 392 376 373 369 367 357 344 319 308 285 264 258 255 252 239 242 225 224 219 216 214 200 197 195 183 181 173 156 154 153 148 143 140 134 131 127 123 121 111 109 105 102 101 94 92 82 80 77 74 61 53 41 39 33 25 10 4 4...

result:

ok Correct!

Test #38:

score: 0
Accepted
time: 2ms
memory: 3596kb

input:

10
45 49
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 3 5 2
0
2 6 5
3 4 5 8
3 9 6 8
3 7 8 10
1 8
3 8 11 10
2 12 10
3 13 12 10
3 14 15 12
3 14 13 12
3 15 17 13
2 15 18
3 19 17 18
4 16 17 19 20
3 18 21 20
1 18
2 19 21
3 21 20 23
3 23 21 25
1 22
3 ...

output:

43
15 19 18 17 39 40 25 20 11 41 42 36 34 29 26 21 14 7 9 12 44 45 43 38 37 35 32 33 31 27 28 24 23 22 13 16 8 10 5 6 3 4 1 30 2 
42
9 40 25 22 7 39 42 37 24 26 29 23 15 8 4 43 44 38 36 35 33 34 32 27 30 28 20 18 21 19 17 16 14 11 10 12 5 6 1 3 2 45 41 31 13 
44
41 42 25 9 38 21 15 12 5 1 34 37 29 1...

result:

ok Correct!

Test #39:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

5
95 98
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 5 1 3
1 6
3 5 4 7
2 7 8
1 7
3 7 10 6
3 9 11 8
4 10 8 9 11
3 9 10 12
1 12
3 12 14 15
1 16
4 14 ...

output:

90
63 61 47 85 78 68 60 59 67 45 15 17 12 92 81 87 79 77 73 71 62 66 64 56 52 51 50 44 41 40 36 16 21 14 10 9 5 1 94 91 93 88 90 84 86 82 83 76 74 75 72 70 69 65 58 55 54 53 49 48 46 43 42 39 37 35 38 34 33 30 32 31 29 27 25 26 24 23 22 20 19 18 11 13 7 8 4 2 3 95 89 80 57 28 6 
90
61 21 8 3 97 94 8...

result:

ok Correct!

Test #40:

score: 0
Accepted
time: 9ms
memory: 3844kb

input:

1
498 496
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

462
430 318 427 362 345 319 278 157 159 144 100 495 484 480 473 458 449 441 428 419 414 417 407 399 378 379 365 342 334 316 313 303 302 292 284 280 236 211 158 152 143 146 133 123 130 121 103 88 63 54 45 34 26 13 18 3 491 487 485 482 477 476 471 465 457 463 451 450 442 438 435 425 426 418 410 406 40...

result:

ok Correct!

Test #41:

score: 0
Accepted
time: 2ms
memory: 3724kb

input:

10
49 47
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
7 3 1 7 4 9 8 10
6 11 2 6 3 4 5
3 3 12 10
5 5 9 6 12 7
5 13 7 10 9 11
3 10 6 12
6 12 16 7 8 10 9
7 11 9 16 12 17 10 15
5 15 14 11 13 9
3 12 18 16
5 13 20 17 15 19
4 15 19 17 12
7 13 15 22 18 19 17 ...

output:

47
30 15 5 35 44 49 41 40 38 31 23 21 20 13 10 2 47 39 36 42 33 32 37 29 34 24 27 28 26 19 17 25 22 16 11 12 18 14 7 9 6 8 48 45 46 4 1 43 3 
48
17 48 23 7 14 2 45 46 40 28 31 26 37 32 20 18 5 12 3 8 36 44 38 42 41 39 33 29 35 34 27 30 24 25 22 19 21 16 11 15 9 13 10 6 4 1 43 47 
47
33 18 21 22 11 4...

result:

ok Correct!

Test #42:

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

input:

5
99 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 9 7
5 2 9 8 11 7
6 5 8 4 9 3 11
6 9 8 13 10 12 4
6 5 6 12 14 9 11
2 10 14
8 12 15 8 9 14 ...

output:

99
66 70 58 72 71 83 87 55 2 89 76 67 81 52 48 62 29 26 33 37 13 25 21 96 88 80 84 82 77 73 63 59 51 56 49 61 43 46 39 31 42 32 10 18 22 7 4 97 99 98 90 92 94 91 86 85 79 78 74 75 69 65 60 68 64 57 50 53 54 47 45 44 40 38 41 36 34 35 28 30 24 19 27 20 17 23 15 9 16 11 14 12 8 6 1 5 3 95 93 
95
67 46...

result:

ok Correct!

Test #43:

score: 0
Accepted
time: 16ms
memory: 3840kb

input:

1
498 499
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

497
30 34 481 362 203 114 31 36 486 476 446 438 416 357 358 356 306 325 300 205 180 143 104 117 100 68 78 46 39 29 497 488 470 480 473 453 447 435 437 455 428 434 425 421 420 408 413 366 368 346 342 348 353 332 310 323 328 302 288 281 272 275 265 277 274 271 241 217 218 207 214 190 176 177 174 162 1...

result:

ok Correct!