- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ program to find out the maximum possible tally from given integers

Suppose, we are given two integers n and m and there are k tuples of integers that contain four integer numbers {ai, bi, ci, di}. Four arrays a, b, c, d are given, and a[i] signifies the i-th tuple's a value. Now, let us consider a sequence dp that has n positive integers and 1 <= dp[1] < dp[2] < ..... < dp[n] <= m. We define a metric 'tally'. The metric tally is the sum of d[i] over all indices i such that dp[b[i]] − dp[a[i]] = c[i]. If there is no such i, the tally is 0. We have to find out the maximum possible tally of dp.

So, if the input is like n = 4, m = 5, k = 4, a = {2, 2, 3, 5}, b = {4, 3, 4, 6}, c = {4, 3, 3, 4}, d = {110, 20, 20, 40}, then the output will be 130.

## Steps

To solve this, we will follow these steps −

Define arrays A, B, C, D, and dp of sizes: 100, 100, 100, 100, 10 respectively. Define a function depthSearch(), this will take c, l, if c is same as n, then: total := 0 for initialize i := 0, when i < k, update (increase i by 1), do: if dp[B[i]] - dp[A[i]] is same as C[i], then: total := total + D[i] res := maximum of res and total return for initialize j := l, when j <= m, update (increase j by 1), do: dp[c] := j depthSearch(c + 1, j) for initialize i := 0, when i < k, update (increase i by 1), do: A[i] := a[i], B[i] := b[i], C[i] := c[i], D[i] := d[i] decrease A[i] by 1 decrease B[i] by 1 depthSearch(0, 1) return res

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int n, m, k, res = 0; int A[100], B[100], C[100], D[100], dp[10]; void depthSearch(int c, int l){ if(c == n){ int total = 0; for(int i = 0; i < k; i++) { if(dp[B[i]] - dp[A[i]] == C[i]) total += D[i]; } res = max(res, total); return; } for(int j = l; j <= m; j++){ dp[c] = j; depthSearch(c + 1, j); } } int solve(int a[], int b[], int c[], int d[]){ for(int i = 0; i < k; i++){ A[i] = a[i], B[i] = b[i], C[i] = c[i], D[i] = d[i]; A[i]--, B[i]--; } depthSearch(0, 1); return res; } int main() { n = 4, m = 5, k = 4; int a[] = {2, 2, 3, 5}, b[] = {4, 3, 4, 6}, c[] = {4, 3, 3, 4}, d[] = {110, 20, 20, 40}; cout<< solve(a, b, c, d); return 0; }

## Input

4, 5, 4, {2, 2, 3, 5}, {4, 3, 4, 6}, {4, 3, 3, 4}, {110, 20, 20, 40}

## Output

130

- Related Articles
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Program to Find Out the Maximum Points From Removals in Python
- C++ program to find out the maximum value of i
- C++ Program to find out the maximum rated parts set
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- C++ program to find maximum possible value of XORed sum
- Program to find the maximum score from all possible valid paths in Python
- C++ Program to find out if a round trip is possible from a particular city
- C++ program to find maximum possible value for which XORed sum is maximum
- C program to find maximum of four integers by defining function
- Find maximum possible stolen value from houses in C++
- C++ program to find maximum possible amount of allowance after playing the game
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- C++ Program to find out the maximum amount of money that can be made from selling cars
- C++ Program to find out the maximum amount of profit that can be achieved from selling wheat