1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include<vector>
using namespace std;

struct Block
{
  int side;
  int col;
};

struct Tower
{
  vector<Block> blocks;
  int height;
  int mark;
};

Tower AddBlock(Tower tower, Block block, int punish)
{
  if(tower.blocks.size()>0)
  {
    if(tower.blocks[tower.blocks.size()-1].side>=block.side) 
    {
      tower.mark = 0;
      return tower;
    }
    else if(tower.blocks[tower.blocks.size()-1].col!=block.col)
    {
      tower.mark-=punish;
    }
  }
  tower.height+=block.side;
  tower.mark+=block.side;
  tower.blocks.push_back(block);
  return tower;
}

int main() 
{
    Tower tower;
    vector<Block> blocks;
    vector<Tower> towers;
    int punish, n, max=0;
    tower.mark=0;
    tower.height=0;
    cin>>n>>punish;
    for(int i=0;i<n;i++)
    {
      Block a;
      cin>>a.side>>a.col;
      if(towers.size()==0)
      {
        towers.push_back(tower);
      }
      int size=towers.size();
      for(int j=0;j<size;j++)
      {
          tower=AddBlock(towers[j],a,punish);
          if(tower.mark>0)
          {
            towers.push_back(tower);
          }
          if(tower.mark>max) max=tower.mark;
      }
      size=towers.size();
    }
    cout<<max;
    return 0;
}