Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.Comparator;
- import java.util.StringTokenizer;
- import java.util.*;
- public class Cardboard_va implements Runnable {
- public static void main(String[] args) {
- new Thread(new Cardboard_va()).run();
- }
- BufferedReader br;
- StringTokenizer in;
- PrintWriter out;
- public String nextToken() throws IOException {
- while (in == null || !in.hasMoreTokens()) {
- in = new StringTokenizer(br.readLine());
- }
- return in.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- public long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- public double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- public class Level {
- int a, b, ind;
- public Level(int a, int b, int ind) {
- this.a = a;
- this.b = b;
- this.ind = ind;
- }
- }
- public class LevelComparator implements Comparator<Level> {
- @Override
- public int compare(Level o1, Level o2) {
- return o1.b == o2.b ? o1.a - o2.a : o1.b - o2.b;
- }
- }
- public class Level1Comparator implements Comparator<Level> {
- @Override
- public int compare(Level o1, Level o2) {
- return o1.a - o2.a;
- }
- }
- public class LevelDiffComparator implements Comparator<Level> {
- public int compare(Level o1, Level o2) {
- return o1.b - o1.a - (o2.b - o2.a);
- }
- }
- Random rnd = new Random(29);
- public class Node {
- int x, id, y, size;
- long sum;
- Node l, r;
- public Node(int x, int id) {
- this.x = x;
- this.id = id;
- y = rnd.nextInt();
- size = 1;
- sum = x;
- }
- public String toString() {
- return x + " " + id + " " + size + " " + sum + " [ Left: " + l + ", Right: " + r + "]";
- }
- }
- public int size(Node n) {
- return n == null ? 0 : n.size;
- }
- public long sum(Node n) {
- return n == null ? 0 : n.sum;
- }
- public void update(Node n) {
- n.size = size(n.l) + size(n.r) + 1;
- n.sum = sum(n.l) + sum(n.r) + n.x;
- }
- public Node[] split(Node n, int x, int id) {
- if (n == null) {
- return new Node[]{null, null};
- }
- if (n.x < x || (n.x == x && n.id < id)) {
- Node[] t = split(n.r, x, id);
- n.r = t[0];
- update(n);
- return new Node[]{n, t[1]};
- } else {
- Node[] t = split(n.l, x, id);
- n.l = t[1];
- update(n);
- return new Node[]{t[0], n};
- }
- }
- public Node merge(Node l, Node r) {
- if (l == null)
- return r;
- if (r == null)
- return l;
- if (l.y < r.y) {
- l.r = merge(l.r, r);
- update(l);
- return l;
- } else {
- r.l = merge(l, r.l);
- update(r);
- return r;
- }
- }
- public long sum(Node n, int k) {
- if (k == 0) {
- return 0;
- }
- if (size(n.l) >= k) {
- return sum(n.l, k);
- }
- long ans = sum(n.l);
- ans += n.x;
- return ans + sum(n.r, k - size(n.l) - 1);
- }
- public Node delete(Node n, int x, int id) {
- Node[] s1 = split(n, x, id);
- Node[] s2 = split(s1[1], x, id + 1);
- return merge(s1[0], s2[1]);
- }
- public Node add(Node n, int x, int id) {
- Node add = new Node(x, id);
- Node[] s = split(n, x, id);
- return merge(s[0], merge(add, s[1]));
- }
- public void solve() throws IOException {
- int n = nextInt();
- int w = nextInt();
- Level[] a = new Level[n];
- for (int i = 0; i < n; i++) {
- a[i] = new Level(nextInt(), nextInt(), i);
- }
- Arrays.sort(a, new LevelComparator());
- Node root = null;
- int bd = -1;
- long ans = Long.MAX_VALUE;
- long total = 0;
- for (int i = 0; i < w - n; i++) {
- total += a[i].a;
- root = add(root, a[i].b - a[i].a, a[i].ind);
- }
- for (int i = Math.max(w - n, 0); i < n; i++) {
- root = add(root, a[i].a, a[i].ind);
- }
- for (int s = Math.max(w - n, 0); s <= Math.min(w, n); s++) {
- long cnt = total + sum(root, w - s);
- if (cnt < ans) {
- ans = cnt;
- bd = s;
- }
- if (s < n) {
- total += a[s].a;
- root = delete(root, a[s].a, a[s].ind);
- root = add(root, a[s].b - a[s].a, a[s].ind);
- }
- }
- if (bd == -1) {
- throw new AssertionError();
- }
- int[] r = new int[n];
- Level[] b = new Level[n];
- for (int i = 0; i < b.length; i++) {
- if (i < bd) {
- b[i] = new Level(a[i].b - a[i].a, 0, a[i].ind);
- r[a[i].ind]++;
- } else
- b[i] = a[i];
- }
- Arrays.sort(b, new Level1Comparator());
- for (int i = 0; i < w - bd; i++) {
- r[b[i].ind]++;
- }
- out.println(ans);
- for (int i = 0; i < n; i++) {
- out.print(r[i]);
- }
- }
- public void run() {
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- solve();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement