{"id":93,"date":"2012-08-18T08:17:17","date_gmt":"2012-08-18T12:17:17","guid":{"rendered":"http:\/\/www.ferociouscoder.com\/blog\/?p=93"},"modified":"2013-07-13T02:51:03","modified_gmt":"2013-07-13T06:51:03","slug":"airport-218b-codeforces-round-134-div-2","status":"publish","type":"post","link":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html","title":{"rendered":"Airport (218B) &#8211; Codeforces Round #134 (Div. 2)"},"content":{"rendered":"<p><a href=\"http:\/\/codeforces.com\/problemset\/problem\/218\/B\" title=\"Airport (218B) - Codeforces\" target=\"_blank\">http:\/\/codeforces.com\/problemset\/problem\/218\/B<\/a><\/p>\n<p>This is a very easy problem to solve given that you know what data structure to use!<\/p>\n<p>The data structure you need here is a heap. A heap is a tree like data structure which offers O(log(n)) inserts and removes and it also keeps the smallest or biggest element at the head.<\/p>\n<p>Here is my code for solving the question:<br \/>\nPriorityQueue<> is the heap like data structure in java. It is a min-heap by default so I used some trickery to quickly convert it into a max-heap.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.*;\r\n\r\npublic class B {\r\n\r\n  public static void main(String&#x5B;] args) {\r\n    Scanner sc = new Scanner(System.in);\r\n    int n = sc.nextInt();\r\n    int m = sc.nextInt();\r\n    int&#x5B;] seats = new int&#x5B;m];\r\n    for (int i = 0; i &lt; seats.length; i++) {\r\n      seats&#x5B;i] = sc.nextInt();\r\n    }\r\n    sc.close();\r\n\r\n    PriorityQueue&lt;Integer&gt; maxHeap = new PriorityQueue&lt;Integer&gt;();\r\n    PriorityQueue&lt;Integer&gt; minHeap = new PriorityQueue&lt;Integer&gt;();\r\n    for (int i = 0; i &lt; seats.length; i++) {\r\n      if (seats&#x5B;i] != 0) {\r\n        maxHeap.add(-seats&#x5B;i]);\r\n        minHeap.add(seats&#x5B;i]);\r\n      }\r\n    }\r\n\r\n    long max = 0;\r\n    for (int i = 0; i &lt; n; i++) {\r\n      int t = -maxHeap.poll();\r\n      max += t;\r\n      if (t - 1 != 0)\r\n        maxHeap.add(-(t - 1));\r\n    }\r\n\r\n    long min = 0;\r\n    for (int i = 0; i &lt; n; i++) {\r\n      int t = minHeap.poll();\r\n      min += t;\r\n      if (t - 1 != 0)\r\n        minHeap.add(t - 1);\r\n    }\r\n\r\n    System.out.println(max + &quot; &quot; + min);\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/codeforces.com\/problemset\/problem\/218\/B This is a very easy problem to solve given that you know what data structure to use! The data structure you need here is a heap. A heap is a tree like data structure which offers O(log(n)) inserts and removes and it also keeps the smallest or biggest element at the head. Here is &hellip; <a href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Airport (218B) &#8211; Codeforces Round #134 (Div. 2)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[24],"tags":[25,20],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-codeforces","tag-codeforces-2","tag-java-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Airport (218B) - Codeforces Round #134 (Div. 2) - Ferocious Coder<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Airport (218B) - Codeforces Round #134 (Div. 2) - Ferocious Coder\" \/>\n<meta property=\"og:description\" content=\"http:\/\/codeforces.com\/problemset\/problem\/218\/B This is a very easy problem to solve given that you know what data structure to use! The data structure you need here is a heap. A heap is a tree like data structure which offers O(log(n)) inserts and removes and it also keeps the smallest or biggest element at the head. Here is &hellip; Continue reading Airport (218B) &#8211; Codeforces Round #134 (Div. 2) &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html\" \/>\n<meta property=\"og:site_name\" content=\"Ferocious Coder\" \/>\n<meta property=\"article:published_time\" content=\"2012-08-18T12:17:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-13T06:51:03+00:00\" \/>\n<meta name=\"author\" content=\"Rawrosaur\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rawrosaur\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html\"},\"author\":{\"name\":\"Rawrosaur\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"headline\":\"Airport (218B) &#8211; Codeforces Round #134 (Div. 2)\",\"datePublished\":\"2012-08-18T12:17:17+00:00\",\"dateModified\":\"2013-07-13T06:51:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html\"},\"wordCount\":241,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"keywords\":[\"codeforces\",\"java\"],\"articleSection\":[\"Codeforces\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html\",\"name\":\"Airport (218B) - Codeforces Round #134 (Div. 2) - Ferocious Coder\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\"},\"datePublished\":\"2012-08-18T12:17:17+00:00\",\"dateModified\":\"2013-07-13T06:51:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/airport-218b-codeforces-round-134-div-2-93.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Airport (218B) &#8211; Codeforces Round #134 (Div. 2)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/\",\"name\":\"Ferocious Coder\",\"description\":\"RAWR!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\",\"name\":\"Rawrosaur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"caption\":\"Rawrosaur\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\"},\"sameAs\":[\"http:\\\/\\\/www.ferociouscoder.com\\\/\"],\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Airport (218B) - Codeforces Round #134 (Div. 2) - Ferocious Coder","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html","og_locale":"en_US","og_type":"article","og_title":"Airport (218B) - Codeforces Round #134 (Div. 2) - Ferocious Coder","og_description":"http:\/\/codeforces.com\/problemset\/problem\/218\/B This is a very easy problem to solve given that you know what data structure to use! The data structure you need here is a heap. A heap is a tree like data structure which offers O(log(n)) inserts and removes and it also keeps the smallest or biggest element at the head. Here is &hellip; Continue reading Airport (218B) &#8211; Codeforces Round #134 (Div. 2) &rarr;","og_url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html","og_site_name":"Ferocious Coder","article_published_time":"2012-08-18T12:17:17+00:00","article_modified_time":"2013-07-13T06:51:03+00:00","author":"Rawrosaur","twitter_misc":{"Written by":"Rawrosaur","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html#article","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html"},"author":{"name":"Rawrosaur","@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"headline":"Airport (218B) &#8211; Codeforces Round #134 (Div. 2)","datePublished":"2012-08-18T12:17:17+00:00","dateModified":"2013-07-13T06:51:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html"},"wordCount":241,"commentCount":1,"publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"keywords":["codeforces","java"],"articleSection":["Codeforces"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html","url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html","name":"Airport (218B) - Codeforces Round #134 (Div. 2) - Ferocious Coder","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#website"},"datePublished":"2012-08-18T12:17:17+00:00","dateModified":"2013-07-13T06:51:03+00:00","breadcrumb":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/airport-218b-codeforces-round-134-div-2-93.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ferociouscoder.com\/blog"},{"@type":"ListItem","position":2,"name":"Airport (218B) &#8211; Codeforces Round #134 (Div. 2)"}]},{"@type":"WebSite","@id":"https:\/\/www.ferociouscoder.com\/blog\/#website","url":"https:\/\/www.ferociouscoder.com\/blog\/","name":"Ferocious Coder","description":"RAWR!","publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ferociouscoder.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a","name":"Rawrosaur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","caption":"Rawrosaur"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg"},"sameAs":["http:\/\/www.ferociouscoder.com\/"],"url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1mYMV-1v","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":5,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}