I'm not sure i understand you well, but the answer is quite obvious...
If the second line represents pairs of cabins, then you need to use below "algorithm":
30 - 20 + 1 = 11
80 - 75 + 1 = 6
----------------
sum: 17
A [+ 1] is needed because you need to "include" the last broken cabin ;)
[EDIT]
Accordingly to...
Quote:
let say if there are 150 cabins and 6 broken cabins 20, 30, 75, 80, 110, 140
and number of pieces to be cut is 3
.....20.....30....75....80.....110....140....
then
-----|-- 11 --|---|-- 6 --|-----|--31--|-----
or
-----|-- 11 --|---|----- 36 -----|---|-1-|---
now the answer is 11+6+31
but another possible ways is <code">56+31+1
In other words, you have to get all possible combinations without repetitions (where the order in the group is matter) of broken cabins which can create a group of 3 (the number of parts of tape).
I'd suggest to get piece of paper and pencil, then you'll be able to write algorithm.
Well... Broken cabins
{20, 30, 75, 80, 110, 140}
can create the following groups of three elements (items). See the
image[
^]
There is 10 possible combinations:
1) 6 = 1 + 1 + 4 => 20, 30, {75, 80, 110, 140} => 1+1+66=68
2) 6 = 1 + 2 + 3 => 20, {30, 75}, {80, 110, 140} => 1+46+61=108
3) 6 = 1 + 3 + 2 => 20, {30, 75, 80}, {110, 140} => 1+51+31=83
4) 6 = 1 + 4 + 1 => 20, {30, 75, 80, 110}, 140 => 1+81+1=82
5) 6 = 2 + 1 + 3 => {20, 30}, 75, {80, 110, 140} => 11+1+61=73
6) 6 = 2 + 2 + 2 => {20, 30}, {75, 80}, {110, 140} => 11+6+31=48
7) 6 = 2 + 3 + 1 => {20, 30}, {75, 80, 110}, 140 => 11+36+1=48
8) 6 = 3 + 1 + 2 => {20, 30, 75}, 80, {110, 140} => 56+1+31=88
9) 6 = 3 + 2 + 1 => {20, 30, 75}, {80, 110}, 140 => 56+31+1=88
10) 6 = 4 + 1 + 1 => {20, 30, 75, 80}, 110, 140 => 56+1+1=58
Good luck!