CSES - Datatähti 2024 alku - Results
Submission details
Task:Laskettelukeskus
Sender:Verlet
Submission time:2023-10-30 18:05:28 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int haku(int)':
input/code.cpp:20:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |   for (int child = 0; child < v[index].size())
      |                       ~~~~~~^~~~~~~~~~~~~~~~~
input/code.cpp:20:46: error: expected ';' before ')' token
   20 |   for (int child = 0; child < v[index].size())
      |                                              ^
      |                                              ;

Code

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<vector<int>> v;
vector<int> values;

int haku(int index)
{
  // If the node has no children
  if (v[index].size() == 0) return values[index];

  // The node has children
  
  // The number needed to plow the children
  int s = 0;

  for (int child = 0; child < v[index].size())
  {
    s += haku(v[index][child]);
  }

  /*
  Return the max of the number needed to
  plow the children and the number needed
  to plow the node
  */

  return max(s, values[index]);
}

int main()
{
  int n = 10;

  cin >> n;

  v.resize(n);
  values.resize(n);

  for (int i = 0; i < n; i++)
  {
    int a, b;

    cin >> a >> b;

    v[a-1].push_back(b-1);
  }

  for (int i = 0; i < n; i++)
  {
    cin >> values[i];
  }

  cout << haku(0);
}