<p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>

<p>We want to place these books in order onto bookcase shelves that have a total width <code>shelfWidth</code>.</p>

<p>We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to <code>shelfWidth</code>, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.</p>

<p>Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.</p>

<ul>
	<li>For example, if we have an ordered list of <code>5</code> books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.</li>
</ul>

<p>Return <em>the minimum possible height that the total bookshelf can be after placing shelves in this manner</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/06/24/shelves.png" style="height: 500px; width: 337px;" />
<pre>
<strong>Input:</strong> books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelfWidth = 4
<strong>Output:</strong> 6
<strong>Explanation:</strong>
The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
Notice that book number 2 does not have to be on the first shelf.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> books = [[1,3],[2,4],[3,2]], shelfWidth = 6
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= books.length &lt;= 1000</code></li>
	<li><code>1 &lt;= thickness<sub>i</sub> &lt;= shelfWidth &lt;= 1000</code></li>
	<li><code>1 &lt;= height<sub>i</sub> &lt;= 1000</code></li>
</ul>


struct Shape {
    int width;
    int height;
    
    Shape(int width, int height): width(width), height(height) {}
};

class Solution {
public:
    int func2(int pos, const Shape& level_shape, int width, const std::vector<std::vector<int>>& books, std::vector<std::unordered_map<int, int>>& dp) {
        if (pos >= books.size()) {
            //std::cout << "done: level height: " << level_shape.height << "\n";
            return level_shape.height;
        }
        
        
        //std::cout << "pos: " << pos << " level height: " << level_shape.height << ", level width: " << level_shape.width << "\n";
        
        int key = level_shape.width;
        key <<= 10;
        key |= level_shape.height;
        auto it = dp[pos].find(key);
        
        if (it != dp[pos].end()) {
            //std::cout << "pos: " << pos << ", dp: " << it->second << "\n";
            return it->second;
        }
        
        //std::cout << "pos: " << pos << " new level\n";
        int h1 = level_shape.height + func2(pos + 1, Shape(books[pos][0], books[pos][1]), width, books, dp);
        int h2 = INT32_MAX;
        
        if (level_shape.width + books[pos][0] <= width) {
            //std::cout << "pos: " << pos << " add to level\n";
            h2 = func2(pos + 1, Shape(level_shape.width + books[pos][0], std::max(books[pos][1], level_shape.height)), width, books, dp);
        }
        
        int h = std::min(h1, h2);
        dp[pos][key] = h;
        //std::cout << "pos: " << pos << " level height: " << level_shape.height << ", level width: " << level_shape.width << " H: " << h << "\n";
        return h;
    }
    
    
    int func(int pos, const Shape& level_shape, const Shape& shelf_shape, const std::vector<std::vector<int>>& books, std::vector<std::unordered_map<int, int>>& dp) {
        if (pos >= books.size()) {
            std::cout << "done: level height: " << level_shape.height << ", shelf height: " << shelf_shape.height << ", total: " << (shelf_shape.height + level_shape.height) << "\n";
            return shelf_shape.height + level_shape.height;
        }
        
        
        std::cout << "pos: " << pos << " level height: " << level_shape.height << ", level width: " << level_shape.width << ", shelf height: " << shelf_shape.height << "\n";
        
        int key = level_shape.width;
        key <<= 10;
        key |= level_shape.height;
        auto it = dp[pos].find(key);
        
        if (it != dp[pos].end()) {
            std::cout << "pos: " << pos << ", dp: " << it->second << "\n";
            return it->second;
        }
        
        std::cout << "pos: " << pos << " new level\n";
        int h1 = func(pos + 1, Shape(books[pos][0], books[pos][1]), Shape(shelf_shape.width, shelf_shape.height + level_shape.height), books, dp);
        int h2 = INT32_MAX;
        
        if (level_shape.width + books[pos][0] <= shelf_shape.width) {
            std::cout << "pos: " << pos << " add to level\n";
            h2 = func(pos + 1, Shape(level_shape.width + books[pos][0], std::max(books[pos][1], level_shape.height)), shelf_shape, books, dp);
        }
        
        int h = std::min(h1, h2);
        dp[pos][key] = h;
        std::cout << "pos: " << pos << " level height: " << level_shape.height << ", level width: " << level_shape.width << " H: " << h << "\n";
        return h;
    }
    
    int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
        std::vector<std::unordered_map<int, int>> dp(books.size());
        //return func(1, Shape(books[0][0], books[0][1]), Shape(shelfWidth, 0), books, dp);
        return func2(1, Shape(books[0][0], books[0][1]), shelfWidth, books, dp);
    }
};





/*

[[11,83],[170,4],[93,80],[155,163],[134,118],[75,14],[122,192],[123,154],[187,29],[160,64],[170,152],[113,179],[60,102],[28,187],[59,95],[187,97],[49,193],[67,126],[75,45],[130,160],[4,102],[116,171],[43,170],[96,188],[54,15],[167,183],[58,158],[59,55],[148,183],[89,95],[90,113],[51,49],[91,28],[172,103],[173,3],[131,78],[11,199],[77,200],[58,65],[77,30],[157,58],[18,194],[101,148],[22,197],[76,181],[21,176],[50,45],[80,174],[116,198],[138,9],[58,125],[163,102],[133,175],[21,39],[141,156],[34,185],[14,113],[11,34],[35,184],[16,132],[78,147],[85,170],[32,149],[46,94],[196,3],[155,90],[9,114],[117,119],[17,157],[94,178],[53,55],[103,142],[70,121],[9,141],[16,170],[92,137],[157,30],[94,82],[144,149],[128,160],[8,147],[153,198],[12,22],[140,68],[64,172],[86,63],[66,158],[23,15],[120,99],[27,165],[79,174],[46,19],[60,98],[160,172],[128,184],[63,172],[135,54],[40,4],[102,171],[29,125],[81,9],[111,197],[16,90],[22,150],[168,126],[187,61],[47,190],[54,110],[106,102],[55,47],[117,134],[33,107],[2,10],[18,62],[109,188],[113,37],[59,159],[120,175],[17,147],[112,195],[177,53],[148,173],[29,105],[196,32],[123,51],[29,19],[161,178],[148,2],[70,124],[126,9],[105,87],[41,121],[147,10],[78,167],[91,197],[22,98],[73,33],[148,194],[166,64],[33,138],[139,158],[160,19],[140,27],[103,109],[88,16],[99,181],[2,140],[50,188],[200,77],[73,84],[159,130],[115,199],[152,79],[1,172],[124,136],[117,138],[158,86],[193,150],[56,57],[150,133],[52,186],[21,145],[127,97],[108,110],[174,44],[199,169],[139,200],[66,48],[52,190],[27,86],[142,191],[191,79],[126,114],[125,100],[176,95],[104,79],[146,189],[144,78],[52,106],[74,74],[163,128],[34,181],[20,178],[15,107],[105,8],[66,142],[39,126],[95,59],[164,69],[138,18],[110,145],[128,200],[149,150],[149,93],[145,140],[90,170],[81,127],[57,151],[167,127],[95,89]]
200

*/
13600