{"id":28169,"date":"2022-12-29T02:19:39","date_gmt":"2022-12-28T20:49:39","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/"},"modified":"2022-12-29T02:19:39","modified_gmt":"2022-12-28T20:49:39","slug":"solved-c-chocolate-puzzle-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/","title":{"rendered":"[Solved] C++ Chocolate Puzzle [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-14977539\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"14977539\" data-parentid=\"14976211\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here is how I would solve this in Java:<\/p>\n<pre><code>import java.util.HashMap;\nimport java.util.Map;\nimport java.util.Random;\n\npublic class ChocolatePuzzle {\n    private static final Map &lt;String, Integer&gt; solutions =\n        new HashMap &lt;String, Integer&gt; ();\n\n    private static final Map &lt;String, Integer&gt; bestMoves =\n            new HashMap &lt;String, Integer&gt; ();\n\n    private static int [] x;\n\n    private static int k (int from, int to)\n    {\n        int sum = x [from];\n        int max = x [from];\n        int min = x [from];\n        for (int i = from + 1; i &lt; to; i++)\n        {\n            sum += x [i];\n            max = Math.max (max, x [i]);\n            min = Math.min (min, x [i]);\n        }\n\n        return sum * 3 + max * max - min * min;\n    }\n\n    public static int solve (int n, int p)\n    {\n        String signature = n + \",\" + p;\n        Integer solution = solutions.get (signature);\n        if (solution == null)\n        {\n            solution = Integer.valueOf (doSolve (n, p, signature));\n            solutions.put (signature, solution);\n        }\n        return solution.intValue ();\n    }\n\n    public static int doSolve (int n, int p, String signature)\n    {\n        if (p == 1)\n        {\n            bestMoves.put (signature, Integer.valueOf (x.length - n));\n            return k (n, x.length);\n        }\n        else\n        {\n            int result = Integer.MAX_VALUE;\n            int bestMove = 0;\n\n            int maxI = x.length - n - p + 1;\n            for (int i = 1; i &lt;= maxI; i++)\n            {\n                int k = Math.max (k (n, n + i), solve (n + i, p - 1));\n\n                if (k &lt; result)\n                {\n                    result = k;\n                    bestMove = i;\n                }\n            }\n\n            bestMoves.put (signature, Integer.valueOf (bestMove));\n\n            return result;\n        }\n    }\n\n    public static void main(String[] args) {\n        int n = 20;\n        int p = 5;\n        x = new int [n];\n\n        Random r = new Random ();\n        for (int i = 0; i &lt; n; i++)\n            x [i] = r.nextInt (9) + 1;\n\n        System.out.println(\"N: \" + n);\n        System.out.println(\"P: \" + p);\n        System.out.print(\"X: {\");\n        for (int i = 0; i &lt; n; i++)\n        {\n            if (i &gt; 0) System.out.print (\", \");\n            System.out.print (x [i]);\n        }\n        System.out.println(\"}\");\n\n        System.out.println();\n\n        int k = solve (0, p);\n\n        int o = 0;\n        for (int i = p; i &gt; 0; i--)\n        {\n            int m = bestMoves.get (o + \",\" + i);\n\n            System.out.print (\"{\");\n            for (int j = 0; j &lt; m; j++)\n            {\n                if (j &gt; 0)\n                    System.out.print (\", \");\n                System.out.print (x [o + j]);\n            }\n            System.out.print (\"} (k: \");\n            System.out.print(k (o, o + m));\n            System.out.println (\")\");\n\n            o += m;\n        }\n\n        System.out.println(\"min(k): \" + k);\n    }\n}\n<\/code><\/pre>\n<p>Probably you could find some useful tips in this code.<\/p>\n<p>Sample input:<\/p>\n<pre><code>N: 20\nP: 5\nX: {1, 7, 6, 6, 5, 5, 7, 9, 1, 3, 9, 5, 3, 7, 9, 1, 4, 2, 4, 8}\n<\/code><\/pre>\n<p>Sample output:<\/p>\n<pre><code>{1, 7, 6, 6} (k: 108)\n{5, 5, 7, 9} (k: 134)\n{1, 3, 9, 5} (k: 134)\n{3, 7, 9} (k: 129)\n{1, 4, 2, 4, 8} (k: 120)\nmin(k): 134\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved C++ Chocolate Puzzle [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here is how I would solve this in Java: import java.util.HashMap; import java.util.Map; import java.util.Random; public class ChocolatePuzzle { private static final Map &lt;String, Integer&gt; solutions = new HashMap &lt;String, Integer&gt; (); private static final Map &lt;String, Integer&gt; bestMoves = new HashMap &lt;String, Integer&gt; (); private static int [] x; private static int k &#8230; <a title=\"[Solved] C++ Chocolate Puzzle [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\" aria-label=\"More on [Solved] C++ Chocolate Puzzle [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[457,324,1324],"class_list":["post-28169","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-c","tag-puzzle"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] C++ Chocolate Puzzle [closed] - JassWeb<\/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:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] C++ Chocolate Puzzle [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here is how I would solve this in Java: import java.util.HashMap; import java.util.Map; import java.util.Random; public class ChocolatePuzzle { private static final Map &lt;String, Integer&gt; solutions = new HashMap &lt;String, Integer&gt; (); private static final Map &lt;String, Integer&gt; bestMoves = new HashMap &lt;String, Integer&gt; (); private static int [] x; private static int k ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-28T20:49:39+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] C++ Chocolate Puzzle [closed]\",\"datePublished\":\"2022-12-28T20:49:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\"},\"wordCount\":35,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"algorithm\",\"c++\",\"puzzle\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\",\"name\":\"[Solved] C++ Chocolate Puzzle [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-28T20:49:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] C++ Chocolate Puzzle [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] C++ Chocolate Puzzle [closed] - JassWeb","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:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] C++ Chocolate Puzzle [closed] - JassWeb","og_description":"[ad_1] Here is how I would solve this in Java: import java.util.HashMap; import java.util.Map; import java.util.Random; public class ChocolatePuzzle { private static final Map &lt;String, Integer&gt; solutions = new HashMap &lt;String, Integer&gt; (); private static final Map &lt;String, Integer&gt; bestMoves = new HashMap &lt;String, Integer&gt; (); private static int [] x; private static int k ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/","og_site_name":"JassWeb","article_published_time":"2022-12-28T20:49:39+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] C++ Chocolate Puzzle [closed]","datePublished":"2022-12-28T20:49:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/"},"wordCount":35,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","c++","puzzle"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/","name":"[Solved] C++ Chocolate Puzzle [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-28T20:49:39+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-c-chocolate-puzzle-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] C++ Chocolate Puzzle [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=28169"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28169\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=28169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=28169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=28169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}